Skip to content

Commit b34f21d

Browse files
committed
Added tests for Httplug factory
1 parent 6a2de00 commit b34f21d

File tree

2 files changed

+125
-4
lines changed

2 files changed

+125
-4
lines changed

HttplugFactory.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ private function findOneByType($type)
7979
* @param mixed $condition
8080
*
8181
* @return bool
82-
*
83-
* TODO: review this method
8482
*/
8583
protected function evaluateCondition($condition)
8684
{
@@ -95,8 +93,8 @@ protected function evaluateCondition($condition)
9593
$evaluatedCondition = true;
9694

9795
// Immediately stop execution if the condition is false
98-
for ($i = 0; $i < count($condition) && false !== $evaluatedCondition; ++$i) {
99-
$evaluatedCondition &= $this->evaluateCondition($condition[$i]);
96+
while (count($condition) > 0 && $evaluatedCondition) {
97+
$evaluatedCondition = $this->evaluateCondition(array_shift($condition));
10098
}
10199

102100
return $evaluatedCondition;

Tests/Unit/HttplugFactoryTest.php

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Tests\Unit;
4+
5+
use Http\HttplugBundle\ClientFactory\DummyClient;
6+
use Http\HttplugBundle\Collector\MessageJournal;
7+
use Http\HttplugBundle\HttplugFactory;
8+
use Puli\Discovery\Api\Discovery;
9+
use Puli\Discovery\Binding\ClassBinding;
10+
use Webmozart\Expression\Expr;
11+
12+
/**
13+
* @author Tobias Nyholm <[email protected]>
14+
*/
15+
class HttplugFactoryTest extends \PHPUnit_Framework_TestCase
16+
{
17+
public function testEvaluateConditionString()
18+
{
19+
$existingClass = MessageJournal::class;
20+
$factory = $this->getMockedFactory($existingClass);
21+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
22+
}
23+
24+
/**
25+
* @expectedException \RuntimeException
26+
*/
27+
public function testEvaluateConditionInvalidString()
28+
{
29+
// String
30+
$factory = $this->getMockedFactory('non_existent_class');
31+
$factory->find('type');
32+
}
33+
34+
public function testEvaluateConditionCallableTrue()
35+
{
36+
$factory = $this->getMockedFactory(
37+
function () {
38+
return true;
39+
}
40+
);
41+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
42+
}
43+
44+
/**
45+
* @expectedException \RuntimeException
46+
*/
47+
public function testEvaluateConditionCallableFalse()
48+
{
49+
$factory = $this->getMockedFactory(
50+
function () {
51+
return false;
52+
}
53+
);
54+
55+
$factory->find('type');
56+
}
57+
58+
public function testEvaluateConditionBooleanTrue()
59+
{
60+
$factory = $this->getMockedFactory(true);
61+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
62+
}
63+
64+
/**
65+
* @expectedException \RuntimeException
66+
*/
67+
public function testEvaluateConditionBooleanFalse()
68+
{
69+
$factory = $this->getMockedFactory(false);
70+
$factory->find('type');
71+
}
72+
73+
public function testEvaluateConditionArrayTrue()
74+
{
75+
$factory = $this->getMockedFactory([true, true]);
76+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
77+
}
78+
79+
/**
80+
* @expectedException \RuntimeException
81+
*/
82+
public function testEvaluateConditionArrayFalse()
83+
{
84+
$factory = $this->getMockedFactory([true, false, true]);
85+
$factory->find('type');
86+
}
87+
88+
public function testEvaluateConditionArrayAssoc()
89+
{
90+
$factory = $this->getMockedFactory(['test1' => true, true]);
91+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
92+
}
93+
94+
/**
95+
* @expectedException \RuntimeException
96+
*/
97+
public function testEvaluateConditionObject()
98+
{
99+
$factory = $this->getMockedFactory(new \DateTime());
100+
$factory->find('type');
101+
}
102+
103+
/**
104+
* @param $condition
105+
*
106+
* @return HttplugFactory
107+
*/
108+
private function getMockedFactory($condition)
109+
{
110+
$discovery = $this->prophesize(Discovery::class);
111+
$factory = new HttplugFactory($discovery->reveal());
112+
$binding = $this->prophesize(ClassBinding::class);
113+
$discovery->findBindings('type', Expr::isInstanceOf('Puli\Discovery\Binding\ClassBinding'))->willReturn(
114+
[$binding->reveal()]
115+
);
116+
117+
$binding->hasParameterValue('depends')->willReturn(true);
118+
$binding->getClassName()->willReturn(DummyClient::class);
119+
$binding->getParameterValue('depends')->willReturn($condition);
120+
121+
return $factory;
122+
}
123+
}

0 commit comments

Comments
 (0)