Completed
Push — master ( 9de1be...100987 )
by David
8s
created

CacheManagerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 90
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testTagResponse() 0 22 1
B testInvalidateRoute() 0 26 1
B testRefreshRoute() 0 27 1
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\HttpCacheBundle\Tests\Unit;
13
14
use FOS\HttpCacheBundle\CacheManager;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
18
class CacheManagerTest extends \PHPUnit_Framework_TestCase
19
{
20
    protected $proxyClient;
21
22
    public function setUp()
23
    {
24
        $this->proxyClient = \Mockery::mock('\FOS\HttpCache\ProxyClient\ProxyClientInterface');
25
    }
26
27
    public function testInvalidateRoute()
28
    {
29
        $httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface')
30
            ->shouldReceive('purge')->once()->with('/my/route', array())
31
            ->shouldReceive('purge')->once()->with('/route/with/params/id/123', array())
32
            ->shouldReceive('purge')->once()->with('/route/with/params/id/123', array('X-Foo' => 'bar'))
33
            ->shouldReceive('flush')->once()
34
            ->getMock();
35
36
        $router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface')
37
            ->shouldReceive('generate')
38
            ->with('my_route', array(), UrlGeneratorInterface::ABSOLUTE_PATH)
39
            ->andReturn('/my/route')
40
41
            ->shouldReceive('generate')
42
            ->with('route_with_params', array('id' => 123), UrlGeneratorInterface::ABSOLUTE_PATH)
43
            ->andReturn('/route/with/params/id/123')
44
            ->getMock();
45
46
        $cacheManager = new CacheManager($httpCache, $router);
47
48
        $cacheManager->invalidateRoute('my_route')
49
            ->invalidateRoute('route_with_params', array('id' => 123))
50
            ->invalidateRoute('route_with_params', array('id' => 123), array('X-Foo' => 'bar'))
51
            ->flush();
52
    }
53
54
    public function testRefreshRoute()
55
    {
56
        $httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface')
57
            ->shouldReceive('refresh')->once()->with('/my/route', null)
58
            ->shouldReceive('refresh')->once()->with('/route/with/params/id/123', null)
59
            ->shouldReceive('refresh')->once()->with('/route/with/params/id/123', array('X-Foo' => 'bar'))
60
            ->shouldReceive('flush')->never()
61
            ->getMock();
62
63
        $router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface')
64
            ->shouldReceive('generate')
65
            ->with('my_route', array(), UrlGeneratorInterface::ABSOLUTE_PATH)
66
            ->andReturn('/my/route')
67
68
            ->shouldReceive('generate')
69
            ->with('route_with_params', array('id' => 123), UrlGeneratorInterface::ABSOLUTE_PATH)
70
            ->andReturn('/route/with/params/id/123')
71
            ->getMock();
72
73
        $cacheManager = new CacheManager($httpCache, $router);
74
75
        $cacheManager
76
            ->refreshRoute('my_route')
77
            ->refreshRoute('route_with_params', array('id' => 123))
78
            ->refreshRoute('route_with_params', array('id' => 123), array('X-Foo' => 'bar'))
79
        ;
80
    }
81
82
    /**
83
     * @group legacy
84
     */
85
    public function testTagResponse()
86
    {
87
        $ban = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\BanInterface');
88
        $router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface');
89
90
        $tags1 = array('post-1', 'posts');
91
        $tags2 = array('post-2');
92
        $tags3 = array('different');
93
94
        $cacheManager = new CacheManager($ban, $router);
95
        $response = new Response();
96
        $response->headers->set($cacheManager->getTagsHeader(), '');
0 ignored issues
show
Deprecated Code introduced by
The method FOS\HttpCache\CacheInvalidator::getTagsHeader() has been deprecated with message: Use TagHandler::getTagsHeaderName instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
97
        $cacheManager->tagResponse($response, $tags1);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\HttpCacheBundle\CacheManager::tagResponse() has been deprecated with message: Add tags with TagHandler::addTags and then use TagHandler::tagResponse

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
98
        $this->assertTrue($response->headers->has($cacheManager->getTagsHeader()));
0 ignored issues
show
Deprecated Code introduced by
The method FOS\HttpCache\CacheInvalidator::getTagsHeader() has been deprecated with message: Use TagHandler::getTagsHeaderName instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
99
        $this->assertEquals(implode(',', $tags1), $response->headers->get($cacheManager->getTagsHeader()));
0 ignored issues
show
Deprecated Code introduced by
The method FOS\HttpCache\CacheInvalidator::getTagsHeader() has been deprecated with message: Use TagHandler::getTagsHeaderName instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
101
        $cacheManager->tagResponse($response, $tags2);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\HttpCacheBundle\CacheManager::tagResponse() has been deprecated with message: Add tags with TagHandler::addTags and then use TagHandler::tagResponse

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
102
        $this->assertEquals(implode(',', array_merge($tags1, $tags2)), $response->headers->get($cacheManager->getTagsHeader()));
0 ignored issues
show
Deprecated Code introduced by
The method FOS\HttpCache\CacheInvalidator::getTagsHeader() has been deprecated with message: Use TagHandler::getTagsHeaderName instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
103
104
        $cacheManager->tagResponse($response, $tags3, true);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\HttpCacheBundle\CacheManager::tagResponse() has been deprecated with message: Add tags with TagHandler::addTags and then use TagHandler::tagResponse

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
105
        $this->assertEquals(implode(',', $tags3), $response->headers->get($cacheManager->getTagsHeader()));
0 ignored issues
show
Deprecated Code introduced by
The method FOS\HttpCache\CacheInvalidator::getTagsHeader() has been deprecated with message: Use TagHandler::getTagsHeaderName instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
106
    }
107
}
108