Skip to content

Commit 8fa2c99

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

File tree

2 files changed

+140
-4
lines changed

2 files changed

+140
-4
lines changed

HttplugFactory.php

Lines changed: 2 additions & 4 deletions
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

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
/**
18+
* @var \Prophecy\Prophet
19+
*/
20+
private $prophet;
21+
22+
protected function setup()
23+
{
24+
$this->prophet = new \Prophecy\Prophet();
25+
}
26+
27+
protected function tearDown()
28+
{
29+
$this->prophet->checkPredictions();
30+
}
31+
32+
public function testEvaluateConditionString()
33+
{
34+
$existingClass = MessageJournal::class;
35+
$factory = $this->getMockedFactory($existingClass);
36+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
37+
}
38+
39+
/**
40+
* @expectedException \RuntimeException
41+
*/
42+
public function testEvaluateConditionInvalidString()
43+
{
44+
// String
45+
$factory = $this->getMockedFactory('non_existent_class');
46+
$factory->find('type');
47+
}
48+
49+
public function testEvaluateConditionCallableTrue()
50+
{
51+
$factory = $this->getMockedFactory(
52+
function () {
53+
return true;
54+
}
55+
);
56+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
57+
}
58+
59+
/**
60+
* @expectedException \RuntimeException
61+
*/
62+
public function testEvaluateConditionCallableFalse()
63+
{
64+
$factory = $this->getMockedFactory(
65+
function () {
66+
return false;
67+
}
68+
);
69+
70+
$factory->find('type');
71+
}
72+
73+
public function testEvaluateConditionBooleanTrue()
74+
{
75+
$factory = $this->getMockedFactory(true);
76+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
77+
}
78+
79+
/**
80+
* @expectedException \RuntimeException
81+
*/
82+
public function testEvaluateConditionBooleanFalse()
83+
{
84+
$factory = $this->getMockedFactory(false);
85+
$factory->find('type');
86+
}
87+
88+
public function testEvaluateConditionArrayTrue()
89+
{
90+
$factory = $this->getMockedFactory([true, true]);
91+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
92+
}
93+
94+
/**
95+
* @expectedException \RuntimeException
96+
*/
97+
public function testEvaluateConditionArrayFalse()
98+
{
99+
$factory = $this->getMockedFactory([true, false, true]);
100+
$factory->find('type');
101+
}
102+
103+
public function testEvaluateConditionArrayAssoc()
104+
{
105+
$factory = $this->getMockedFactory(['test1' => true, true]);
106+
$this->assertInstanceOf(DummyClient::class, $factory->find('type'));
107+
}
108+
109+
/**
110+
* @expectedException \RuntimeException
111+
*/
112+
public function testEvaluateConditionObject()
113+
{
114+
$factory = $this->getMockedFactory(new \DateTime());
115+
$factory->find('type');
116+
}
117+
118+
/**
119+
* @param $condition
120+
*
121+
* @return HttplugFactory
122+
*/
123+
private function getMockedFactory($condition)
124+
{
125+
$discovery = $this->prophet->prophesize(Discovery::class);
126+
$factory = new HttplugFactory($discovery->reveal());
127+
$binding = $this->prophet->prophesize(ClassBinding::class);
128+
$discovery->findBindings('type', Expr::isInstanceOf('Puli\Discovery\Binding\ClassBinding'))->willReturn(
129+
[$binding->reveal()]
130+
);
131+
132+
$binding->hasParameterValue('depends')->willReturn(true);
133+
$binding->getClassName()->willReturn(DummyClient::class);
134+
$binding->getParameterValue('depends')->willReturn($condition);
135+
136+
return $factory;
137+
}
138+
}

0 commit comments

Comments
 (0)