|
| 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