| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Sonata Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Thomas Rabaix <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Sonata\CacheBundle\Tests\Adapter\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Sonata\CacheBundle\Adapter\VarnishCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | class VarnishCacheTest extends \PHPUnit_Framework_TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function testInitCache() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $router->expects($this->any())->method('generate')->will($this->returnValue('https://sonata-project.org/cache/esi/TOKEN?controller=asdsad')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $cache = new VarnishCache('token', array(), $router, 'ban', $resolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->assertTrue($cache->flush(array())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->assertTrue($cache->flushAll()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $cacheElement = $cache->set(array('id' => 7), 'data'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->assertInstanceOf('Sonata\Cache\CacheElement', $cacheElement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->assertTrue($cache->has(array('id' => 7))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $cacheElement = $cache->get(array('id' => 7, 'controller' => 'foo.service::runAction', 'parameters' => array())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->assertInstanceOf('Sonata\Cache\CacheElement', $cacheElement); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->assertEquals('<esi:include src="https://sonata-project.org/cache/esi/TOKEN?controller=asdsad"/>', $cacheElement->getData()->getContent()); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function testActionInvalidToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $router->expects($this->any())->method('generate')->will($this->returnValue('http://sonata-project.orf/cache/esi/TOKEN?controller=asdsad')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $request = Request::create('cache/esi/TOKEN?controller=asdsad', 'get', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             'token' => 'wrong', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $cache = new VarnishCache('token', array(), $router, 'ban', $resolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $cache->cacheAction($request); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function testValidToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $resolver->expects($this->any())->method('getController')->will($this->returnValue(function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return new Response(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $resolver->expects($this->any())->method('getArguments')->will($this->returnValue(array())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $request = Request::create('cache/esi/TOKEN', 'get', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             'token' => '44befdbd93f304ea693023aa6587729bed76a206ecdacfd9bbd9b43fcf2e1664', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             'parameters' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 'controller' => 'asfsat', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 'parameters' => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $cache = new VarnishCache('token', array(), $router, 'ban', $resolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $cache->cacheAction($request); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |