Completed
Pull Request — master (#1203)
by
unknown
04:38
created

setUpBeforeClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;
4
5
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel;
6
use Doctrine\ORM\EntityManagerInterface;
7
use PHPUnit\Framework\TestCase;
8
use Symfony\Bundle\FrameworkBundle\Console\Application;
9
use Symfony\Component\Console\Tester\CommandTester;
10
use Symfony\Component\Filesystem\Filesystem;
11
use Symfony\Component\HttpKernel\Bundle\Bundle;
12
13
/**
14
 * @group legacy
15
 */
16
class ImportMappingDoctrineCommandTest extends TestCase
17
{
18
    /** @var TestKernel|null */
19
    private $kernel;
20
21
    /** @var CommandTester|null */
22
    private $commandTester;
23
24
    public static function setUpBeforeClass() : void
25
    {
26
        if (interface_exists(EntityManagerInterface::class)) {
27
            return;
28
        }
29
30
        self::markTestSkipped('This test requires ORM');
31
    }
32
33
    protected function setup() : void
34
    {
35
        $this->kernel = new class() extends TestKernel {
36
            public function registerBundles() : iterable
37
            {
38
                yield from parent::registerBundles();
39
                yield new ImportMappingTestFooBundle();
40
            }
41
        };
42
43
        $this->kernel->boot();
44
45
        $connection = $this->kernel->getContainer()
46
            ->get('doctrine')
47
            ->getConnection();
48
        $connection->executeQuery('CREATE TABLE product (id integer primary key, name varchar(20), hint text)');
49
50
        $application         = new Application($this->kernel);
51
        $command             = $application->find('doctrine:mapping:import');
52
        $this->commandTester = new CommandTester($command);
53
    }
54
55
    protected function tearDown() : void
56
    {
57
        $fs = new Filesystem();
58
        if ($this->kernel !== null) {
59
            $fs->remove($this->kernel->getCacheDir());
60
        }
61
62
        $fs->remove(sys_get_temp_dir() . '/import_mapping_bundle');
63
        $this->kernel        = null;
64
        $this->commandTester = null;
65
    }
66
67 View Code Duplication
    public function testExecuteXmlWithBundle() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $this->commandTester->execute(['name' => 'ImportMappingTestFooBundle']);
70
71
        $expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Resources/config/doctrine/Product.orm.xml';
72
        $this->assertFileExists($expectedMetadataPath);
73
        $this->assertContains('"Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
74
    }
75
76 View Code Duplication
    public function testExecuteAnnotationsWithBundle() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $this->commandTester->execute([
79
            'name' => 'ImportMappingTestFooBundle',
80
            'mapping-type' => 'annotation',
81
        ]);
82
83
        $expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Entity/Product.php';
84
        $this->assertFileExists($expectedMetadataPath);
85
        $this->assertContains('namespace Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity;', file_get_contents($expectedMetadataPath), 'File contains correct namespace');
86
    }
87
88
    /**
89
     * @expectedException \InvalidArgumentException
90
     * @expectedExceptionMessageRegExp /The --path option is required/
91
     */
92
    public function testExecuteThrowsExceptionWithNamespaceAndNoPath() : void
93
    {
94
        $this->commandTester->execute(['name' => 'Some\Namespace']);
95
    }
96
97 View Code Duplication
    public function testExecuteXmlWithNamespace() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $this->commandTester->execute([
100
            'name' => 'Some\Namespace\Entity',
101
            '--path' => $this->kernel->getProjectDir() . '/config/doctrine',
102
        ]);
103
104
        $expectedMetadataPath = $this->kernel->getProjectDir() . '/config/doctrine/Product.orm.xml';
105
        $this->assertFileExists($expectedMetadataPath);
106
        $this->assertContains('"Some\Namespace\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
107
    }
108
109 View Code Duplication
    public function testExecuteAnnotationsWithNamespace() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $this->commandTester->execute([
112
            'name' => 'Some\Namespace\Entity',
113
            '--path' => $this->kernel->getProjectDir() . '/src/Entity',
114
            'mapping-type' => 'annotation',
115
        ]);
116
117
        $expectedMetadataPath = $this->kernel->getProjectDir() . '/src/Entity/Product.php';
118
        $this->assertFileExists($expectedMetadataPath);
119
        $this->assertContains('namespace Some\Namespace\Entity;', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
120
    }
121
}
122
123
class ImportMappingTestFooBundle extends Bundle
124
{
125
    public function getPath() : string
126
    {
127
        return sys_get_temp_dir() . '/import_mapping_bundle';
128
    }
129
}
130