| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace ShlinkioTest\Shlink\Common\Mercure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Prophecy\PhpUnit\ProphecyTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Prophecy\Prophecy\ObjectProphecy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use ReflectionObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Shlinkio\Shlink\Common\Exception\MercureException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Shlinkio\Shlink\Common\Mercure\LcobucciJwtProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Shlinkio\Shlink\Common\Mercure\PublisherFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class PublisherFactoryTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     use ProphecyTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private PublisherFactory $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private ObjectProphecy $container; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function setUp(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $this->container = $this->prophesize(ContainerInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->factory = new PublisherFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @dataProvider provideInvalidConfigs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function throwsExceptionWhenNoHubUrlIsConfigured(array $config): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $getConfig = $this->container->get('config')->willReturn($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $getJwtProvider = $this->container->get(LcobucciJwtProvider::class)->willReturn(function (): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->expectException(MercureException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->expectExceptionMessage( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'You have to provide mercure hub URL under mercure.internal_hub_url or mercure.public_hub_url', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $getConfig->shouldBeCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $getJwtProvider->shouldNotBeCalled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         ($this->factory)($this->container->reveal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function provideInvalidConfigs(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         yield 'empty config' => [[]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         yield 'empty mercure' => [['mercure' => []]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         yield 'empty public url' => [['mercure' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'public_hub_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ]]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         yield 'empty internal url' => [['mercure' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'internal_hub_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         ]]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         yield 'both urls empty' => [['mercure' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'internal_hub_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'public_hub_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ]]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @test | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @dataProvider provideValidConfigs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function returnsExpectedObjectIfProperConfigIsFound(array $config, string $expectedHubUrl): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $getConfig = $this->container->get('config')->willReturn($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $getJwtProvider = $this->container->get(LcobucciJwtProvider::class)->willReturn(function (): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $publisher = ($this->factory)($this->container->reveal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $ref = new ReflectionObject($publisher); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $prop = $ref->getProperty('hubUrl'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $prop->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->assertEquals($expectedHubUrl . '/.well-known/mercure', $prop->getValue($publisher)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $getConfig->shouldHaveBeenCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $getJwtProvider->shouldHaveBeenCalledOnce(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function provideValidConfigs(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         yield 'with internal url' => [['mercure' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'internal_hub_url' => $url = 'http://foo.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ]], $url]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         yield 'with public url' => [['mercure' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'public_hub_url' => $url = 'http://bar.com', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         ]], $url]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  |