Completed
Push — master ( 902133...f13570 )
by Philip
23:54
created

BaseAcceptanceTest::loadClientAndFixtures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Tests\Acceptance;
4
5
use App\Entity\User;
6
use App\Tests\Integration\BaseIntegrationTest;
7
use Doctrine\Common\DataFixtures\ReferenceRepository;
8
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
9
use Symfony\Component\BrowserKit\Cookie;
10
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
11
12
abstract class BaseAcceptanceTest extends BaseIntegrationTest
13
{
14
    /** @var KernelBrowser */
15
    protected $client;
16
17
    /** @var ReferenceRepository; */
18
    protected $referenceRepository;
19
20
    protected function loadClientAndFixtures(array $classNames = [], bool $catchExceptions = true): ReferenceRepository
21
    {
22
        $this->client = self::createClient();
23
        $this->client->catchExceptions($catchExceptions);
24
        $this->referenceRepository = $this->loadFixtures($classNames)->getReferenceRepository();
25
26
        return $this->referenceRepository;
27
    }
28
29
    protected function logIn(User $user)
30
    {
31
        $session = $this->client->getContainer()->get('session');
32
33
        $firewall = 'main';
34
        $token = new UsernamePasswordToken($user, null, $firewall, $user->getRoles());
35
        $session->set('_security_' . $firewall, serialize($token));
36
        $session->save();
37
38
        $cookie = new Cookie($session->getName(), $session->getId());
39
        $this->client->getCookieJar()->set($cookie);
40
    }
41
42
    protected function logOut()
43
    {
44
        $session = $this->client->getContainer()->get('session');
45
        $session->clear();
46
        $session->save();
47
48
        $this->client->getCookieJar()->clear();
49
    }
50
}
51