Skip to content

Commit df8c93e

Browse files
committed
added valid configuration constraint
1 parent b4bca09 commit df8c93e

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

PhpUnit/AbstractConfigurationTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ protected function assertConfigurationIsInvalid(array $configurationValues, $exp
3535
);
3636
}
3737

38+
/**
39+
* Assert that the given configuration values are valid.
40+
*
41+
* @param array $configurationValues
42+
*/
43+
protected function assertConfigurationIsValid(array $configurationValues)
44+
{
45+
self::assertThat(
46+
$configurationValues,
47+
new ConfigurationValuesAreValidConstraint(
48+
$this->getConfiguration()
49+
)
50+
);
51+
}
52+
3853
/**
3954
* Assert that the given configuration values, when processed, will equal to the given array
4055
*
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConfigTest\PhpUnit;
4+
5+
use Symfony\Component\Config\Definition\ConfigurationInterface;
6+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
7+
8+
class ConfigurationValuesAreValidConstraint extends AbstractConfigurationConstraint
9+
{
10+
public function __construct(ConfigurationInterface $configuration)
11+
{
12+
parent::__construct($configuration);
13+
}
14+
15+
public function matches($other)
16+
{
17+
$this->validateConfigurationValuesArray($other);
18+
19+
$success = true;
20+
21+
try {
22+
$this->processConfiguration($other);
23+
} catch (InvalidConfigurationException $exception) {
24+
$success = false;
25+
}
26+
27+
return $success;
28+
}
29+
30+
public function toString()
31+
{
32+
return 'is valid for the given configuration';
33+
}
34+
}

Tests/PhpUnit/AbstractConfigurationTestCaseTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ public function it_fails_when_a_configuration_is_valid_when_it_should_have_been_
3939
);
4040
}
4141

42+
/**
43+
* @test
44+
*/
45+
public function it_can_assert_that_a_configuration_is_valid()
46+
{
47+
$this->assertConfigurationIsValid(
48+
array(
49+
array('required_value' => 'some value')
50+
)
51+
);
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function it_fails_when_a_configuration_is_invalid_when_it_should_have_been_valid()
58+
{
59+
$this->setExpectedException('\PHPUnit_Framework_ExpectationFailedException', 'valid');
60+
61+
$this->assertConfigurationIsValid(
62+
array(
63+
array()
64+
)
65+
);
66+
}
67+
4268
/**
4369
* @test
4470
*/
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConfigTest\Tests;
4+
5+
use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationValuesAreValidConstraint;
6+
use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\AlwaysValidConfiguration;
7+
use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\ConfigurationWithRequiredValue;
8+
9+
class ConfigurationValuesAreValidConstraintTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
public function if_configuration_values_is_no_array_it_fails()
15+
{
16+
$constraint = new ConfigurationValuesAreValidConstraint(new AlwaysValidConfiguration());
17+
18+
$this->setExpectedException('\InvalidArgumentException', 'array');
19+
20+
$constraint->evaluate('not an array');
21+
}
22+
23+
/**
24+
* @test
25+
*/
26+
public function if_configuration_values_is_no_array_of_arrays_it_fails()
27+
{
28+
$constraint = new ConfigurationValuesAreValidConstraint(new AlwaysValidConfiguration());
29+
30+
$this->setExpectedException('\InvalidArgumentException', 'array');
31+
32+
$constraint->evaluate(array('not an array'));
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function if_configuration_values_are_valid_it_matches()
39+
{
40+
$constraint = new ConfigurationValuesAreValidConstraint(new AlwaysValidConfiguration());
41+
42+
$this->assertTrue($constraint->evaluate(array(array()), '', true));
43+
}
44+
45+
/**
46+
* @test
47+
*/
48+
public function if_configuration_values_are_invalid_it_does_not_match()
49+
{
50+
$constraint = new ConfigurationValuesAreValidConstraint(new ConfigurationWithRequiredValue());
51+
52+
$this->assertFalse($constraint->evaluate(array(array()), '', true));
53+
}
54+
55+
/**
56+
* @test
57+
*/
58+
public function to_string_returns_a_message()
59+
{
60+
$constraint = new ConfigurationValuesAreValidConstraint(new AlwaysValidConfiguration());
61+
62+
$this->assertSame('is valid for the given configuration', $constraint->toString());
63+
}
64+
}

0 commit comments

Comments
 (0)