Completed
Push — feature/427-FeatureToggle-with... ( 142147 )
by Michael
05:42
created

WebTestCase::createClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
     * @throws \Exception
46
     */
47
    public function setUp(): void
48
    {
49
        parent::setUp();
50
51
        self::bootKernel();
52
53
        self::$em = self::$kernel->getContainer()
54
            ->get('doctrine_mongodb')
55
            ->getManager();
56
57
        $this->createAdminUser();
58
    }
59
60
    /**
61
     * @return void
62
     *
63
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
64
     */
65
    public function tearDown(): void
66
    {
67
        parent::tearDown();
68
69
        self::$em->createQueryBuilder(Importfile::class)->remove()->getQuery()->execute();
70
        self::$em->createQueryBuilder(Licensee::class)->remove()->getQuery()->execute();
71
        self::$em->createQueryBuilder(Readpath::class)->remove()->getQuery()->execute();
72
        self::$em->createQueryBuilder(Textnode::class)->remove()->getQuery()->execute();
73
        self::$em->createQueryBuilder(TextnodeHitch::class)->remove()->getQuery()->execute();
74
        self::$em->createQueryBuilder(Topic::class)->remove()->getQuery()->execute();
75
        self::$em->createQueryBuilder(User::class)->remove()->getQuery()->execute();
76
    }
77
78
    /**
79
     * @param array $options
80
     * @param array $server
81
     *
82
     * @return \Symfony\Bundle\FrameworkBundle\Client
83
     */
84
    protected static function createClient(array $options = [], array $server = [])
85
    {
86
        $server['PHP_AUTH_USER'] = '[email protected]';
87
        $server['PHP_AUTH_PW'] = 'dembelo';
88
        return parent::createClient($options, $server);
89
    }
90
91
    /**
92
     * @return DocumentManager
93
     */
94
    protected function getMongo(): DocumentManager
95
    {
96
        return self::$em;
97
    }
98
99
    /**
100
     * @throws \Exception
101
     *
102
     * @return void
103
     */
104
    private function createAdminUser(): void
105
    {
106
        $passwordEncoder = self::$kernel->getContainer()->get('security.password_encoder');
107
        $user = new User();
108
        $user->setStatus(1);
109
        $user->setEmail('[email protected]');
110
        $user->setPassword($passwordEncoder->encodePassword($user, 'dembelo'));
111
        $user->setRoles(['ROLE_ADMIN']);
112
        $user->setStatus(1);
113
        $user->setMetadata(['created' => time(), 'updated' => time()]);
114
        self::$em->persist($user);
115
        self::$em->flush();
116
    }
117
}
118