Completed
Push — master ( 4a844a...bb1ac0 )
by Andreas
14:48 queued 19s
created

  A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;
4
5
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel;
6
use PHPUnit\Framework\TestCase;
7
use Symfony\Bundle\FrameworkBundle\Console\Application;
8
use Symfony\Component\Console\Tester\CommandTester;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\HttpKernel\Bundle\Bundle;
11
12
/**
13
 * @group legacy
14
 */
15
class ImportMappingDoctrineCommandTest extends TestCase
16
{
17
    /** @var TestKernel|null */
18
    private $kernel;
19
20
    /** @var CommandTester|null */
21
    private $commandTester;
22
23
    protected function setup() : void
24
    {
25
        $this->kernel = new class() extends TestKernel {
26
            public function registerBundles() : iterable
27
            {
28
                yield from parent::registerBundles();
29
                yield new ImportMappingTestFooBundle();
30
            }
31
        };
32
33
        $this->kernel->boot();
34
35
        $connection = $this->kernel->getContainer()
36
            ->get('doctrine')
37
            ->getConnection();
38
        $connection->executeQuery('CREATE TABLE product (id integer primary key, name varchar(20), hint text)');
39
40
        $application         = new Application($this->kernel);
41
        $command             = $application->find('doctrine:mapping:import');
42
        $this->commandTester = new CommandTester($command);
43
    }
44
45
    protected function tearDown() : void
46
    {
47
        $fs = new Filesystem();
48
        if ($this->kernel !== null) {
49
            $fs->remove($this->kernel->getCacheDir());
50
        }
51
52
        $fs->remove(sys_get_temp_dir() . '/import_mapping_bundle');
53
        $this->kernel        = null;
54
        $this->commandTester = null;
55
    }
56
57 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...
58
    {
59
        $this->commandTester->execute(['name' => 'ImportMappingTestFooBundle']);
60
61
        $expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Resources/config/doctrine/Product.orm.xml';
62
        $this->assertFileExists($expectedMetadataPath);
63
        $this->assertContains('"Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
64
    }
65
66 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...
67
    {
68
        $this->commandTester->execute([
69
            'name' => 'ImportMappingTestFooBundle',
70
            'mapping-type' => 'annotation',
71
        ]);
72
73
        $expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Entity/Product.php';
74
        $this->assertFileExists($expectedMetadataPath);
75
        $this->assertContains('namespace Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity;', file_get_contents($expectedMetadataPath), 'File contains correct namespace');
76
    }
77
78
    /**
79
     * @expectedException \InvalidArgumentException
80
     * @expectedExceptionMessageRegExp /The --path option is required/
81
     */
82
    public function testExecuteThrowsExceptionWithNamespaceAndNoPath() : void
83
    {
84
        $this->commandTester->execute(['name' => 'Some\Namespace']);
85
    }
86
87 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...
88
    {
89
        $this->commandTester->execute([
90
            'name' => 'Some\Namespace\Entity',
91
            '--path' => $this->kernel->getProjectDir() . '/config/doctrine',
92
        ]);
93
94
        $expectedMetadataPath = $this->kernel->getProjectDir() . '/config/doctrine/Product.orm.xml';
95
        $this->assertFileExists($expectedMetadataPath);
96
        $this->assertContains('"Some\Namespace\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
97
    }
98
99 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...
100
    {
101
        $this->commandTester->execute([
102
            'name' => 'Some\Namespace\Entity',
103
            '--path' => $this->kernel->getProjectDir() . '/src/Entity',
104
            'mapping-type' => 'annotation',
105
        ]);
106
107
        $expectedMetadataPath = $this->kernel->getProjectDir() . '/src/Entity/Product.php';
108
        $this->assertFileExists($expectedMetadataPath);
109
        $this->assertContains('namespace Some\Namespace\Entity;', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
110
    }
111
}
112
113
class ImportMappingTestFooBundle extends Bundle
114
{
115
    public function getPath() : string
116
    {
117
        return sys_get_temp_dir() . '/import_mapping_bundle';
118
    }
119
}
120