Skip to content
Merged
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
3 changes: 3 additions & 0 deletions PhpUnit/AbstractConfigurationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
/**
* Extend your test case from this abstract class to test a class that implements
* Symfony\Component\Config\Definition\ConfigurationInterface
*
* @deprecated only use this class if you're still running php 5.3. Use
* Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait instead
*/
abstract class AbstractConfigurationTestCase extends \PHPUnit_Framework_TestCase
{
Expand Down
76 changes: 76 additions & 0 deletions PhpUnit/ConfigurationTestCaseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Matthias\SymfonyConfigTest\PhpUnit;

/**
* Add this trait to your Test Case to add the ability of testing your configuration
* which should implement Symfony\Component\Config\Definition\ConfigurationInterface
*/
trait ConfigurationTestCaseTrait
{
/**
* Return the instance of ConfigurationInterface that should be used by the
* Configuration-specific assertions in this test-case
*
* @return \Symfony\Component\Config\Definition\ConfigurationInterface
*/
abstract protected function getConfiguration();

/**
* Assert that the given configuration values are invalid.
*
* Optionally provide (part of) the exception message that you expect to receive.
*
* When running PHPUnit >=4.3.0, you need to set useRegExp to true if you'd like
* to match the exception message using a regular expression.
*
* @param array $configurationValues
* @param string|null $expectedMessage
* @param bool $useRegExp
*/
protected function assertConfigurationIsInvalid(array $configurationValues, $expectedMessage = null, $useRegExp = false)
{
\PHPUnit_Framework_TestCase::assertThat(
$configurationValues,
new ConfigurationValuesAreInvalidConstraint(
$this->getConfiguration(),
$expectedMessage,
$useRegExp
)
);
}

/**
* Assert that the given configuration values are valid.
*
* @param array $configurationValues
*/
protected function assertConfigurationIsValid(array $configurationValues)
{
\PHPUnit_Framework_TestCase::assertThat(
$configurationValues,
new ConfigurationValuesAreValidConstraint(
$this->getConfiguration()
)
);
}

/**
* Assert that the given configuration values, when processed, will equal to the given array
*
* @param array $configurationValues
* @param array $expectedProcessedConfiguration
*/
protected function assertProcessedConfigurationEquals(
array $configurationValues,
array $expectedProcessedConfiguration
) {
\PHPUnit_Framework_TestCase::assertThat(
$expectedProcessedConfiguration,
new ProcessedConfigurationEqualsConstraint(
$this->getConfiguration(),
$configurationValues
)
);
}
}
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ Using Composer:

## Usage

### php 5.4 and up

Create a test case and use the trait from ``Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait``.
Then implement ``getConfiguration()``:

```php
<?php

class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
use ConfigurationTestCaseTrait;

protected function getConfiguration()
{
return new Configuration();
}
}
```

### php 5.3

Create a test case and extend from ``Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase``. Then implement
``getConfiguration()``:

Expand Down Expand Up @@ -66,8 +87,10 @@ When you provide an empty array as the values for this configuration, you would
```php
<?php

class ConfigurationTest extends AbstractConfigurationTestCase
class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
use ConfigurationTestCaseTrait;

public function testValuesAreInvalidIfRequiredValueIsNotProvided()
{
$this->assertConfigurationIsInvalid(
Expand All @@ -85,8 +108,10 @@ You may also want to verify that after processing an array of configuration valu
```php
<?php

class ConfigurationTest extends AbstractConfigurationTestCase
class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
use ConfigurationTestCaseTrait;

public function testProcessedValueContainsRequiredValue()
{
$this->assertProcessedConfigurationEquals(array(
Expand Down
3 changes: 3 additions & 0 deletions Tests/PhpUnit/AbstractConfigurationTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase;
use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\ConfigurationWithRequiredValue;

/**
* @deprecated should be removed once the minimum php version is elevated to at least 5.4
*/
class AbstractConfigurationTestCaseTest extends AbstractConfigurationTestCase
{
protected function getConfiguration()
Expand Down
134 changes: 134 additions & 0 deletions Tests/PhpUnit/ConfigurationTestCaseTraitActual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace Matthias\SymfonyConfigTest\Tests;

use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait;
use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\ConfigurationWithRequiredValue;

class ConfigurationTestCaseTraitTest extends \PHPUnit_Framework_TestCase
{
use ConfigurationTestCaseTrait;

protected function getConfiguration()
{
return new ConfigurationWithRequiredValue();
}

/**
* @test
*/
public function it_can_assert_that_a_configuration_is_invalid()
{
$this->assertConfigurationIsInvalid(
array(
array() // no configuration values
),
'required_value'
);
}

/**
* @test
*/
public function it_fails_when_a_configuration_is_valid_when_it_should_have_been_invalid()
{
$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'invalid');

$this->assertConfigurationIsInvalid(
array(
array('required_value' => 'some value')
)
);
}

/**
* @test
*/
public function it_can_assert_that_a_configuration_is_valid()
{
$this->assertConfigurationIsValid(
array(
array('required_value' => 'some value')
)
);
}

/**
* @test
*/
public function it_fails_when_a_configuration_is_invalid_when_it_should_have_been_valid()
{
$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'valid');

$this->assertConfigurationIsValid(
array(
array()
)
);
}

/**
* @test
*/
public function it_can_assert_that_a_processed_configuration_matches_the_expected_array_of_values()
{
$value = 'some value';

$this->assertProcessedConfigurationEquals(
array(
array(),
array('required_value' => $value)
),
array(
'required_value' => $value
)
);
}

/**
* @test
*/
public function it_fails_when_a_processed_configuration_does_not_match_the_expected_array_of_values()
{
$value = 'some value';

$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'equal');
$this->assertProcessedConfigurationEquals(
array(
array('required_value' => $value)
),
array(
'invalid_key' => 'invalid_value'
)
);
}

/**
* @test
*/
public function it_throws_a_comparison_failed_exception_with_the_values_in_the_right_order()
{
$value = 'some value';

//$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'equal');
$configurationValues = array(
array('required_value' => $value)
);

$expectedProcessedConfigurationValues = array(
'invalid_key' => 'invalid_value'
);

try {
$this->assertProcessedConfigurationEquals(
$configurationValues,
$expectedProcessedConfigurationValues
);
} catch (\PHPUnit_Framework_ExpectationFailedException $exception) {
$this->assertSame(
$expectedProcessedConfigurationValues,
$exception->getComparisonFailure()->getExpected()
);
}
}
}
6 changes: 6 additions & 0 deletions Tests/PhpUnit/ConfigurationTestCaseTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// TODO put the actual class in this file when the minimum requirement is 5.4
if (PHP_VERSION_ID > 50400) {
require_once 'ConfigurationTestCaseTraitActual.php';
}