Skip to content

Commit 1aa904f

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

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
* @author Tobias Nyholm <[email protected]>
11+
*/
12+
class HttplugFactoryTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testEvaluateCondition()
15+
{
16+
$method = new \ReflectionMethod(HttplugFactory::class, 'evaluateCondition');
17+
$method->setAccessible(true);
18+
$factory = new HttplugFactory($this->getMock(Discovery::class));
19+
$existingClass = MessageJournal::class;
20+
21+
// String
22+
$result = $method->invoke($factory, $existingClass);
23+
$this->assertTrue($result);
24+
$result = $method->invoke($factory, 'non_existent_class');
25+
$this->assertFalse($result);
26+
27+
// Callable
28+
$result = $method->invoke($factory, function () {return 'value'; });
29+
$this->assertEquals('value', $result);
30+
31+
// Boolean
32+
$result = $method->invoke($factory, true);
33+
$this->assertTrue($result);
34+
$result = $method->invoke($factory, false);
35+
$this->assertFalse($result);
36+
37+
// Array
38+
$result = $method->invoke($factory, [true, $existingClass]);
39+
$this->assertTrue((bool) $result);
40+
$result = $method->invoke($factory, [true, $existingClass, false]);
41+
$this->assertFalse((bool) $result);
42+
43+
// Associative array
44+
$result = $method->invoke($factory, ['test1' => true, $existingClass]);
45+
$this->assertTrue((bool) $result);
46+
}
47+
}

0 commit comments

Comments
 (0)