Skip to content

Commit b01c61f

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

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

HttplugFactory.php

Lines changed: 6 additions & 5 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,11 +93,14 @@ 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+
foreach ($condition as $c) {
97+
$evaluatedCondition = $evaluatedCondition && $this->evaluateCondition($c);
98+
if ($evaluatedCondition === false) {
99+
return false;
100+
}
100101
}
101102

102-
return $evaluatedCondition;
103+
return true;
103104
}
104105

105106
return false;

Tests/Unit/HttplugFactoryTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\Tests\Unit\ClientFactory;
4+
5+
use Http\HttplugBundle\Collector\MessageJournal;
6+
use Http\HttplugBundle\HttplugFactory;
7+
use Puli\Discovery\Api\Discovery;
8+
9+
10+
/**
11+
* @author Tobias Nyholm <[email protected]>
12+
*/
13+
class HttplugFactoryTest extends \PHPUnit_Framework_TestCase
14+
{
15+
public function testEvaluateCondition()
16+
{
17+
$method = new \ReflectionMethod(HttplugFactory::class, 'evaluateCondition');
18+
$method->setAccessible(true);
19+
$factory = new HttplugFactory($this->getMock(Discovery::class));
20+
$existingClass = MessageJournal::class;
21+
22+
// String
23+
$result = $method->invoke($factory, $existingClass);
24+
$this->assertTrue($result);
25+
$result = $method->invoke($factory, 'non_existent_class');
26+
$this->assertFalse($result);
27+
28+
// Callable
29+
$result = $method->invoke($factory, function() {return 'value';});
30+
$this->assertEquals('value', $result);
31+
32+
// Boolean
33+
$result = $method->invoke($factory, true);
34+
$this->assertTrue($result);
35+
$result = $method->invoke($factory, false);
36+
$this->assertFalse($result);
37+
38+
// Array
39+
$result = $method->invoke($factory, [true, $existingClass]);
40+
$this->assertTrue((bool) $result);
41+
$result = $method->invoke($factory, [true, $existingClass, false]);
42+
$this->assertFalse((bool) $result);
43+
44+
// Associative array
45+
$result = $method->invoke($factory, ['test1'=>true, $existingClass]);
46+
$this->assertTrue((bool) $result);
47+
48+
}
49+
}

0 commit comments

Comments
 (0)