Completed
Pull Request — master (#791)
by
unknown
02:10
created

testExecuteAnnotationsWithBundle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 12
Ratio 109.09 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 12
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Tests\Command;
4
5
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
6
use PHPUnit\Framework\TestCase;
7
use Symfony\Bundle\FrameworkBundle\Console\Application;
8
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
9
use Symfony\Component\Config\Loader\LoaderInterface;
10
use Symfony\Component\Console\Tester\CommandTester;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\Filesystem\Filesystem;
13
use Symfony\Component\HttpKernel\Bundle\Bundle;
14
use Symfony\Component\HttpKernel\Kernel;
15
16
class ImportMappingDoctrineCommandTest extends TestCase
17
{
18
    /** @var Kernel|null */
19
    private $kernel;
20
21
    /** @var CommandTester|null */
22
    private $commandTester;
23
24
    public function setup()
25
    {
26
        $this->kernel = new ImportMappingTestingKernel('test', true);
27
        $this->kernel->boot();
28
29
        $connection = $this->kernel->getContainer()
30
            ->get('doctrine')
31
            ->getConnection();
32
        $connection->executeQuery('CREATE TABLE product (id integer primary key, name varchar(20), hint text)');
33
34
        $application         = new Application($this->kernel);
35
        $command             = $application->find('doctrine:mapping:import');
36
        $this->commandTester = new CommandTester($command);
37
    }
38
39
    public function tearDown()
40
    {
41
        $fs = new Filesystem();
42
        if ($this->kernel !== null) {
43
            $fs->remove($this->kernel->getCacheDir());
44
        }
45
46
        $fs->remove(sys_get_temp_dir() . '/import_mapping_bundle');
47
    }
48
49 View Code Duplication
    public function testExecuteXmlWithBundle()
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...
50
    {
51
        $this->commandTester->execute(['name' => 'ImportMappingTestFooBundle']);
52
53
        $expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Resources/config/doctrine/Product.orm.xml';
54
        $this->assertFileExists($expectedMetadataPath);
55
        $this->assertContains('"Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
56
    }
57
58 View Code Duplication
    public function testExecuteAnnotationsWithBundle()
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...
59
    {
60
        $this->commandTester->execute([
61
            'name' => 'ImportMappingTestFooBundle',
62
            'mapping-type' => 'annotation',
63
        ]);
64
65
        $expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Entity/Product.php';
66
        $this->assertFileExists($expectedMetadataPath);
67
        $this->assertContains('namespace Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity;', file_get_contents($expectedMetadataPath), 'File contains correct namespace');
68
    }
69
70
    /**
71
     * @expectedException \InvalidArgumentException
72
     * @expectedExceptionMessageRegExp /The \-\-path option is required/
73
     */
74
    public function testExecuteThrowsExceptionWithNamespaceAndNoPath()
75
    {
76
        $this->commandTester->execute(['name' => 'Some\\Namespace']);
77
    }
78
79 View Code Duplication
    public function testExecuteXmlWithNamespace()
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...
80
    {
81
        $this->commandTester->execute([
82
            'name' => 'Some\\Namespace\\Entity',
83
            '--path' => $this->kernel->getRootDir() . '/config/doctrine',
84
        ]);
85
86
        $expectedMetadataPath = $this->kernel->getRootDir() . '/config/doctrine/Product.orm.xml';
87
        $this->assertFileExists($expectedMetadataPath);
88
        $this->assertContains('"Some\Namespace\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
89
    }
90
91 View Code Duplication
    public function testExecuteAnnotationsWithNamespace()
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...
92
    {
93
        $this->commandTester->execute([
94
            'name' => 'Some\\Namespace\\Entity',
95
            '--path' => $this->kernel->getRootDir() . '/src/Entity',
96
            'mapping-type' => 'annotation',
97
        ]);
98
99
        $expectedMetadataPath = $this->kernel->getRootDir() . '/src/Entity/Product.php';
100
        $this->assertFileExists($expectedMetadataPath);
101
        $this->assertContains('namespace Some\Namespace\Entity;', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
102
    }
103
}
104
105
class ImportMappingTestingKernel extends Kernel
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
106
{
107
    public function registerBundles()
108
    {
109
        return [
110
            new FrameworkBundle(),
111
            new DoctrineBundle(),
112
            new ImportMappingTestFooBundle(),
113
        ];
114
    }
115
116
    public function registerContainerConfiguration(LoaderInterface $loader)
117
    {
118
        $loader->load(function (ContainerBuilder $container) {
119
            $container->loadFromExtension('framework', ['secret' => 'F00']);
120
121
            $container->loadFromExtension('doctrine', [
122
                'dbal' => [
123
                    'driver' => 'pdo_sqlite',
124
                    'path' => $this->getCacheDir() . '/testing.db',
125
                ],
126
                'orm' => [],
127
            ]);
128
129
            // Register a NullLogger to avoid getting the stderr default logger of FrameworkBundle
130
            $container->register('logger', 'Psr\Log\NullLogger');
131
        });
132
    }
133
134
    public function getRootDir()
135
    {
136
        return sys_get_temp_dir() . '/sf_kernel_' . spl_object_hash($this);
137
    }
138
}
139
140
class ImportMappingTestFooBundle extends Bundle
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
141
{
142
    public function getPath()
143
    {
144
        return sys_get_temp_dir() . '/import_mapping_bundle';
145
    }
146
}
147