ImportfileControllerTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 63
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testImportAction() 0 46 1
A tearDown() 0 5 1
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 AdminBundle\IntegrationTests\Controller;
21
22
use DembeloMain\Document\Importfile;
23
use DembeloMain\Document\Licensee;
24
use DembeloMain\Document\Textnode;
25
use DembeloMain\Document\TextnodeHitch;
26
use DembeloMain\Document\Topic;
27
use DembeloMain\IntegrationTests\WebTestCase;
28
29
/**
30
 * @group integration
31
 */
32
class ImportfileControllerTest extends WebTestCase
33
{
34
    private const TMP_PATH = '/tmp/dembelotest/';
35
36
    /**
37
     * @throws \Doctrine\ODM\MongoDB\MongoDBException
38
     */
39
    public function tearDown(): void
40
    {
41
        parent::tearDown(); // TODO: Change the autogenerated stub
42
        @unlink(self::TMP_PATH);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

42
        /** @scrutinizer ignore-unhandled */ @unlink(self::TMP_PATH);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
43
        @rmdir(self::TMP_PATH);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

43
        /** @scrutinizer ignore-unhandled */ @rmdir(self::TMP_PATH);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
44
    }
45
46
    /**
47
     * @return void
48
     */
49
    public function testImportAction(): void
50
    {
51
        $teststorySourcePath = __DIR__.'/../Fixtures/teststory.html';
52
        @mkdir(self::TMP_PATH);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

52
        /** @scrutinizer ignore-unhandled */ @mkdir(self::TMP_PATH);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
53
        $teststoryPath = self::TMP_PATH.'teststory.html';
54
        copy($teststorySourcePath, $teststoryPath);
55
56
        $licensee = new Licensee();
57
        $licensee->setName('some Licensee');
58
        $this->getMongo()->persist($licensee);
59
60
        $topic = new Topic();
61
        $topic->setName('some Topic');
62
        $this->getMongo()->persist($topic);
63
64
        $importfile = new Importfile();
65
        $importfile->setTopicId($topic->getId());
66
        $importfile->setName('some importfile name');
67
        $importfile->setOriginalname('some importfile original name');
68
        $importfile->setFilename($teststoryPath);
69
        $importfile->setAuthor('some author name');
70
        $importfile->setPublisher('some publisher name');
71
        $importfile->setLicenseeId($licensee->getId());
72
        $this->getMongo()->persist($importfile);
73
74
        $this->getMongo()->flush();
75
76
        $parameters = [
77
            'importfileId' => $importfile->getId(),
78
        ];
79
        $client = static::createClient();
80
        $client->request('POST', '/admin/import', $parameters);
81
        $response = $client->getResponse();
82
        self::assertEquals(200, $response->getStatusCode());
83
84
        $textnodes = $this->getMongo()->getRepository(Textnode::class)->findAll();
85
        self::assertCount(4, $textnodes);
86
        $textnodeHitches = $this->getMongo()->getRepository(TextnodeHitch::class)->findAll();
87
        self::assertCount(4, $textnodeHitches);
88
89
        $accessTextnodes = $this->getMongo()->getRepository(Textnode::class)->findBy(['access' => true]);
90
        self::assertCount(1, $accessTextnodes);
91
        $accessTextnode = $accessTextnodes[0];
92
        self::assertEquals('1', $accessTextnode->getTwineId());
93
        self::assertEquals(2, $accessTextnode->getChildHitches()->count());
94
        self::assertEquals(0, $accessTextnode->getParentHitches()->count());
95
    }
96
}
97