Completed
Push — master ( 4314eb...ec5974 )
by André
62:08 queued 47:43
created

InstantCachePurgerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 136
Duplicated Lines 24.26 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 33
loc 136
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 23 1
A testPurge() 0 12 1
B testPurgeForContent() 0 36 2
B getPermissionResolverMock() 24 24 1
A testPurgeAll() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the InstantCachePurgerTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\Cache\Tests\Http;
10
11
use eZ\Publish\API\Repository\ContentService;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\API\Repository\Values\User\UserReference;
14
use eZ\Publish\Core\Base\Tests\PHPUnit5CompatTrait;
15
use eZ\Publish\Core\MVC\Symfony\Cache\Http\InstantCachePurger;
16
use eZ\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface;
17
use eZ\Publish\Core\MVC\Symfony\Event\ContentCacheClearEvent;
18
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
19
use eZ\Publish\Core\Repository\Helper\LimitationService;
20
use eZ\Publish\Core\Repository\Helper\RoleDomainMapper;
21
use eZ\Publish\Core\Repository\Permission\PermissionResolver;
22
use eZ\Publish\Core\Repository\Repository;
23
use eZ\Publish\Core\Repository\Values\Content\Location;
24
use eZ\Publish\SPI\Persistence\User\Handler as UserHandler;
25
use PHPUnit\Framework\TestCase;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
use Symfony\Component\EventDispatcher\EventDispatcher;
28
29
class InstantCachePurgerTest extends TestCase
30
{
31
    use PHPUnit5CompatTrait;
32
33
    /**
34
     * @var \eZ\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface|\PHPUnit_Framework_MockObject_MockObject
35
     */
36
    protected $purgeClient;
37
38
    /**
39
     * @var \eZ\Publish\API\Repository\ContentService|\PHPUnit_Framework_MockObject_MockObject
40
     */
41
    protected $contentService;
42
43
    /**
44
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
45
     */
46
    protected $eventDispatcher;
47
48
    /**
49
     * @var \eZ\Publish\Core\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject
50
     */
51
    protected $repository;
52
53
    protected function setUp()
54
    {
55
        parent::setUp();
56
        $this->purgeClient = $this->createMock(PurgeClientInterface::class);
57
        $this->contentService = $this->createMock(ContentService::class);
58
        $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
59
60
        $this->repository = $this
61
            ->getMockBuilder(Repository::class)
62
            ->disableOriginalConstructor()
63
            ->setMethods(
64
                array_diff(
65
                    get_class_methods(Repository::class),
66
                    array('sudo')
67
                )
68
            )
69
            ->getMock();
70
71
        $this->repository
72
            ->expects($this->any())
73
            ->method('getPermissionResolver')
74
            ->will($this->returnValue($this->getPermissionResolverMock()));
75
    }
76
77
    public function testPurge()
78
    {
79
        $locationIds = array(123, 456, 789);
80
        $this->purgeClient
81
            ->expects($this->once())
82
            ->method('purge')
83
            ->with($locationIds)
84
            ->will($this->returnArgument(0));
85
86
        $purger = new InstantCachePurger($this->purgeClient, $this->contentService, $this->eventDispatcher, $this->repository);
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...Http\InstantCachePurger has been deprecated with message: since 6.8 will be removed in 7.0, use PurgeClient directly.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
87
        $this->assertSame($locationIds, $purger->purge($locationIds));
88
    }
89
90 View Code Duplication
    public function testPurgeAll()
91
    {
92
        $this->purgeClient
93
            ->expects($this->once())
94
            ->method('purgeAll');
95
96
        $purger = new InstantCachePurger($this->purgeClient, $this->contentService, $this->eventDispatcher, $this->repository);
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...Http\InstantCachePurger has been deprecated with message: since 6.8 will be removed in 7.0, use PurgeClient directly.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
97
        $purger->purgeAll();
98
    }
99
100
    public function testPurgeForContent()
101
    {
102
        $contentId = 123;
103
        $contentInfo = new ContentInfo(['id' => $contentId, 'published' => true]);
104
        // Assume listeners have added locations.
105
        // Adding duplicates on purpose.
106
        $locationIds = [123, 456, 789, 234, 567];
107
108
        $this->contentService
109
            ->expects($this->once())
110
            ->method('loadContentInfo')
111
            ->with($contentId)
112
            ->will($this->returnValue($contentInfo));
113
114
        $eventDispatcher = new EventDispatcher();
115
        $eventDispatcher->addListener(
116
            MVCEvents::CACHE_CLEAR_CONTENT,
117
            function (ContentCacheClearEvent $event) use ($locationIds) {
118
                foreach ($locationIds as $id) {
119
                    $event->addLocationToClear(new Location(['id' => $id]));
120
                }
121
122
                // Adding a few duplicates on purpose.
123
                $event->addLocationToClear(new Location(['id' => 123]));
124
                $event->addLocationToClear(new Location(['id' => 567]));
125
            }
126
        );
127
128
        $this->purgeClient
129
            ->expects($this->once())
130
            ->method('purge')
131
            ->with($locationIds);
132
133
        $purger = new InstantCachePurger($this->purgeClient, $this->contentService, $eventDispatcher, $this->repository);
0 ignored issues
show
Deprecated Code introduced by
The class eZ\Publish\Core\MVC\Symf...Http\InstantCachePurger has been deprecated with message: since 6.8 will be removed in 7.0, use PurgeClient directly.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
134
        $purger->purgeForContent($contentId);
135
    }
136
137
    /**
138
     * @return \eZ\Publish\Core\Repository\Permission\PermissionResolver|\PHPUnit_Framework_MockObject_MockObject
139
     */
140 View Code Duplication
    private function getPermissionResolverMock()
141
    {
142
        return $this
143
            ->getMockBuilder(PermissionResolver::class)
144
            ->setMethods(null)
145
            ->setConstructorArgs(
146
                [
147
                    $this
148
                        ->getMockBuilder(RoleDomainMapper::class)
149
                        ->disableOriginalConstructor()
150
                        ->getMock(),
151
                    $this
152
                        ->getMockBuilder(LimitationService::class)
153
                        ->getMock(),
154
                    $this
155
                        ->getMockBuilder(UserHandler::class)
156
                        ->getMock(),
157
                    $this
158
                        ->getMockBuilder(UserReference::class)
159
                        ->getMock(),
160
                ]
161
            )
162
            ->getMock();
163
    }
164
}
165