Skip to content

Commit ce15822

Browse files
Revert "Remove deprecated things"
This reverts commit 238ec28.
1 parent e88ffda commit ce15822

10 files changed

+424
-183
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Changelog
22

3-
## v2.0.0
3+
## 1.2.0
44

55
- Use a breadcrumb path to test only one particular part of the configuration node tree.
66

7+
## 1.1.0
8+
9+
- Add a trait for test cases, mark the abstract base class "deprecated"
10+
711
## v0.1.1
812

913
- Fixed issue #1: ``ProcessedConfigurationEqualsConstraint`` had expected and actual value mixed up

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2013-2015 Matthias Noback
1+
Copyright (c) 2013 Matthias Noback
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConfigTest\PhpUnit;
4+
5+
/**
6+
* Extend your test case from this abstract class to test a class that implements
7+
* 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
11+
*/
12+
abstract class AbstractConfigurationTestCase extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* Return the instance of ConfigurationInterface that should be used by the
16+
* Configuration-specific assertions in this test-case
17+
*
18+
* @return \Symfony\Component\Config\Definition\ConfigurationInterface
19+
*/
20+
abstract protected function getConfiguration();
21+
22+
/**
23+
* Assert that the given configuration values are invalid.
24+
*
25+
* Optionally provide (part of) the exception message that you expect to receive.
26+
*
27+
* When running PHPUnit >=4.3.0, you need to set useRegExp to true if you'd like
28+
* to match the exception message using a regular expression.
29+
*
30+
* @param array $configurationValues
31+
* @param string|null $expectedMessage
32+
* @param bool $useRegExp
33+
*/
34+
protected function assertConfigurationIsInvalid(array $configurationValues, $expectedMessage = null, $useRegExp = false)
35+
{
36+
\PHPUnit_Framework_TestCase::assertThat(
37+
$configurationValues,
38+
new ConfigurationValuesAreInvalidConstraint(
39+
$this->getConfiguration(),
40+
$expectedMessage,
41+
$useRegExp
42+
)
43+
);
44+
}
45+
46+
/**
47+
* Assert that the given configuration values are invalid.
48+
*
49+
* Optionally provide (part of) the exception message that you expect to receive.
50+
*
51+
* When running PHPUnit >=4.3.0, you need to set useRegExp to true if you'd like
52+
* to match the exception message using a regular expression.
53+
*
54+
* @param array $configurationValues
55+
* @param string $breadcrumbPath The path that should be validated, e.g. "doctrine.orm"
56+
* @param string|null $expectedMessage
57+
* @param bool $useRegExp
58+
*/
59+
protected function assertPartialConfigurationIsInvalid(
60+
array $configurationValues,
61+
$breadcrumbPath,
62+
$expectedMessage = null,
63+
$useRegExp = false
64+
) {
65+
\PHPUnit_Framework_TestCase::assertThat(
66+
$configurationValues,
67+
new ConfigurationValuesAreInvalidConstraint(
68+
$this->getConfiguration(),
69+
$expectedMessage,
70+
$useRegExp,
71+
$breadcrumbPath
72+
)
73+
);
74+
}
75+
76+
/**
77+
* Assert that the given configuration values are valid.
78+
*
79+
* Optionally provide the part of the configuration that you want to test, e.g. "doctrine.orm"
80+
*
81+
* @param array $configurationValues
82+
* @param string|null $breadcrumbPath
83+
*/
84+
protected function assertConfigurationIsValid(array $configurationValues, $breadcrumbPath = null)
85+
{
86+
\PHPUnit_Framework_TestCase::assertThat(
87+
$configurationValues,
88+
new ConfigurationValuesAreValidConstraint(
89+
$this->getConfiguration(),
90+
$breadcrumbPath
91+
)
92+
);
93+
}
94+
95+
/**
96+
* Assert that the given configuration values, when processed, will equal to the given array.
97+
*
98+
* Optionally provide the part of the configuration that you want to test, e.g. "doctrine.orm"
99+
*
100+
* @param array $configurationValues
101+
* @param array $expectedProcessedConfiguration
102+
* @param string|null $breadcrumbPath
103+
*/
104+
protected function assertProcessedConfigurationEquals(
105+
array $configurationValues,
106+
array $expectedProcessedConfiguration,
107+
$breadcrumbPath = null
108+
) {
109+
\PHPUnit_Framework_TestCase::assertThat(
110+
$expectedProcessedConfiguration,
111+
new ProcessedConfigurationEqualsConstraint(
112+
$this->getConfiguration(),
113+
$configurationValues,
114+
$breadcrumbPath
115+
)
116+
);
117+
}
118+
}

PhpUnit/ConfigurationTestCaseTrait.php

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -40,75 +40,36 @@ protected function assertConfigurationIsInvalid(array $configurationValues, $exp
4040
);
4141
}
4242

43-
/**
44-
* Assert that the given configuration values are invalid.
45-
*
46-
* Optionally provide (part of) the exception message that you expect to receive.
47-
*
48-
* When running PHPUnit >=4.3.0, you need to set useRegExp to true if you'd like
49-
* to match the exception message using a regular expression.
50-
*
51-
* @param array $configurationValues
52-
* @param string $breadcrumbPath The path that should be validated, e.g. "doctrine.orm"
53-
* @param string|null $expectedMessage
54-
* @param bool $useRegExp
55-
*/
56-
protected function assertPartialConfigurationIsInvalid(
57-
array $configurationValues,
58-
$breadcrumbPath,
59-
$expectedMessage = null,
60-
$useRegExp = false
61-
) {
62-
\PHPUnit_Framework_TestCase::assertThat(
63-
$configurationValues,
64-
new ConfigurationValuesAreInvalidConstraint(
65-
$this->getConfiguration(),
66-
$expectedMessage,
67-
$useRegExp,
68-
$breadcrumbPath
69-
)
70-
);
71-
}
72-
7343
/**
7444
* Assert that the given configuration values are valid.
7545
*
76-
* Optionally provide the part of the configuration that you want to test, e.g. "doctrine.orm"
77-
*
7846
* @param array $configurationValues
79-
* @param string|null $breadcrumbPath
8047
*/
81-
protected function assertConfigurationIsValid(array $configurationValues, $breadcrumbPath = null)
48+
protected function assertConfigurationIsValid(array $configurationValues)
8249
{
8350
\PHPUnit_Framework_TestCase::assertThat(
8451
$configurationValues,
8552
new ConfigurationValuesAreValidConstraint(
86-
$this->getConfiguration(),
87-
$breadcrumbPath
53+
$this->getConfiguration()
8854
)
8955
);
9056
}
9157

9258
/**
93-
* Assert that the given configuration values, when processed, will equal to the given array.
94-
*
95-
* Optionally provide the part of the configuration that you want to test, e.g. "doctrine.orm"
59+
* Assert that the given configuration values, when processed, will equal to the given array
9660
*
9761
* @param array $configurationValues
9862
* @param array $expectedProcessedConfiguration
99-
* @param string|null $breadcrumbPath
10063
*/
10164
protected function assertProcessedConfigurationEquals(
10265
array $configurationValues,
103-
array $expectedProcessedConfiguration,
104-
$breadcrumbPath = null
66+
array $expectedProcessedConfiguration
10567
) {
10668
\PHPUnit_Framework_TestCase::assertThat(
10769
$expectedProcessedConfiguration,
10870
new ProcessedConfigurationEqualsConstraint(
10971
$this->getConfiguration(),
110-
$configurationValues,
111-
$breadcrumbPath
72+
$configurationValues
11273
)
11374
);
11475
}

README.md

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

1717
## Usage
1818

19+
### PHP 5.4 and up
20+
1921
Create a test case and use the trait from ``Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait``.
2022
Then implement ``getConfiguration()``:
2123

@@ -33,6 +35,23 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
3335
}
3436
```
3537

38+
### PHP 5.3
39+
40+
Create a test case and extend from ``Matthias\SymfonyConfigTest\PhpUnit\AbstractConfigurationTestCase``. Then implement
41+
``getConfiguration()``:
42+
43+
```php
44+
<?php
45+
46+
class ConfigurationTest extends AbstractConfigurationTestCase
47+
{
48+
protected function getConfiguration()
49+
{
50+
return new Configuration();
51+
}
52+
}
53+
```
54+
3655
### Test invalid configuration values
3756

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

0 commit comments

Comments
 (0)