Completed
Push — http_cache_slots ( c4b669 )
by
unknown
13:55
created

AbstractSlotTest::getCachePurger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
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\SignalSlot;
10
11
use eZ\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface;
12
use PHPUnit_Framework_TestCase;
13
14
abstract class AbstractSlotTest extends PHPUnit_Framework_TestCase
15
{
16
    /** @var \eZ\Publish\Core\MVC\Symfony\Cache\Http\SignalSlot\AbstractSlot */
17
    protected $slot;
18
19
    /** @var \eZ\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface|\PHPUnit_Framework_MockObject_MockObject */
20
    protected $purgeClientMock;
21
22
    private $signal;
23
24
    public function setUp()
25
    {
26
        $this->purgeClientMock = $this->getMock(PurgeClientInterface::class);
27
        $this->slot = $this->createSlot();
28
        $this->signal = $this->createSignal();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eZ\Publish\Core\MVC\Symf...alSlot\AbstractSlotTest as the method createSignal() does only exist in the following sub-classes of eZ\Publish\Core\MVC\Symf...alSlot\AbstractSlotTest: eZ\Publish\Core\MVC\Symf...t\AssignSectionSlotTest, eZ\Publish\Core\MVC\Symf...UserToUserGroupSlotTest, eZ\Publish\Core\MVC\Symf...lot\CopyContentSlotTest, eZ\Publish\Core\MVC\Symf...\CreateLocationSlotTest, eZ\Publish\Core\MVC\Symf...t\DeleteContentSlotTest, eZ\Publish\Core\MVC\Symf...\DeleteLocationSlotTest, eZ\Publish\Core\MVC\Symf...t\DeleteVersionSlotTest, eZ\Publish\Core\MVC\Symf...ot\HideLocationSlotTest, eZ\Publish\Core\MVC\Symf...lot\MoveSubtreeSlotTest, eZ\Publish\Core\MVC\Symf...\PublishVersionSlotTest, eZ\Publish\Core\MVC\Symf...nalSlot\RecoverSlotTest, eZ\Publish\Core\MVC\Symf...SetContentStateSlotTest, eZ\Publish\Core\MVC\Symf...ot\SwapLocationSlotTest, eZ\Publish\Core\MVC\Symf...ignalSlot\TrashSlotTest, eZ\Publish\Core\MVC\Symf...erFromUserGroupSlotTest, eZ\Publish\Core\MVC\Symf...\UnhideLocationSlotTest, eZ\Publish\Core\MVC\Symf...\UpdateLocationSlotTest, eZ\Publish\Core\MVC\Symf...Slot\UpdateUserSlotTest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
29
    }
30
31
    protected function createSlot()
32
    {
33
        $class = $this->getSlotClass();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eZ\Publish\Core\MVC\Symf...alSlot\AbstractSlotTest as the method getSlotClass() does only exist in the following sub-classes of eZ\Publish\Core\MVC\Symf...alSlot\AbstractSlotTest: eZ\Publish\Core\MVC\Symf...t\AssignSectionSlotTest, eZ\Publish\Core\MVC\Symf...UserToUserGroupSlotTest, eZ\Publish\Core\MVC\Symf...lot\CopyContentSlotTest, eZ\Publish\Core\MVC\Symf...\CreateLocationSlotTest, eZ\Publish\Core\MVC\Symf...t\DeleteContentSlotTest, eZ\Publish\Core\MVC\Symf...\DeleteLocationSlotTest, eZ\Publish\Core\MVC\Symf...t\DeleteVersionSlotTest, eZ\Publish\Core\MVC\Symf...ot\HideLocationSlotTest, eZ\Publish\Core\MVC\Symf...lot\MoveSubtreeSlotTest, eZ\Publish\Core\MVC\Symf...\PublishVersionSlotTest, eZ\Publish\Core\MVC\Symf...nalSlot\RecoverSlotTest, eZ\Publish\Core\MVC\Symf...SetContentStateSlotTest, eZ\Publish\Core\MVC\Symf...ot\SwapLocationSlotTest, eZ\Publish\Core\MVC\Symf...ignalSlot\TrashSlotTest, eZ\Publish\Core\MVC\Symf...erFromUserGroupSlotTest, eZ\Publish\Core\MVC\Symf...\UnhideLocationSlotTest, eZ\Publish\Core\MVC\Symf...\UpdateLocationSlotTest, eZ\Publish\Core\MVC\Symf...Slot\UpdateUserSlotTest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
34
35
        return new $class($this->purgeClientMock);
36
    }
37
38
    /**
39
     * @dataProvider getUnreceivedSignals
40
     */
41
    public function testDoesNotReceiveOtherSignals($signal)
42
    {
43
        $this->purgeClientMock->expects($this->never())->method('purge');
44
        $this->purgeClientMock->expects($this->never())->method('purgeAll');
45
46
        $this->slot->receive($signal);
47
    }
48
49
    /**
50
     * @dataProvider getReceivedSignals
51
     */
52
    public function testReceivePurgesCacheForTags($signal)
53
    {
54
        $this->purgeClientMock->expects($this->once())->method('purge')->with($this->generateTags());
55
        $this->purgeClientMock->expects($this->never())->method('purgeAll');
56
        $this->receive($signal);
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    abstract public function generateTags();
63
64
    protected function receive($signal)
65
    {
66
        $this->slot->receive($signal);
67
    }
68
69
    public function getReceivedSignals()
70
    {
71
        return [[$this->createSignal()]];
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eZ\Publish\Core\MVC\Symf...alSlot\AbstractSlotTest as the method createSignal() does only exist in the following sub-classes of eZ\Publish\Core\MVC\Symf...alSlot\AbstractSlotTest: eZ\Publish\Core\MVC\Symf...t\AssignSectionSlotTest, eZ\Publish\Core\MVC\Symf...UserToUserGroupSlotTest, eZ\Publish\Core\MVC\Symf...lot\CopyContentSlotTest, eZ\Publish\Core\MVC\Symf...\CreateLocationSlotTest, eZ\Publish\Core\MVC\Symf...t\DeleteContentSlotTest, eZ\Publish\Core\MVC\Symf...\DeleteLocationSlotTest, eZ\Publish\Core\MVC\Symf...t\DeleteVersionSlotTest, eZ\Publish\Core\MVC\Symf...ot\HideLocationSlotTest, eZ\Publish\Core\MVC\Symf...lot\MoveSubtreeSlotTest, eZ\Publish\Core\MVC\Symf...\PublishVersionSlotTest, eZ\Publish\Core\MVC\Symf...nalSlot\RecoverSlotTest, eZ\Publish\Core\MVC\Symf...SetContentStateSlotTest, eZ\Publish\Core\MVC\Symf...ot\SwapLocationSlotTest, eZ\Publish\Core\MVC\Symf...ignalSlot\TrashSlotTest, eZ\Publish\Core\MVC\Symf...erFromUserGroupSlotTest, eZ\Publish\Core\MVC\Symf...\UnhideLocationSlotTest, eZ\Publish\Core\MVC\Symf...\UpdateLocationSlotTest, eZ\Publish\Core\MVC\Symf...Slot\UpdateUserSlotTest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
72
    }
73
74
    /**
75
     * All existing SignalSlots.
76
     */
77
    public function getUnreceivedSignals()
78
    {
79
        $arguments = [];
80
81
        if (empty($arguments)) {
82
            $signals = $this->getAllSignals();
83
84
            foreach ($signals as $signalClass) {
85
                if (in_array($signalClass, $this->getReceivedSignalClasses())) {
0 ignored issues
show
Bug introduced by
The method getReceivedSignalClasses() does not exist on eZ\Publish\Core\MVC\Symf...alSlot\AbstractSlotTest. Did you maybe mean receive()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
86
                    continue;
87
                }
88
                $arguments[] = [new $signalClass()];
89
            }
90
        }
91
92
        return $arguments;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    private function getAllSignals()
99
    {
100
        return array(
101
            'eZ\Publish\Core\SignalSlot\Signal\URLAliasService\CreateUrlAliasSignal',
102
            'eZ\Publish\Core\SignalSlot\Signal\URLAliasService\RemoveAliasesSignal',
103
            'eZ\Publish\Core\SignalSlot\Signal\URLAliasService\CreateGlobalUrlAliasSignal',
104
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\CreateContentTypeSignal',
105
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\AddFieldDefinitionSignal',
106
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\CopyContentTypeSignal',
107
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\DeleteContentTypeSignal',
108
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\UpdateContentTypeGroupSignal',
109
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\DeleteContentTypeGroupSignal',
110
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\UnassignContentTypeGroupSignal',
111
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\PublishContentTypeDraftSignal',
112
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\AssignContentTypeGroupSignal',
113
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\UpdateFieldDefinitionSignal',
114
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\UpdateContentTypeDraftSignal',
115
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\RemoveFieldDefinitionSignal',
116
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\CreateContentTypeDraftSignal',
117
            'eZ\Publish\Core\SignalSlot\Signal\ContentTypeService\CreateContentTypeGroupSignal',
118
            'eZ\Publish\Core\SignalSlot\Signal\LanguageService\EnableLanguageSignal',
119
            'eZ\Publish\Core\SignalSlot\Signal\LanguageService\UpdateLanguageNameSignal',
120
            'eZ\Publish\Core\SignalSlot\Signal\LanguageService\CreateLanguageSignal',
121
            'eZ\Publish\Core\SignalSlot\Signal\LanguageService\DisableLanguageSignal',
122
            'eZ\Publish\Core\SignalSlot\Signal\LanguageService\DeleteLanguageSignal',
123
            'eZ\Publish\Core\SignalSlot\Signal\UserService\MoveUserGroupSignal',
124
            'eZ\Publish\Core\SignalSlot\Signal\UserService\DeleteUserGroupSignal',
125
            'eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserGroupSignal',
126
            'eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserGroupSignal',
127
            'eZ\Publish\Core\SignalSlot\Signal\UserService\UnAssignUserFromUserGroupSignal',
128
            'eZ\Publish\Core\SignalSlot\Signal\UserService\AssignUserToUserGroupSignal',
129
            'eZ\Publish\Core\SignalSlot\Signal\UserService\DeleteUserSignal',
130
            'eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserSignal',
131
            'eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserSignal',
132
            'eZ\Publish\Core\SignalSlot\Signal\SectionService\DeleteSectionSignal',
133
            'eZ\Publish\Core\SignalSlot\Signal\SectionService\CreateSectionSignal',
134
            'eZ\Publish\Core\SignalSlot\Signal\SectionService\UpdateSectionSignal',
135
            'eZ\Publish\Core\SignalSlot\Signal\SectionService\AssignSectionSignal',
136
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\AssignRoleToUserGroupSignal',
137
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\UpdatePolicySignal',
138
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\CreateRoleSignal',
139
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\RemovePolicySignal',
140
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\UnassignRoleFromUserSignal',
141
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\AddPolicySignal',
142
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\UnassignRoleFromUserGroupSignal',
143
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\UpdateRoleSignal',
144
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\AssignRoleToUserSignal',
145
            'eZ\Publish\Core\SignalSlot\Signal\RoleService\DeleteRoleSignal',
146
            'eZ\Publish\Core\SignalSlot\Signal\TrashService\TrashSignal',
147
            'eZ\Publish\Core\SignalSlot\Signal\TrashService\EmptyTrashSignal',
148
            'eZ\Publish\Core\SignalSlot\Signal\TrashService\RecoverSignal',
149
            'eZ\Publish\Core\SignalSlot\Signal\TrashService\DeleteTrashItemSignal',
150
            'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\DeleteObjectStateSignal',
151
            'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\CreateObjectStateSignal',
152
            'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\DeleteObjectStateGroupSignal',
153
            'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\CreateObjectStateGroupSignal',
154
            'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\UpdateObjectStateSignal',
155
            'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\UpdateObjectStateGroupSignal',
156
            'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\SetContentStateSignal',
157
            'eZ\Publish\Core\SignalSlot\Signal\ObjectStateService\SetPriorityOfObjectStateSignal',
158
            'eZ\Publish\Core\SignalSlot\Signal\URLWildcardService\TranslateSignal',
159
            'eZ\Publish\Core\SignalSlot\Signal\URLWildcardService\RemoveSignal',
160
            'eZ\Publish\Core\SignalSlot\Signal\URLWildcardService\CreateSignal',
161
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\UpdateContentSignal',
162
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\CreateContentDraftSignal',
163
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\AddRelationSignal',
164
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\CreateContentSignal',
165
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\DeleteContentSignal',
166
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\AddTranslationInfoSignal',
167
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\CopyContentSignal',
168
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\UpdateContentMetadataSignal',
169
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\TranslateVersionSignal',
170
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\PublishVersionSignal',
171
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\DeleteRelationSignal',
172
            'eZ\Publish\Core\SignalSlot\Signal\ContentService\DeleteVersionSignal',
173
            'eZ\Publish\Core\SignalSlot\Signal\LocationService\UpdateLocationSignal',
174
            'eZ\Publish\Core\SignalSlot\Signal\LocationService\HideLocationSignal',
175
            'eZ\Publish\Core\SignalSlot\Signal\LocationService\SwapLocationSignal',
176
            'eZ\Publish\Core\SignalSlot\Signal\LocationService\MoveSubtreeSignal',
177
            'eZ\Publish\Core\SignalSlot\Signal\LocationService\UnhideLocationSignal',
178
            'eZ\Publish\Core\SignalSlot\Signal\LocationService\CreateLocationSignal',
179
            'eZ\Publish\Core\SignalSlot\Signal\LocationService\DeleteLocationSignal',
180
            'eZ\Publish\Core\SignalSlot\Signal\LocationService\CopySubtreeSignal',
181
        );
182
    }
183
}
184