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

getPermissionResolverMock()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
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