Skip to content

Commit 0f030d6

Browse files
author
Iltar van der Berg
committed
Added the trait variant of the configuration tester
1 parent 8d3a62a commit 0f030d6

File tree

6 files changed

+249
-2
lines changed

6 files changed

+249
-2
lines changed

PhpUnit/AbstractConfigurationTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
/**
66
* Extend your test case from this abstract class to test a class that implements
77
* Symfony\Component\Config\Definition\ConfigurationInterface
8+
*
9+
* @deprecated only use this class if you're still running php 5.3. Use
10+
* Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait instead
811
*/
912
abstract class AbstractConfigurationTestCase extends \PHPUnit_Framework_TestCase
1013
{
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConfigTest\PhpUnit;
4+
5+
/**
6+
* Add this trait to your Test Case to add the ability of testing your configuration
7+
* which should implement Symfony\Component\Config\Definition\ConfigurationInterface
8+
*/
9+
trait ConfigurationTestCaseTrait
10+
{
11+
/**
12+
* Return the instance of ConfigurationInterface that should be used by the
13+
* Configuration-specific assertions in this test-case
14+
*
15+
* @return \Symfony\Component\Config\Definition\ConfigurationInterface
16+
*/
17+
abstract protected function getConfiguration();
18+
19+
/**
20+
* Assert that the given configuration values are invalid.
21+
*
22+
* Optionally provide (part of) the exception message that you expect to receive.
23+
*
24+
* When running PHPUnit >=4.3.0, you need to set useRegExp to true if you'd like
25+
* to match the exception message using a regular expression.
26+
*
27+
* @param array $configurationValues
28+
* @param string|null $expectedMessage
29+
* @param bool $useRegExp
30+
*/
31+
protected function assertConfigurationIsInvalid(array $configurationValues, $expectedMessage = null, $useRegExp = false)
32+
{
33+
\PHPUnit_Framework_TestCase::assertThat(
34+
$configurationValues,
35+
new ConfigurationValuesAreInvalidConstraint(
36+
$this->getConfiguration(),
37+
$expectedMessage,
38+
$useRegExp
39+
)
40+
);
41+
}
42+
43+
/**
44+
* Assert that the given configuration values are valid.
45+
*
46+
* @param array $configurationValues
47+
*/
48+
protected function assertConfigurationIsValid(array $configurationValues)
49+
{
50+
\PHPUnit_Framework_TestCase::assertThat(
51+
$configurationValues,
52+
new ConfigurationValuesAreValidConstraint(
53+
$this->getConfiguration()
54+
)
55+
);
56+
}
57+
58+
/**
59+
* Assert that the given configuration values, when processed, will equal to the given array
60+
*
61+
* @param array $configurationValues
62+
* @param array $expectedProcessedConfiguration
63+
*/
64+
protected function assertProcessedConfigurationEquals(
65+
array $configurationValues,
66+
array $expectedProcessedConfiguration
67+
) {
68+
\PHPUnit_Framework_TestCase::assertThat(
69+
$expectedProcessedConfiguration,
70+
new ProcessedConfigurationEqualsConstraint(
71+
$this->getConfiguration(),
72+
$configurationValues
73+
)
74+
);
75+
}
76+
}

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ Using Composer:
1616

1717
## Usage
1818

19+
### php 5.4 and up
20+
21+
Create a test case and use the trait from ``Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait``.
22+
Then implement ``getConfiguration()``:
23+
24+
```php
25+
<?php
26+
27+
class ConfigurationTest extends \PHPUnit_Framework_TestCase
28+
{
29+
use ConfigurationTestCaseTrait;
30+
31+
protected function getConfiguration()
32+
{
33+
return new Configuration();
34+
}
35+
}
36+
```
37+
38+
### php 5.3
39+
1940
Create a test case and extend from ``Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase``. Then implement
2041
``getConfiguration()``:
2142

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

69-
class ConfigurationTest extends AbstractConfigurationTestCase
90+
class ConfigurationTest extends \PHPUnit_Framework_TestCase
7091
{
92+
use ConfigurationTestCaseTrait;
93+
7194
public function testValuesAreInvalidIfRequiredValueIsNotProvided()
7295
{
7396
$this->assertConfigurationIsInvalid(
@@ -85,8 +108,10 @@ You may also want to verify that after processing an array of configuration valu
85108
```php
86109
<?php
87110

88-
class ConfigurationTest extends AbstractConfigurationTestCase
111+
class ConfigurationTest extends \PHPUnit_Framework_TestCase
89112
{
113+
use ConfigurationTestCaseTrait;
114+
90115
public function testProcessedValueContainsRequiredValue()
91116
{
92117
$this->assertProcessedConfigurationEquals(array(

Tests/PhpUnit/AbstractConfigurationTestCaseTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase;
66
use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\ConfigurationWithRequiredValue;
77

8+
/**
9+
* @deprecated should be removed once the minimum php version is elevated to at least 5.4
10+
*/
811
class AbstractConfigurationTestCaseTest extends AbstractConfigurationTestCase
912
{
1013
protected function getConfiguration()
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConfigTest\Tests;
4+
5+
use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait;
6+
use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\ConfigurationWithRequiredValue;
7+
8+
class ConfigurationTestCaseTraitTest extends \PHPUnit_Framework_TestCase
9+
{
10+
use ConfigurationTestCaseTrait;
11+
12+
protected function getConfiguration()
13+
{
14+
return new ConfigurationWithRequiredValue();
15+
}
16+
17+
/**
18+
* @test
19+
*/
20+
public function it_can_assert_that_a_configuration_is_invalid()
21+
{
22+
$this->assertConfigurationIsInvalid(
23+
array(
24+
array() // no configuration values
25+
),
26+
'required_value'
27+
);
28+
}
29+
30+
/**
31+
* @test
32+
*/
33+
public function it_fails_when_a_configuration_is_valid_when_it_should_have_been_invalid()
34+
{
35+
$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'invalid');
36+
37+
$this->assertConfigurationIsInvalid(
38+
array(
39+
array('required_value' => 'some value')
40+
)
41+
);
42+
}
43+
44+
/**
45+
* @test
46+
*/
47+
public function it_can_assert_that_a_configuration_is_valid()
48+
{
49+
$this->assertConfigurationIsValid(
50+
array(
51+
array('required_value' => 'some value')
52+
)
53+
);
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function it_fails_when_a_configuration_is_invalid_when_it_should_have_been_valid()
60+
{
61+
$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'valid');
62+
63+
$this->assertConfigurationIsValid(
64+
array(
65+
array()
66+
)
67+
);
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function it_can_assert_that_a_processed_configuration_matches_the_expected_array_of_values()
74+
{
75+
$value = 'some value';
76+
77+
$this->assertProcessedConfigurationEquals(
78+
array(
79+
array(),
80+
array('required_value' => $value)
81+
),
82+
array(
83+
'required_value' => $value
84+
)
85+
);
86+
}
87+
88+
/**
89+
* @test
90+
*/
91+
public function it_fails_when_a_processed_configuration_does_not_match_the_expected_array_of_values()
92+
{
93+
$value = 'some value';
94+
95+
$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'equal');
96+
$this->assertProcessedConfigurationEquals(
97+
array(
98+
array('required_value' => $value)
99+
),
100+
array(
101+
'invalid_key' => 'invalid_value'
102+
)
103+
);
104+
}
105+
106+
/**
107+
* @test
108+
*/
109+
public function it_throws_a_comparison_failed_exception_with_the_values_in_the_right_order()
110+
{
111+
$value = 'some value';
112+
113+
//$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'equal');
114+
$configurationValues = array(
115+
array('required_value' => $value)
116+
);
117+
118+
$expectedProcessedConfigurationValues = array(
119+
'invalid_key' => 'invalid_value'
120+
);
121+
122+
try {
123+
$this->assertProcessedConfigurationEquals(
124+
$configurationValues,
125+
$expectedProcessedConfigurationValues
126+
);
127+
} catch (\PHPUnit_Framework_ExpectationFailedException $exception) {
128+
$this->assertSame(
129+
$expectedProcessedConfigurationValues,
130+
$exception->getComparisonFailure()->getExpected()
131+
);
132+
}
133+
}
134+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// TODO put the actual class in this file when the minimum requirement is 5.4
4+
if (PHP_VERSION_ID > 50400) {
5+
require_once 'ConfigurationTestCaseTraitActual.php';
6+
}

0 commit comments

Comments
 (0)