Passed
Push — master ( 94badf...ee3e77 )
by Michael
01:46
created

WebTestCase::getMongo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/* Copyright (C) 2018 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace DembeloMain\IntegrationTests;
21
22
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SymfonyWebTestCase;
23
use Doctrine\ODM\MongoDB\DocumentManager;
24
use DembeloMain\Document\Importfile;
25
use DembeloMain\Document\Licensee;
26
use DembeloMain\Document\Readpath;
27
use DembeloMain\Document\Textnode;
28
use DembeloMain\Document\TextnodeHitch;
29
use DembeloMain\Document\Topic;
30
use DembeloMain\Document\User;
31
use Symfony\Component\BrowserKit\Client;
32
33
/**
34
 * Class WebTestCase
35
 */
36
class WebTestCase extends SymfonyWebTestCase
37
{
38
    /**
39
     * @var DocumentManager
40
     */
41
    private static $em;
42
43
    /**
44
     * @return void
45
     *
46
     * @throws \Exception
47
     */
48
    public function setUp(): void
49
    {
50
        parent::setUp();
51
52
        self::bootKernel();
53
54
        self::$em = self::$kernel->getContainer()
55
            ->get('doctrine_mongodb')
56
            ->getManager();
57
58
        $this->createAdminUser();
59
    }
60
61
    /**
62
     * @return void
63
     *
64
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
65
     */
66
    public function tearDown(): void
67
    {
68
        parent::tearDown();
69
70
        self::$em->createQueryBuilder(Importfile::class)->remove()->getQuery()->execute();
71
        self::$em->createQueryBuilder(Licensee::class)->remove()->getQuery()->execute();
72
        self::$em->createQueryBuilder(Readpath::class)->remove()->getQuery()->execute();
73
        self::$em->createQueryBuilder(Textnode::class)->remove()->getQuery()->execute();
74
        self::$em->createQueryBuilder(TextnodeHitch::class)->remove()->getQuery()->execute();
75
        self::$em->createQueryBuilder(Topic::class)->remove()->getQuery()->execute();
76
        self::$em->createQueryBuilder(User::class)->remove()->getQuery()->execute();
77
    }
78
79
    /**
80
     * @param array $options
81
     * @param array $server
82
     *
83
     * @return \Symfony\Bundle\FrameworkBundle\Client
84
     */
85
    protected static function createClient(array $options = [], array $server = [])
86
    {
87
        $server['PHP_AUTH_USER'] = '[email protected]';
88
        $server['PHP_AUTH_PW'] = 'dembelo';
89
90
        return parent::createClient($options, $server);
91
    }
92
93
    /**
94
     * @return DocumentManager
95
     */
96
    protected function getMongo(): DocumentManager
97
    {
98
        return self::$em;
99
    }
100
101
    /**
102
     * @throws \Exception
103
     *
104
     * @return void
105
     */
106
    private function createAdminUser(): void
107
    {
108
        $passwordEncoder = self::$kernel->getContainer()->get('security.password_encoder');
109
        $user = new User();
110
        $user->setStatus(1);
111
        $user->setEmail('[email protected]');
112
        $user->setPassword($passwordEncoder->encodePassword($user, 'dembelo'));
113
        $user->setRoles(['ROLE_ADMIN']);
114
        $user->setStatus(1);
115
        $user->setMetadata(['created' => time(), 'updated' => time()]);
116
        self::$em->persist($user);
117
        self::$em->flush();
118
    }
119
}
120