[FEATURE] Integrate preliminary PackageManager API
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Package / PackageTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Package;
3
4 /* *
5 * This script belongs to the TYPO3 Flow framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 use TYPO3\Flow\Package\Package as FlowPackage;
15 use org\bovigo\vfs\vfsStream;
16
17 /**
18 * Testcase for the package class
19 *
20 */
21 class PackageTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
22
23 /**
24 */
25 public function setUp() {
26 vfsStream::setup('Packages');
27 }
28
29 /**
30 * @test
31 * @expectedException \TYPO3\Flow\Package\Exception\InvalidPackagePathException
32 */
33 public function constructThrowsPackageDoesNotExistException() {
34 new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'Vendor.TestPackage', './ThisPackageSurelyDoesNotExist');
35 }
36
37 /**
38 */
39 public function validPackageKeys() {
40 return array(
41 array('Doctrine.DBAL'),
42 array('TYPO3.Flow'),
43 array('RobertLemke.Flow.Twitter'),
44 array('Sumphonos.Stem'),
45 array('Schalke04.Soccer.MagicTrainer')
46 );
47 }
48
49 /**
50 * @test
51 * @dataProvider validPackageKeys
52 */
53 public function constructAcceptsValidPackageKeys($packageKey) {
54 $packagePath = 'vfs://Packages/' . str_replace('\\', '/', $packageKey) . '/';
55 mkdir($packagePath, 0777, TRUE);
56 file_put_contents($packagePath . 'composer.json', '{"name": "' . $packageKey . '", "type": "flow-test"}');
57
58 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), $packageKey, $packagePath);
59 $this->assertEquals($packageKey, $package->getPackageKey());
60 }
61
62 /**
63 */
64 public function invalidPackageKeys() {
65 return array(
66 array('TYPO3..Flow'),
67 array('RobertLemke.Flow. Twitter'),
68 array('Schalke*4')
69 );
70 }
71
72 /**
73 * @test
74 * @dataProvider invalidPackageKeys
75 * @expectedException \TYPO3\Flow\Package\Exception\InvalidPackageKeyException
76 */
77 public function constructRejectsInvalidPackageKeys($packageKey) {
78 $packagePath = 'vfs://Packages/' . str_replace('\\', '/', $packageKey) . '/';
79 mkdir($packagePath, 0777, TRUE);
80 new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), $packageKey, $packagePath);
81 }
82
83 /**
84 * @test
85 */
86 public function getNamespaceReturnsThePhpNamespaceCorrespondingToThePackageKey() {
87 $packagePath = 'vfs://Packages/Application/Acme.MyPackage/';
88 mkdir($packagePath, 0777, TRUE);
89 file_put_contents($packagePath . 'composer.json', '{"name": "acme/mypackage", "type": "flow-test"}');
90 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'Acme.MyPackage', $packagePath);
91 $this->assertEquals('Acme\\MyPackage', $package->getNamespace());
92 }
93
94 /**
95 * @test
96 */
97 public function getMetaPathReturnsPathToMetaDirectory() {
98 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'TYPO3.CMS.Core', PATH_typo3 . 'sysext/core/');
99 $packageMetaDataPath = $package->getMetaPath();
100 $this->assertSame($package->getPackagePath() . FlowPackage::DIRECTORY_METADATA, $packageMetaDataPath);
101 }
102
103 /**
104 * @test
105 */
106 public function getDocumentationPathReturnsPathToDocumentationDirectory() {
107 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'TYPO3.CMS.Core', PATH_typo3 . 'sysext/core/');
108 $packageDocumentationPath = $package->getDocumentationPath();
109
110 $this->assertEquals($package->getPackagePath() . FlowPackage::DIRECTORY_DOCUMENTATION, $packageDocumentationPath);
111 }
112
113 /**
114 * @test
115 */
116 public function getClassesPathReturnsPathToClasses() {
117 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'TYPO3.CMS.Core', PATH_typo3 . 'sysext/core/', FlowPackage::DIRECTORY_CLASSES);
118 $packageClassesPath = $package->getClassesPath();
119 $expected = $package->getPackagePath() . FlowPackage::DIRECTORY_CLASSES;
120 $this->assertEquals($expected, $packageClassesPath);
121 }
122
123 /**
124 * @test
125 */
126 public function getClassesPathReturnsNormalizedPathToClasses() {
127 $packagePath = 'vfs://Packages/Application/Acme/MyPackage/';
128 mkdir($packagePath, 0777, TRUE);
129 file_put_contents($packagePath . 'composer.json', '{"name": "acme/mypackage", "type": "flow-test"}');
130
131 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'Acme.MyPackage', $packagePath, 'no/trailing/slash');
132
133 $packageClassesPath = $package->getClassesPath();
134 $expected = $package->getPackagePath() . 'no/trailing/slash/';
135
136 $this->assertEquals($expected, $packageClassesPath);
137 }
138
139 /**
140 * @test
141 */
142 public function getPackageDocumentationsReturnsEmptyArrayIfDocumentationDirectoryDoesntExist() {
143 vfsStream::setup('testDirectory');
144
145 $packagePath = vfsStream::url('testDirectory') . '/';
146 file_put_contents($packagePath . 'composer.json', '{"name": "typo3/flow", "type": "flow-test"}');
147
148 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'TYPO3.Flow', $packagePath);
149 $documentations = $package->getPackageDocumentations();
150
151 $this->assertEquals(array(), $documentations);
152 }
153
154 /**
155 * @test
156 */
157 public function aPackageCanBeFlaggedAsProtected() {
158 $packagePath = 'vfs://Packages/Application/Vendor/Dummy/';
159 mkdir($packagePath, 0700, TRUE);
160 file_put_contents($packagePath . 'composer.json', '{"name": "vendor/dummy", "type": "flow-test"}');
161 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'Vendor.Dummy', $packagePath);
162
163 $this->assertFalse($package->isProtected());
164 $package->setProtected(TRUE);
165 $this->assertTrue($package->isProtected());
166 }
167
168 /**
169 * @test
170 */
171 public function isObjectManagementEnabledTellsIfObjectManagementShouldBeEnabledForThePackage() {
172 $packagePath = 'vfs://Packages/Application/Vendor/Dummy/';
173 mkdir($packagePath, 0700, TRUE);
174 file_put_contents($packagePath . 'composer.json', '{"name": "vendor/dummy", "type": "flow-test"}');
175 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'Vendor.Dummy', $packagePath);
176
177 $this->assertTrue($package->isObjectManagementEnabled());
178 }
179
180 /**
181 * @test
182 */
183 public function getClassFilesReturnsAListOfClassFilesOfThePackage() {
184 $packagePath = 'vfs://Packages/Application/Acme.MyPackage/';
185 mkdir($packagePath, 0777, TRUE);
186 file_put_contents($packagePath . 'composer.json', '{"name": "acme/mypackage", "type": "flow-test"}');
187
188 mkdir($packagePath . 'Classes/Acme/MyPackage/Controller', 0770, TRUE);
189 mkdir($packagePath . 'Classes/Acme/MyPackage/Domain/Model', 0770, TRUE);
190
191 file_put_contents($packagePath . 'Classes/Acme/MyPackage/Controller/FooController.php', '');
192 file_put_contents($packagePath . 'Classes/Acme/MyPackage/Domain/Model/Foo.php', '');
193 file_put_contents($packagePath . 'Classes/Acme/MyPackage/Domain/Model/Bar.php', '');
194
195 $expectedClassFilesArray = array(
196 'Acme\MyPackage\Controller\FooController' => 'Classes/Acme/MyPackage/Controller/FooController.php',
197 'Acme\MyPackage\Domain\Model\Foo' => 'Classes/Acme/MyPackage/Domain/Model/Foo.php',
198 'Acme\MyPackage\Domain\Model\Bar' => 'Classes/Acme/MyPackage/Domain/Model/Bar.php',
199 );
200
201 $package = new FlowPackage($this->getMock('TYPO3\CMS\Core\Package\PackageManager'), 'Acme.MyPackage', $packagePath, 'Classes');
202 $actualClassFilesArray = $package->getClassFiles();
203
204 $this->assertEquals($expectedClassFilesArray, $actualClassFilesArray);
205 }
206
207 }