Completed
Push — master ( c5d644...a435b5 )
by Guillaume
02:45
created

WebTest::getRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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