| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Shoot\Shoot\Tests\Unit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PHPUnit\Framework\MockObject\MockObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Shoot\Shoot\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Shoot\Shoot\MiddlewareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Shoot\Shoot\Pipeline; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Shoot\Shoot\PresentationModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Shoot\Shoot\Tests\Fixtures\ViewFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Twig_Filter as TwigFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Twig_Test as TwigTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | final class ExtensionTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public function testExtensionShouldDelegateProcessingToPipeline() | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         /** @var ServerRequestInterface|MockObject $request */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $request = $this->createMock(ServerRequestInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $view = ViewFactory::create(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $middleware = $this->createMock(MiddlewareInterface::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $middleware | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             ->expects($this->once()) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             ->method('process') | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             ->with($this->equalTo($view)) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             ->willReturn($view); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $pipeline = new Pipeline([$middleware]); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $extension = new Extension($pipeline); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $pipeline->withRequest($request, function () use ($extension, $view) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             $extension->process($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function testExtensionShouldIncludeVariablesFilter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $extension = new Extension(new Pipeline()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $presentationModel = new PresentationModel(['variable' => 'variable']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         /** @var TwigFilter[] $filters */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $filters = array_filter($extension->getFilters(), function (TwigFilter $filter): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             return $filter->getName() === 'variables'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->assertCount(1, $filters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         list($filter) = $filters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $callback = $filter->getCallable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         /** @var mixed[] $variables */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $variables = $callback($presentationModel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->assertInternalType('array', $variables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertCount(1, $variables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertArrayHasKey('variable', $variables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function testExtensionShouldIncludeModelTest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $extension = new Extension(new Pipeline()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $presentationModel = new PresentationModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         /** @var TwigTest[] $tests */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $tests = array_filter($extension->getTests(), function (TwigTest $test): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             return $test->getName() === 'model'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->assertCount(1, $tests); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         list($test) = $tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $callback = $test->getCallable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertTrue($callback($presentationModel)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |