From df8c93e90bce24d4edc89b51fa7062207f78f5e2 Mon Sep 17 00:00:00 2001 From: satahippy Date: Wed, 10 Sep 2014 17:06:48 +0400 Subject: [PATCH] added valid configuration constraint --- PhpUnit/AbstractConfigurationTestCase.php | 15 +++++ .../ConfigurationValuesAreValidConstraint.php | 34 ++++++++++ .../AbstractConfigurationTestCaseTest.php | 26 ++++++++ ...figurationValuesAreValidConstraintTest.php | 64 +++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 PhpUnit/ConfigurationValuesAreValidConstraint.php create mode 100644 Tests/PhpUnit/ConfigurationValuesAreValidConstraintTest.php diff --git a/PhpUnit/AbstractConfigurationTestCase.php b/PhpUnit/AbstractConfigurationTestCase.php index 5b26803..ddfd020 100644 --- a/PhpUnit/AbstractConfigurationTestCase.php +++ b/PhpUnit/AbstractConfigurationTestCase.php @@ -35,6 +35,21 @@ protected function assertConfigurationIsInvalid(array $configurationValues, $exp ); } + /** + * Assert that the given configuration values are valid. + * + * @param array $configurationValues + */ + protected function assertConfigurationIsValid(array $configurationValues) + { + self::assertThat( + $configurationValues, + new ConfigurationValuesAreValidConstraint( + $this->getConfiguration() + ) + ); + } + /** * Assert that the given configuration values, when processed, will equal to the given array * diff --git a/PhpUnit/ConfigurationValuesAreValidConstraint.php b/PhpUnit/ConfigurationValuesAreValidConstraint.php new file mode 100644 index 0000000..9b36499 --- /dev/null +++ b/PhpUnit/ConfigurationValuesAreValidConstraint.php @@ -0,0 +1,34 @@ +validateConfigurationValuesArray($other); + + $success = true; + + try { + $this->processConfiguration($other); + } catch (InvalidConfigurationException $exception) { + $success = false; + } + + return $success; + } + + public function toString() + { + return 'is valid for the given configuration'; + } +} diff --git a/Tests/PhpUnit/AbstractConfigurationTestCaseTest.php b/Tests/PhpUnit/AbstractConfigurationTestCaseTest.php index e1bb196..52d3a09 100644 --- a/Tests/PhpUnit/AbstractConfigurationTestCaseTest.php +++ b/Tests/PhpUnit/AbstractConfigurationTestCaseTest.php @@ -39,6 +39,32 @@ public function it_fails_when_a_configuration_is_valid_when_it_should_have_been_ ); } + /** + * @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 */ diff --git a/Tests/PhpUnit/ConfigurationValuesAreValidConstraintTest.php b/Tests/PhpUnit/ConfigurationValuesAreValidConstraintTest.php new file mode 100644 index 0000000..80ebdbe --- /dev/null +++ b/Tests/PhpUnit/ConfigurationValuesAreValidConstraintTest.php @@ -0,0 +1,64 @@ +setExpectedException('\InvalidArgumentException', 'array'); + + $constraint->evaluate('not an array'); + } + + /** + * @test + */ + public function if_configuration_values_is_no_array_of_arrays_it_fails() + { + $constraint = new ConfigurationValuesAreValidConstraint(new AlwaysValidConfiguration()); + + $this->setExpectedException('\InvalidArgumentException', 'array'); + + $constraint->evaluate(array('not an array')); + } + + /** + * @test + */ + public function if_configuration_values_are_valid_it_matches() + { + $constraint = new ConfigurationValuesAreValidConstraint(new AlwaysValidConfiguration()); + + $this->assertTrue($constraint->evaluate(array(array()), '', true)); + } + + /** + * @test + */ + public function if_configuration_values_are_invalid_it_does_not_match() + { + $constraint = new ConfigurationValuesAreValidConstraint(new ConfigurationWithRequiredValue()); + + $this->assertFalse($constraint->evaluate(array(array()), '', true)); + } + + /** + * @test + */ + public function to_string_returns_a_message() + { + $constraint = new ConfigurationValuesAreValidConstraint(new AlwaysValidConfiguration()); + + $this->assertSame('is valid for the given configuration', $constraint->toString()); + } +}