1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\EasyExtendsBundle\Tests\Bundle; |
15
|
|
|
|
16
|
|
|
// Unfortunately phpunit cannot mock a class in chosen namespace. |
17
|
|
|
// Therefore mocks are stored in Fixtures/bundle1 directory and required here. |
18
|
|
|
require_once __DIR__.'/Fixtures/bundle1/SonataAcmeBundle.php'; |
19
|
|
|
require_once __DIR__.'/Fixtures/bundle1/SonataNotExtendableBundle.php'; |
20
|
|
|
require_once __DIR__.'/Fixtures/bundle1/SymfonyNotExtendableBundle.php'; |
21
|
|
|
require_once __DIR__.'/Fixtures/bundle1/LongNamespaceBundle.php'; |
22
|
|
|
require_once __DIR__.'/Fixtures/bundle1/AcmeBundle.php'; |
23
|
|
|
|
24
|
|
|
use PHPUnit\Framework\TestCase; |
25
|
|
|
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata; |
26
|
|
|
|
27
|
|
|
class BundleMetadataTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
public function testBundleMetadata(): void |
30
|
|
|
{ |
31
|
|
|
$bundle = new \Sonata\AcmeBundle\SonataAcmeBundle(); |
32
|
|
|
|
33
|
|
|
$bundleMetadata = new BundleMetadata($bundle, [ |
34
|
|
|
'application_dir' => 'app/Application/:vendor', |
35
|
|
|
'namespace' => 'Application\\:vendor', |
36
|
|
|
'namespace_prefix' => '', |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
$this->assertTrue($bundleMetadata->isExtendable()); |
40
|
|
|
$this->assertTrue($bundleMetadata->isValid()); |
41
|
|
|
$this->assertEquals('SonataAcmeBundle', $bundleMetadata->getName()); |
42
|
|
|
$this->assertEquals('Sonata', $bundleMetadata->getVendor()); |
43
|
|
|
$this->assertEquals('Sonata\AcmeBundle', $bundleMetadata->getNamespace()); |
44
|
|
|
$this->assertEquals('app/Application/Sonata/AcmeBundle', $bundleMetadata->getExtendedDirectory()); |
45
|
|
|
$this->assertEquals('Application\Sonata\AcmeBundle', $bundleMetadata->getExtendedNamespace()); |
46
|
|
|
$this->assertInstanceOf('Sonata\EasyExtendsBundle\Bundle\OrmMetadata', $bundleMetadata->getOrmMetadata()); |
47
|
|
|
$this->assertInstanceOf('Sonata\EasyExtendsBundle\Bundle\OdmMetadata', $bundleMetadata->getOdmMetadata()); |
48
|
|
|
$this->assertSame($bundle, $bundleMetadata->getBundle()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testCustomNamespace(): void |
52
|
|
|
{ |
53
|
|
|
$bundle = new \Sonata\AcmeBundle\SonataAcmeBundle(); |
54
|
|
|
|
55
|
|
|
$bundleMetadata = new BundleMetadata($bundle, [ |
56
|
|
|
'application_dir' => 'app/Custom/:vendor', |
57
|
|
|
'namespace' => 'Custom\\:vendor', |
58
|
|
|
'namespace_prefix' => '', |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$this->assertEquals('app/Custom/Sonata/AcmeBundle', $bundleMetadata->getExtendedDirectory()); |
62
|
|
|
$this->assertEquals('Custom\Sonata\AcmeBundle', $bundleMetadata->getExtendedNamespace()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testApplicationNotExtendableBundle(): void |
66
|
|
|
{ |
67
|
|
|
$bundle = new \Application\Sonata\NotExtendableBundle(); |
68
|
|
|
|
69
|
|
|
$bundleMetadata = new BundleMetadata($bundle, [ |
70
|
|
|
'application_dir' => 'Application', |
71
|
|
|
'namespace' => 'Application', |
72
|
|
|
'namespace_prefix' => '', |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$this->assertFalse($bundleMetadata->isValid()); |
76
|
|
|
$this->assertFalse($bundleMetadata->isExtendable()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testSymfonyNotExtendableBundle(): void |
80
|
|
|
{ |
81
|
|
|
$bundle = new \Symfony\Bundle\NotExtendableBundle(); |
82
|
|
|
|
83
|
|
|
$bundleMetadata = new BundleMetadata($bundle, [ |
84
|
|
|
'application_dir' => 'Application', |
85
|
|
|
'namespace' => 'Application', |
86
|
|
|
'namespace_prefix' => '', |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
$this->assertFalse($bundleMetadata->isValid()); |
90
|
|
|
$this->assertFalse($bundleMetadata->isExtendable()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testBundleNamespace(): void |
94
|
|
|
{ |
95
|
|
|
$bundle = new \Sonata\Bundle\AcmeBundle\LongNamespaceBundle(); |
96
|
|
|
|
97
|
|
|
$bundleMetadata = new BundleMetadata($bundle, [ |
98
|
|
|
'application_dir' => 'Application', |
99
|
|
|
'namespace' => 'Application', |
100
|
|
|
'namespace_prefix' => '', |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
$this->assertFalse($bundleMetadata->isValid()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testBundleName(): void |
107
|
|
|
{ |
108
|
|
|
$bundle = new \Sonata\AcmeBundle\AcmeBundle(); |
109
|
|
|
|
110
|
|
|
$bundleMetadata = new BundleMetadata($bundle, [ |
111
|
|
|
'application_dir' => 'Application', |
112
|
|
|
'namespace' => 'Application', |
113
|
|
|
'namespace_prefix' => '', |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
$this->assertFalse($bundleMetadata->isValid()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testWithNamespacePrefix(): void |
120
|
|
|
{ |
121
|
|
|
$bundle = new \Sonata\AcmeBundle\SonataAcmeBundle(); |
122
|
|
|
|
123
|
|
|
$bundleMetadata = new BundleMetadata($bundle, [ |
124
|
|
|
'application_dir' => 'src/Application/:vendor', |
125
|
|
|
'namespace' => 'Application\\:vendor', |
126
|
|
|
'namespace_prefix' => 'App\\', |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
$this->assertEquals('SonataAcmeBundle', $bundleMetadata->getName()); |
130
|
|
|
$this->assertEquals('Sonata', $bundleMetadata->getVendor()); |
131
|
|
|
$this->assertEquals('Application', $bundleMetadata->getApplication()); |
132
|
|
|
$this->assertEquals('Sonata\AcmeBundle', $bundleMetadata->getNamespace()); |
133
|
|
|
$this->assertEquals('src/Application/Sonata/AcmeBundle', $bundleMetadata->getExtendedDirectory()); |
134
|
|
|
$this->assertEquals('App\Application\Sonata\AcmeBundle', $bundleMetadata->getExtendedNamespace()); |
135
|
|
|
$this->assertSame($bundle, $bundleMetadata->getBundle()); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.