WebTest::getAuthClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Starkerxp\StructureBundle\Test;
4
5
use Doctrine\ORM\EntityManager;
6
use Liip\FunctionalTestBundle\Test\WebTestCase;
7
use Symfony\Bundle\FrameworkBundle\Console\Application;
8
use Symfony\Component\Console\Input\ArrayInput;
9
use Symfony\Component\Console\Output\BufferedOutput;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
12
abstract class WebTest extends WebTestCase
13
{
14
    protected $identifiant = "[email protected]";
15
    protected $motDePasse = "motMotDePasse";
16
    protected $idGeneratorTypes = [];
17
18
    /**
19
     * Erase all database data.
20
     */
21
    public function setUp()
22
    {
23
        parent::setUp();
24
        $this->loadFixtureFiles([]);
25
    }
26
27
    public function loadFixtureFiles(array $paths = [], $append = false, $omName = null, $registryName = 'doctrine', $purgeMode = null)
28
    {
29
        $retour = parent::loadFixtureFiles($paths, $append, $omName, $registryName, $purgeMode);
30
        $this->recoverIdGenerators();
31
        $this->getEntityManager()->clear();
32
33
        return $retour;
34
    }
35
36
    protected function recoverIdGenerators($name = null)
37
    {
38
        foreach ($this->idGeneratorTypes as $entityClass => $idGeneratorType) {
39
            $metadata = $this->getEntityManager($name)->getClassMetadata($entityClass);
40
            $metadata->setIdGeneratorType($idGeneratorType);
41
        }
42
    }
43
44
    /**
45
     * @param string|null $name
46
     * @return EntityManager
47
     */
48
    public function getEntityManager($name = null)
49
    {
50
        $entityManager = $this->getContainer()->get('doctrine')->getManager($name);
51
52
        return $entityManager;
53
    }
54
55
    public function tearDown()
56
    {
57
        parent::tearDown();
58
        $this->getEntityManager()->getConnection()->close();
59
    }
60
61
    public function getAuthClient()
62
    {
63
        $client = static::createClient();
64
        $client->request(
65
            'POST',
66
            '/api/login_check',
67
            [
68
                'identifiant' => $this->identifiant,
69
                'motDePasse' => $this->motDePasse,
70
            ]
71
        );
72
        $dataHeader = json_decode($client->getResponse()->getContent(), true);
73
74
        $client = static::createClient();
75
        $client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', $dataHeader['token']));
76
77
        return $client;
78
    }
79
80
    /**
81
     * @param string|null $name
82
     * @return \Doctrine\DBAL\Connection
83
     */
84
    public function getConnection($name = null)
85
    {
86
        return $this->getContainer()->get('doctrine')->getConnection($name);
87
    }
88
89
    /**
90
     * @param $entiteCQFD
91
     * @param string|null $name
92
     * @return \Doctrine\Common\Persistence\ObjectRepository
93
     */
94
    public function getRepository($entiteCQFD, $name = null)
95
    {
96
        $repository = $this->getEntityManager($name)->getRepository($entiteCQFD);
97
98
        return $repository;
99
    }
100
101
    public function executerCommande($command = [])
102
    {
103
        $container = $this->getContainer();
104
        $kernel = $container->get('kernel');
105
        $application = new Application($kernel);
106
        $application->setAutoExit(false);
107
        $input = new ArrayInput($command);
108
        $output = new BufferedOutput();
109
        $application->run($input, $output);
110
    }
111
112
    /**
113
     * Call protected/private method of a class.
114
     *
115
     * @param object $object Instantiated object that we will run method on.
116
     * @param string $methodName Method name to call
117
     * @param array $parameters Array of parameters to pass into method.
118
     *
119
     * @return mixed Method return.
120
     */
121
    public function invokeMethod($object, $methodName, array $parameters = [])
122
    {
123
        $reflection = new \ReflectionClass(get_class($object));
124
        $method = $reflection->getMethod($methodName);
125
        $method->setAccessible(true);
126
127
        return $method->invokeArgs($object, $parameters);
128
    }
129
130
    /**
131
     * Generates a URL from the given parameters.
132
     *
133
     * @param string $route The name of the route
134
     * @param mixed $parameters An array of parameters
135
     * @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
136
     *
137
     * @return string The generated URL
138
     *
139
     * @see UrlGeneratorInterface
140
     */
141
    protected function generateUrl($route, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
142
    {
143
        return $this->getContainer()->get('router')->generate($route, $parameters, $referenceType);
144
    }
145
146
    protected function allowFixedIdsFor(array $entityClasses, $name = null)
147
    {
148
        foreach ($entityClasses as $entityClass) {
149
            $metadata = $this->getEntityManager($name)->getClassMetadata($entityClass);
150
            $this->idGeneratorTypes[$entityClass] = $metadata->generatorType;
151
            $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);
152
            $metadata->setIdGenerator(new \Doctrine\ORM\Id\AssignedGenerator());
153
        }
154
    }
155
156
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
157