|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Webfactory\ShortcodeBundle\Tests\DependencyInjection\Compiler; |
| 4 | + |
| 5 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 6 | +use Symfony\Component\DependencyInjection\Definition; |
| 7 | +use Symfony\Component\DependencyInjection\Reference; |
| 8 | +use Symfony\Component\DependencyInjection\TaggedContainerInterface; |
| 9 | +use Thunder\Shortcode\ShortcodeFacade; |
| 10 | +use Webfactory\ShortcodeBundle\DependencyInjection\Compiler\ShortcodeCompilerPass; |
| 11 | + |
| 12 | +final class ShortcodeCompilerPassTest extends \PHPUnit_Framework_TestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * System under test. |
| 16 | + * |
| 17 | + * @var ShortcodeCompilerPass |
| 18 | + */ |
| 19 | + private $compilerPass; |
| 20 | + |
| 21 | + /** @var ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject */ |
| 22 | + private $containerBuilder; |
| 23 | + |
| 24 | + protected function setUp() |
| 25 | + { |
| 26 | + $this->compilerPass = new ShortcodeCompilerPass(); |
| 27 | + $this->containerBuilder = $this->getMockBuilder(ContainerBuilder::class) |
| 28 | + ->disableOriginalConstructor() |
| 29 | + ->getMock(); |
| 30 | + } |
| 31 | + |
| 32 | + /** @test */ |
| 33 | + public function tagged_services_are_added_as_handlers_to_facade() |
| 34 | + { |
| 35 | + $this->containerBuilder->expects($this->once()) |
| 36 | + ->method('findTaggedServiceIds') |
| 37 | + ->willReturn([ |
| 38 | + 'service_id1' => [ |
| 39 | + ['shortcode' => 'shortcode1'] |
| 40 | + ], |
| 41 | + 'service_id2' => [ |
| 42 | + ['shortcode' => 'shortcode2'] |
| 43 | + ] |
| 44 | + ]); |
| 45 | + |
| 46 | + $mockedShortcodeFacade = $this->getMock(Definition::class); |
| 47 | + $mockedShortcodeFacade->expects($this->at(0)) |
| 48 | + ->method('addMethodCall') |
| 49 | + ->with('addHandler', $this->callback(function (array $argument) { |
| 50 | + return $argument[0] === 'shortcode1' |
| 51 | + && $argument[1] instanceof Reference; |
| 52 | + })); |
| 53 | + $mockedShortcodeFacade->expects($this->at(1)) |
| 54 | + ->method('addMethodCall') |
| 55 | + ->with('addHandler', $this->callback(function ($argument) { |
| 56 | + return $argument[0] === 'shortcode2' |
| 57 | + && $argument[1] instanceof Reference; |
| 58 | + })); |
| 59 | + |
| 60 | + $this->containerBuilder->expects($this->once()) |
| 61 | + ->method('findDefinition') |
| 62 | + ->with('webfactory.shortcode.facade') |
| 63 | + ->willReturn($mockedShortcodeFacade); |
| 64 | + |
| 65 | + $this->compilerPass->process($this->containerBuilder); |
| 66 | + } |
| 67 | + |
| 68 | + /** @test */ |
| 69 | + public function no_tagged_services_do_no_harm() |
| 70 | + { |
| 71 | + $this->containerBuilder->expects($this->once()) |
| 72 | + ->method('findTaggedServiceIds') |
| 73 | + ->willReturn([]); |
| 74 | + |
| 75 | + $this->setExpectedException(null); |
| 76 | + |
| 77 | + $this->compilerPass->process($this->containerBuilder); |
| 78 | + } |
| 79 | + |
| 80 | + /** @test */ |
| 81 | + public function shortcode_guide_service_gets_configured_if_set() |
| 82 | + { |
| 83 | + $this->containerBuilder->expects($this->once()) |
| 84 | + ->method('findTaggedServiceIds') |
| 85 | + ->willReturn([ |
| 86 | + 'service_id1' => [ |
| 87 | + ['shortcode' => 'shortcode1'] |
| 88 | + ], |
| 89 | + 'service_id2' => [ |
| 90 | + ['shortcode' => 'shortcode2'] |
| 91 | + ] |
| 92 | + ]); |
| 93 | + |
| 94 | + $this->containerBuilder->expects($this->once()) |
| 95 | + ->method('findDefinition') |
| 96 | + ->with('webfactory.shortcode.facade') |
| 97 | + ->willReturn($this->getMock(Definition::class)); |
| 98 | + |
| 99 | + $this->containerBuilder->expects($this->once()) |
| 100 | + ->method('has') |
| 101 | + ->with('webfactory.shortcode.guide.controller') |
| 102 | + ->willReturn(true); |
| 103 | + |
| 104 | + $mockedShortcodeGuideServiceDefinition = $this->getMock(Definition::class); |
| 105 | + $mockedShortcodeGuideServiceDefinition->expects($this->once()) |
| 106 | + ->method('setArgument') |
| 107 | + ->with( |
| 108 | + 0, |
| 109 | + [ |
| 110 | + ['shortcode' => 'shortcode1'], |
| 111 | + ['shortcode' => 'shortcode2'] |
| 112 | + ] |
| 113 | + ); |
| 114 | + |
| 115 | + $this->containerBuilder->expects($this->once()) |
| 116 | + ->method('getDefinition') |
| 117 | + ->with('webfactory.shortcode.guide.controller') |
| 118 | + ->willReturn($mockedShortcodeGuideServiceDefinition); |
| 119 | + |
| 120 | + $this->compilerPass->process($this->containerBuilder); |
| 121 | + } |
| 122 | + |
| 123 | + /** @test */ |
| 124 | + public function missing_shortcode_guide_service_does_no_harm() |
| 125 | + { |
| 126 | + $this->containerBuilder->expects($this->once()) |
| 127 | + ->method('findTaggedServiceIds') |
| 128 | + ->willReturn([]); |
| 129 | + |
| 130 | + $this->containerBuilder->expects($this->once()) |
| 131 | + ->method('has') |
| 132 | + ->with('webfactory.shortcode.guide.controller') |
| 133 | + ->willReturn(false); |
| 134 | + |
| 135 | + $this->setExpectedException(null); |
| 136 | + |
| 137 | + $this->compilerPass->process($this->containerBuilder); |
| 138 | + } |
| 139 | +} |
0 commit comments