Skip to content

Commit 2f0181f

Browse files
Merge branch 'xabbuh-definition-has-tag-constraint'
2 parents f634db8 + 56fcdc6 commit 2f0181f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

PhpUnit/DefinitionHasTagConstraint.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit;
44

5+
use SebastianBergmann\Exporter\Exporter;
56
use Symfony\Component\DependencyInjection\Definition;
67

78
class DefinitionHasTagConstraint extends \PHPUnit_Framework_Constraint
89
{
10+
protected $exporter;
911
private $name;
1012
private $attributes;
1113

1214
public function __construct($name, array $attributes = array())
1315
{
16+
$this->exporter = new Exporter();
1417
$this->name = $name;
1518
$this->attributes = $attributes;
1619
}
@@ -40,14 +43,22 @@ public function evaluate($other, $description = '', $returnResult = false)
4043
sprintf(
4144
'None of the tags matched the expected name "%s" with attributes %s',
4245
$this->name,
43-
\PHPUnit_Util_Type::export($this->attributes)
46+
$this->exporter->export($this->attributes)
4447
)
4548
);
4649
}
4750

51+
if (!$returnResult) {
52+
$this->fail($other, $description);
53+
}
54+
4855
return false;
4956
}
5057

58+
if (!$returnResult) {
59+
$this->fail($other, $description);
60+
}
61+
5162
return false;
5263
}
5364

Tests/PhpUnit/DefinitionHasTagConstraintTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ public function match(Definition $definition, $tag, $attributes, $expectedToMatc
1818
$this->assertSame($expectedToMatch, $constraint->evaluate($definition, '', true));
1919
}
2020

21+
/**
22+
* @test
23+
* @dataProvider definitionProvider
24+
*/
25+
public function evaluateThrowsExceptionOnFailure(Definition $definition, $tag, $attributes, $expectedToMatch)
26+
{
27+
$constraint = new DefinitionHasTagConstraint($tag, $attributes);
28+
29+
if ($expectedToMatch) {
30+
$this->assertTrue($constraint->evaluate($definition));
31+
} else {
32+
try {
33+
$constraint->evaluate($definition);
34+
$this->fail('DefinitionHasTagConstraint doesn\'t throw expected exception');
35+
} catch (\PHPUnit_Framework_ExpectationFailedException $e) {
36+
$this->assertTrue(true, 'DefinitionHasTagConstraint throws expected exception');
37+
}
38+
}
39+
}
40+
2141
public function definitionProvider()
2242
{
2343
$definitionWithoutTags = new Definition();

0 commit comments

Comments
 (0)