[!!!][FOLLOWUP][TASK] Simplify PackageManagement
[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\CMS\Core\Package\Package as Package;
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 protected function setUp() {
26 vfsStream::setup('Packages');
27 }
28
29 /**
30 * @test
31 * @expectedException \TYPO3\CMS\Core\Package\Exception\InvalidPackagePathException
32 */
33 public function constructThrowsPackageDoesNotExistException() {
34 $packageManagerMock = $this->getMock(\TYPO3\CMS\Core\Package\PackageManager::class);
35 $packageManagerMock->expects($this->any())->method('isPackageKeyValid')->willReturn(TRUE);
36 new Package($packageManagerMock, 'Vendor.TestPackage', './ThisPackageSurelyDoesNotExist');
37 }
38
39 /**
40 */
41 public function validPackageKeys() {
42 return array(
43 array('Doctrine.DBAL'),
44 array('TYPO3.CMS'),
45 array('My.Own.TwitterPackage'),
46 array('GoFor.IT'),
47 array('Symfony.Symfony')
48 );
49 }
50
51 /**
52 * @test
53 * @dataProvider validPackageKeys
54 */
55 public function constructAcceptsValidPackageKeys($packageKey) {
56 $packagePath = 'vfs://Packages/' . str_replace('\\', '/', $packageKey) . '/';
57 mkdir($packagePath, 0777, TRUE);
58 file_put_contents($packagePath . 'composer.json', '{"name": "' . $packageKey . '", "type": "flow-test"}');
59 file_put_contents($packagePath . 'ext_emconf.php', '');
60
61 $packageManagerMock = $this->getMock(\TYPO3\CMS\Core\Package\PackageManager::class);
62 $packageManagerMock->expects($this->any())->method('isPackageKeyValid')->willReturn(TRUE);
63 $package = new Package($packageManagerMock, $packageKey, $packagePath);
64 $this->assertEquals($packageKey, $package->getPackageKey());
65 }
66
67 /**
68 */
69 public function invalidPackageKeys() {
70 return array(
71 array('TYPO3..Flow'),
72 array('RobertLemke.Flow. Twitter'),
73 array('Schalke*4')
74 );
75 }
76
77 /**
78 * @test
79 * @dataProvider invalidPackageKeys
80 * @expectedException \TYPO3\CMS\Core\Package\Exception\InvalidPackageKeyException
81 */
82 public function constructRejectsInvalidPackageKeys($packageKey) {
83 $packagePath = 'vfs://Packages/' . str_replace('\\', '/', $packageKey) . '/';
84 mkdir($packagePath, 0777, TRUE);
85
86 $packageManagerMock = $this->getMock(\TYPO3\CMS\Core\Package\PackageManager::class);
87 new Package($packageManagerMock, $packageKey, $packagePath);
88 }
89
90 /**
91 * @test
92 */
93 public function getNamespaceReturnsThePhpNamespaceCorrespondingToThePackageKey() {
94 $packagePath = 'vfs://Packages/Application/Acme.MyPackage/';
95 mkdir($packagePath, 0777, TRUE);
96 file_put_contents($packagePath . 'composer.json', '{"name": "acme/mypackage", "type": "flow-test"}');
97 file_put_contents($packagePath . 'ext_emconf.php', '');
98
99 $packageManagerMock = $this->getMock(\TYPO3\CMS\Core\Package\PackageManager::class);
100 $packageManagerMock->expects($this->any())->method('isPackageKeyValid')->willReturn(TRUE);
101 $package = new Package($packageManagerMock, 'Acme.MyPackage', $packagePath);
102 $this->assertEquals('Acme\\MyPackage', $package->getNamespace());
103 }
104
105 /**
106 * @test
107 */
108 public function getClassesPathReturnsPathToClasses() {
109 $packageManagerMock = $this->getMock(\TYPO3\CMS\Core\Package\PackageManager::class);
110 $packageManagerMock->expects($this->any())->method('isPackageKeyValid')->willReturn(TRUE);
111 $package = new Package($packageManagerMock, 'core', PATH_typo3 . 'sysext/core/');
112 $packageClassesPath = $package->getClassesPath();
113 $expected = $package->getPackagePath() . Package::DIRECTORY_CLASSES;
114 $this->assertEquals($expected, $packageClassesPath);
115 }
116
117 /**
118 * @test
119 */
120 public function aPackageCanBeFlaggedAsProtected() {
121 $packagePath = 'vfs://Packages/Application/Vendor/Dummy/';
122 mkdir($packagePath, 0700, TRUE);
123 file_put_contents($packagePath . 'composer.json', '{"name": "vendor/dummy", "type": "flow-test"}');
124 file_put_contents($packagePath . 'ext_emconf.php', '');
125
126 $packageManagerMock = $this->getMock(\TYPO3\CMS\Core\Package\PackageManager::class);
127 $packageManagerMock->expects($this->any())->method('isPackageKeyValid')->willReturn(TRUE);
128 $package = new Package($packageManagerMock, 'Vendor.Dummy', $packagePath);
129
130 $this->assertFalse($package->isProtected());
131 $package->setProtected(TRUE);
132 $this->assertTrue($package->isProtected());
133 }
134
135 }