Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional defensive programming tests #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# definition-interop compiler's test suite
# definition-interop container's test suite

Modules (aka packages or bundles) are widespread in modern frameworks. Unfortunately each framework has its own convention and tools for writing them. The goal of *container-interop* and more specifically *definition-interop* is to help developers write modules that can work in any framework.

*definition-interop* contains interfaces helping developers describe container definitions (objects that can be cast to a container entry).

This package contains a set of test suites that can be used to ensure that a container/compiler is indeed compatible with definition-interop.
This package contains a set of test suites that can be used to ensure that a container/compiler is indeed compatible with *definition-interop*.

## How does it work?

Expand All @@ -15,5 +15,7 @@ Container / compilers compatible with *container-interop* should be able to pass
## Usage

This package contains a `AbstractDefinitionCompatibilityTest` class. This is an abstract PHPUnit test class.
In your package, you should extend this class and implement the `compileDefinitions` method. This method should return a container-interop compatible container.
In your package, you should extend this class and implement 2 abstract methods:

- `compileDefinitions` method: This method should return a container-interop compatible container.
- `shouldRunDefensiveProgrammingTests` method: This method should return `true` or `false`. Return true if you want to test your container against defensive programming best practices. If you enable this, tests will provide invalid data to your container and it is expected that your container will throw an exception. Defensive programming tests are out-of-scope of *definition-interop* so they are not compulsory, only recommended.
19 changes: 19 additions & 0 deletions src/AbstractDefinitionCompatibilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ abstract class AbstractDefinitionCompatibilityTest extends \PHPUnit_Framework_Te
*/
abstract protected function getContainer(DefinitionProviderInterface $definitionProvider);

/**
* Returns true if the tested container should be tested against defensive programming tests.
* Returns false if the tested container should not be tested against defensive programming tests.
*
* @return boolean
*/
abstract protected function shouldRunDefensiveProgrammingTests();

public function testInstance()
{
$referenceDefinition = new \Assembly\ObjectDefinition('\\stdClass');
Expand All @@ -41,13 +49,18 @@ public function testInstance()
}

/**
* Defensive programming test.
* Invalid objects (objects not extending one of the xxxDefinitionInterface interfaces) should trigger
* an exception.
*
* @expectedException \Exception
*/
public function testParameterException()
{
if (!$this->shouldRunDefensiveProgrammingTests()) {
throw new \Exception();
}

$assemblyDefinition = new \Assembly\ObjectDefinition('Interop\Container\Definition\Test\Fixtures\\Test');
$assemblyDefinition->addConstructorArgument(new \stdClass());

Expand Down Expand Up @@ -121,10 +134,16 @@ public function testFactory()
}

/**
* Defensive programming test.
*
* @expectedException \Exception
*/
public function testUnsupportedDefinition()
{
if (!$this->shouldRunDefensiveProgrammingTests()) {
throw new \Exception();
}

$definition = new UnsupportedDefinition();

$container = $this->getContainer(new ArrayDefinitionProvider([
Expand Down