Completed
Push — master ( 0e2bb1...d48330 )
by WEBEWEB
02:01
created

AbstractTestCase::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 8.4362
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the core-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\CoreBundle\Tests;
13
14
use Closure;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Doctrine\ORM\EntityManagerInterface;
17
use PHPUnit\Framework\TestCase;
18
use Psr\Log\LoggerInterface;
19
use Swift_Mailer;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
24
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
25
use Symfony\Component\HttpFoundation\Session\SessionInterface;
26
use Symfony\Component\HttpKernel\Kernel;
27
use Symfony\Component\HttpKernel\KernelInterface;
28
use Symfony\Component\Routing\RouterInterface;
29
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
30
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
31
use Symfony\Component\Security\Core\User\UserInterface;
32
use Twig\Environment;
33
use Twig\Loader\LoaderInterface;
34
use WBW\Bundle\CoreBundle\Component\EventDispatcher\BaseEvent;
35
use WBW\Bundle\CoreBundle\Component\Translation\BaseTranslatorInterface;
36
37
/**
38
 * Abstract test case.
39
 *
40
 * @author webeweb <https://github.com/webeweb/>
41
 * @package WBW\Bundle\CoreBundle\Tests
42
 * @abstract
43
 */
44
abstract class AbstractTestCase extends TestCase {
45
46
    /**
47
     * Container builder.
48
     *
49
     * @var ContainerBuilder
50
     */
51
    protected $containerBuilder;
52
53
    /**
54
     * Entity manager.
55
     *
56
     * @var EntityManagerInterface;
57
     */
58
    protected $entityManager;
59
60
    /**
61
     * Event dispatcher.
62
     *
63
     * @var EventDispatcherInterface
64
     */
65
    protected $eventDispatcher;
66
67
    /**
68
     * Flash bag.
69
     *
70
     * @var FlashBagInterface
71
     */
72
    protected $flashBag;
73
74
    /**
75
     * Kernel.
76
     *
77
     * @var KernelInterface
78
     */
79
    protected $kernel;
80
81
    /**
82
     * Logger.
83
     *
84
     * @var LoggerInterface
85
     */
86
    protected $logger;
87
88
    /**
89
     * Object manager.
90
     *
91
     * @var ObjectManager
92
     */
93
    protected $objectManager;
94
95
    /**
96
     * Router.
97
     *
98
     * @var RouterInterface
99
     */
100
    protected $router;
101
102
    /**
103
     * Session.
104
     *
105
     * @var SessionInterface
106
     */
107
    protected $session;
108
109
    /**
110
     * Session bag.
111
     *
112
     * @var SessionBagInterface
113
     */
114
    protected $sessionBag;
115
116
    /**
117
     * Swift mailer.
118
     *
119
     * @var Swift_Mailer
120
     */
121
    protected $swiftMailer;
122
123
    /**
124
     * Token
125
     *
126
     * @var TokenInterface
127
     */
128
    protected $token;
129
130
    /**
131
     * Token storage.
132
     *
133
     * @var TokenStorageInterface
134
     */
135
    protected $tokenStorage;
136
137
    /**
138
     * Translator.
139
     *
140
     * @var BaseTranslatorInterface
141
     */
142
    protected $translator;
143
144
    /**
145
     * Twig environment.
146
     *
147
     * @var Environment
148
     */
149
    protected $twigEnvironment;
150
151
    /**
152
     * Twig globals.
153
     *
154
     * @var array
155
     */
156
    private $twigGlobals = [];
157
158
    /**
159
     * Twig loader.
160
     *
161
     * @var LoaderInterface
162
     */
163
    protected $twigLoader;
164
165
    /**
166
     * User.
167
     *
168
     * @var UserInterface
169
     */
170
    protected $user;
171
172
    /**
173
     * Get the dispatch() method for an EventDispatcher.
174
     *
175
     * @return Closure Returns the dispatch() method for an EventDispatcher.
176
     */
177
    public static function getEventDispatcherDispatchFunction(): Closure {
178
179
        $dispatchFunction = function(BaseEvent $event, $eventName) {
0 ignored issues
show
Unused Code introduced by
The parameter $eventName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
            return $event;
181
        };
182
183
        if (Kernel::VERSION_ID < 40300) {
184
            $dispatchFunction = function($eventName, BaseEvent $event) {
185
                return $event;
186
            };
187
        }
188
189
        return $dispatchFunction;
190
    }
191
192
    /**
193
     * {@inheritDoc}
194
     */
195
    protected function setUp(): void {
196
        parent::setUp();
197
198
        // Set an Entity manager mock.
199
        $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
200
201
        // Set an Event dispatcher mock.
202
        $this->eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
203
204
        // Set a Flash bag mock.
205
        $this->flashBag = $this->getMockBuilder(FlashBagInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...lash\FlashBagInterface> of property $flashBag.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
206
207
        // Set a Kernel mock.
208
        $this->kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...Kernel\KernelInterface> of property $kernel.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
209
210
        // Set a Logger mock.
211
        $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\P...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Psr\Log\LoggerInterface> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
212
213
        // Set an Object manager mock.
214
        $this->objectManager = $this->getMockBuilder(ObjectManager::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\D...ager::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\Common\Persistence\ObjectManager> of property $objectManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
215
216
        // Set a Router mock.
217
        $this->router = $this->getMockBuilder(RouterInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component\Routing\RouterInterface> of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
218
219
        // Set a Session mock.
220
        $this->session = $this->getMockBuilder(SessionInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...ssion\SessionInterface> of property $session.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
221
222
        // Set a Session bag mock.
223
        $this->sessionBag = $this->getMockBuilder(SessionBagInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...on\SessionBagInterface> of property $sessionBag.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
224
225
        // Set a Swift mailer mock.
226
        $this->swiftMailer = $this->getMockBuilder(Swift_Mailer::class)->disableOriginalConstructor()->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Swift_Mailer> of property $swiftMailer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
227
228
        // Set a Translator mock.
229
        $this->translator = $this->getMockBuilder(BaseTranslatorInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\W...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<WBW\Bundle\CoreBu...aseTranslatorInterface> of property $translator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
230
        $this->translator->expects($this->any())->method("trans")->willReturnCallback(function($id, array $parameters = [], $domain = null, $locale = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $domain is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $locale is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
231
            return $id;
232
        });
233
234
        // Set a Token mock.
235
        $this->token = $this->getMockBuilder(TokenInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...n\Token\TokenInterface> of property $token.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
236
        $this->token->expects($this->any())->method("getUser")->willReturnCallback(function() {
237
            return $this->user;
238
        });
239
240
        // Set a Token storage mock.
241
        $this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\S...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...\TokenStorageInterface> of property $tokenStorage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
242
        $this->tokenStorage->expects($this->any())->method("getToken")->willReturn($this->token);
243
244
        // Set a Twig loader mock.
245
        $this->twigLoader = $this->getMockBuilder(LoaderInterface::class)->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\T...face::class)->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Twig\Loader\LoaderInterface> of property $twigLoader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
246
247
        // Set a Twig environment mock.
248
        $this->twigEnvironment = $this->getMockBuilder(Environment::class)->setConstructorArgs([$this->twigLoader, []])->getMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(\T...r, array()))->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Twig\Environment> of property $twigEnvironment.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
249
        $this->twigEnvironment->expects($this->any())->method("addGlobal")->willReturnCallback(function($name, $value) {
250
            $this->twigGlobals[$name] = $value;
251
        });
252
        $this->twigEnvironment->expects($this->any())->method("getGlobals")->willReturnCallback(function() {
253
            return $this->twigGlobals;
254
        });
255
256
        // Set a Parameter bag mock.
257
        $parameterBag = new ParameterBag([
258
            "kernel.environment" => "test",
259
            "kernel.root_dir"    => getcwd() . "/Fixtures/app",
260
        ]);
261
262
        // We set a container builder with only the necessary.
263
        $this->containerBuilder = new ContainerBuilder($parameterBag);
264
        $this->containerBuilder->set("doctrine.orm.entity_manager", $this->entityManager);
265
        $this->containerBuilder->set("event_dispatcher", $this->eventDispatcher);
266
        $this->containerBuilder->set("kernel", $this->kernel);
267
        $this->containerBuilder->set("logger", $this->logger);
268
        $this->containerBuilder->set("router", $this->router);
269
        $this->containerBuilder->set("session", $this->session);
270
        $this->containerBuilder->set("security.token_storage", $this->tokenStorage);
271
        $this->containerBuilder->set("swiftmailer.mailer", $this->swiftMailer);
272
        $this->containerBuilder->set("translator", $this->translator);
273
        $this->containerBuilder->set("twig", $this->twigEnvironment);
274
    }
275
}
276