From b34f21d92158ffcd25d19e7e3b31ddbd7267465e Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 3 Mar 2016 14:02:57 +0100 Subject: [PATCH] Added tests for Httplug factory --- HttplugFactory.php | 6 +- Tests/Unit/HttplugFactoryTest.php | 123 ++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 Tests/Unit/HttplugFactoryTest.php diff --git a/HttplugFactory.php b/HttplugFactory.php index fe8922da..080635fc 100644 --- a/HttplugFactory.php +++ b/HttplugFactory.php @@ -79,8 +79,6 @@ private function findOneByType($type) * @param mixed $condition * * @return bool - * - * TODO: review this method */ protected function evaluateCondition($condition) { @@ -95,8 +93,8 @@ protected function evaluateCondition($condition) $evaluatedCondition = true; // Immediately stop execution if the condition is false - for ($i = 0; $i < count($condition) && false !== $evaluatedCondition; ++$i) { - $evaluatedCondition &= $this->evaluateCondition($condition[$i]); + while (count($condition) > 0 && $evaluatedCondition) { + $evaluatedCondition = $this->evaluateCondition(array_shift($condition)); } return $evaluatedCondition; diff --git a/Tests/Unit/HttplugFactoryTest.php b/Tests/Unit/HttplugFactoryTest.php new file mode 100644 index 00000000..5914bbca --- /dev/null +++ b/Tests/Unit/HttplugFactoryTest.php @@ -0,0 +1,123 @@ + + */ +class HttplugFactoryTest extends \PHPUnit_Framework_TestCase +{ + public function testEvaluateConditionString() + { + $existingClass = MessageJournal::class; + $factory = $this->getMockedFactory($existingClass); + $this->assertInstanceOf(DummyClient::class, $factory->find('type')); + } + + /** + * @expectedException \RuntimeException + */ + public function testEvaluateConditionInvalidString() + { + // String + $factory = $this->getMockedFactory('non_existent_class'); + $factory->find('type'); + } + + public function testEvaluateConditionCallableTrue() + { + $factory = $this->getMockedFactory( + function () { + return true; + } + ); + $this->assertInstanceOf(DummyClient::class, $factory->find('type')); + } + + /** + * @expectedException \RuntimeException + */ + public function testEvaluateConditionCallableFalse() + { + $factory = $this->getMockedFactory( + function () { + return false; + } + ); + + $factory->find('type'); + } + + public function testEvaluateConditionBooleanTrue() + { + $factory = $this->getMockedFactory(true); + $this->assertInstanceOf(DummyClient::class, $factory->find('type')); + } + + /** + * @expectedException \RuntimeException + */ + public function testEvaluateConditionBooleanFalse() + { + $factory = $this->getMockedFactory(false); + $factory->find('type'); + } + + public function testEvaluateConditionArrayTrue() + { + $factory = $this->getMockedFactory([true, true]); + $this->assertInstanceOf(DummyClient::class, $factory->find('type')); + } + + /** + * @expectedException \RuntimeException + */ + public function testEvaluateConditionArrayFalse() + { + $factory = $this->getMockedFactory([true, false, true]); + $factory->find('type'); + } + + public function testEvaluateConditionArrayAssoc() + { + $factory = $this->getMockedFactory(['test1' => true, true]); + $this->assertInstanceOf(DummyClient::class, $factory->find('type')); + } + + /** + * @expectedException \RuntimeException + */ + public function testEvaluateConditionObject() + { + $factory = $this->getMockedFactory(new \DateTime()); + $factory->find('type'); + } + + /** + * @param $condition + * + * @return HttplugFactory + */ + private function getMockedFactory($condition) + { + $discovery = $this->prophesize(Discovery::class); + $factory = new HttplugFactory($discovery->reveal()); + $binding = $this->prophesize(ClassBinding::class); + $discovery->findBindings('type', Expr::isInstanceOf('Puli\Discovery\Binding\ClassBinding'))->willReturn( + [$binding->reveal()] + ); + + $binding->hasParameterValue('depends')->willReturn(true); + $binding->getClassName()->willReturn(DummyClient::class); + $binding->getParameterValue('depends')->willReturn($condition); + + return $factory; + } +}