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
15 changes: 15 additions & 0 deletions PhpUnit/AbstractConfigurationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
34 changes: 34 additions & 0 deletions PhpUnit/ConfigurationValuesAreValidConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Matthias\SymfonyConfigTest\PhpUnit;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

class ConfigurationValuesAreValidConstraint extends AbstractConfigurationConstraint
{
public function __construct(ConfigurationInterface $configuration)
{
parent::__construct($configuration);
}

public function matches($other)
{
$this->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';
}
}
26 changes: 26 additions & 0 deletions Tests/PhpUnit/AbstractConfigurationTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
64 changes: 64 additions & 0 deletions Tests/PhpUnit/ConfigurationValuesAreValidConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Matthias\SymfonyConfigTest\Tests;

use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationValuesAreValidConstraint;
use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\AlwaysValidConfiguration;
use Matthias\SymfonyConfigTest\Tests\PhpUnit\Fixtures\ConfigurationWithRequiredValue;

class ConfigurationValuesAreValidConstraintTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function if_configuration_values_is_no_array_it_fails()
{
$constraint = new ConfigurationValuesAreValidConstraint(new AlwaysValidConfiguration());

$this->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());
}
}