PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Unit/Configuration/ConfigurationManagerTest.php

https://github.com/christianjul/FLOW3-Composer
PHP | 908 lines | 674 code | 128 blank | 106 comment | 6 complexity | bbe2456b1062d2e9e4312163180e3703 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. namespace TYPO3\FLOW3\Tests\Unit\Configuration;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License, either version 3 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * The TYPO3 project - inspiring people to share! *
  11. * */
  12. use TYPO3\FLOW3\Core\ApplicationContext;
  13. /**
  14. * Testcase for the configuration manager
  15. *
  16. */
  17. class ConfigurationManagerTest extends \TYPO3\FLOW3\Tests\UnitTestCase {
  18. /**
  19. * @test
  20. */
  21. public function getConfigurationForSettingsLoadsConfigurationIfNecessary() {
  22. $initialConfigurations = array(
  23. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS => array(),
  24. );
  25. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('loadConfiguration'), array(new ApplicationContext('Testing')), '', FALSE);
  26. $configurationManager->_set('configurations', $initialConfigurations);
  27. $configurationManager->expects($this->once())->method('loadConfiguration')->with(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS);
  28. $configurationManager->getConfiguration(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'Foo');
  29. }
  30. /**
  31. * @test
  32. */
  33. public function getConfigurationForTypeSettingsReturnsRespectiveConfigurationArray() {
  34. $expectedConfiguration = array('foo' => 'bar');
  35. $configurations = array(
  36. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS => array(
  37. 'SomePackage' => $expectedConfiguration
  38. )
  39. );
  40. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('dummy'), array(), '', FALSE);
  41. $configurationManager->_set('configurations', $configurations);
  42. $actualConfiguration = $configurationManager->getConfiguration(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'SomePackage');
  43. $this->assertSame($expectedConfiguration, $actualConfiguration);
  44. }
  45. /**
  46. * @test
  47. */
  48. public function getConfigurationForTypeSettingsLoadsConfigurationIfNecessary() {
  49. $packages = array('SomePackage' => $this->getMock('TYPO3\FLOW3\Package\Package', array(), array(), '', FALSE));
  50. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('loadConfiguration'), array(), '', FALSE);
  51. $configurationManager->_set('configurations', array(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS => array()));
  52. $configurationManager->setPackages($packages);
  53. $configurationManager->expects($this->once())->method('loadConfiguration')->with(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, $packages);
  54. $configurationManager->getConfiguration(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'SomePackage');
  55. }
  56. /**
  57. * @test
  58. */
  59. public function getConfigurationForTypeObjectLoadsConfiguration() {
  60. $packages = array('SomePackage' => $this->getMock('TYPO3\FLOW3\Package\Package', array(), array(), '', FALSE));
  61. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('loadConfiguration'), array(), '', FALSE);
  62. $configurationManager->_set('configurations', array(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_OBJECTS => array()));
  63. $configurationManager->setPackages($packages);
  64. $configurationManager->expects($this->once())->method('loadConfiguration')->with(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_OBJECTS, $packages);
  65. $configurationManager->getConfiguration(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_OBJECTS, 'SomePackage');
  66. }
  67. /**
  68. * @test
  69. */
  70. public function getConfigurationForRoutesAndCachesLoadsConfigurationIfNecessary() {
  71. $initialConfigurations = array(
  72. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES => array('foo' => 'bar'),
  73. );
  74. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('loadConfiguration'), array(), '', FALSE);
  75. $configurationManager->_set('configurations', $initialConfigurations);
  76. $configurationManager->expects($this->at(0))->method('loadConfiguration')->with(\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_CACHES);
  77. $configurationTypes = array(
  78. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES,
  79. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_CACHES
  80. );
  81. foreach ($configurationTypes as $configurationType) {
  82. $configurationManager->getConfiguration($configurationType);
  83. }
  84. }
  85. /**
  86. * @test
  87. */
  88. public function getConfigurationForRoutesAndCachesReturnsRespectiveConfigurationArray() {
  89. $expectedConfigurations = array(
  90. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES => array('routes'),
  91. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_CACHES => array('caches')
  92. );
  93. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('loadConfiguration'), array(), '', FALSE);
  94. $configurationManager->_set('configurations', $expectedConfigurations);
  95. $configurationManager->expects($this->never())->method('loadConfiguration');
  96. foreach ($expectedConfigurations as $configurationType => $expectedConfiguration) {
  97. $actualConfiguration = $configurationManager->getConfiguration($configurationType);
  98. $this->assertSame($expectedConfiguration, $actualConfiguration);
  99. }
  100. }
  101. /**
  102. * @test
  103. */
  104. public function loadConfigurationOverridesSettingsByContext() {
  105. $mockConfigurationSource = $this->getMock('TYPO3\FLOW3\Configuration\Source\YamlSource', array('load', 'save'));
  106. $mockConfigurationSource->expects($this->any())->method('load')->will($this->returnCallback(array($this, 'packageSettingsCallback')));
  107. $mockPackageA = $this->getMock('TYPO3\FLOW3\Package\Package', array(), array(), '', FALSE);
  108. $mockPackageA->expects($this->any())->method('getConfigurationPath')->will($this->returnValue('PackageA/Configuration/'));
  109. $mockPackageA->expects($this->any())->method('getPackageKey')->will($this->returnValue('PackageA'));
  110. $mockPackages = array(
  111. 'PackageA' => $mockPackageA,
  112. );
  113. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('postProcessConfiguration'), array(new ApplicationContext('Testing')));
  114. $configurationManager->_set('configurationSource', $mockConfigurationSource);
  115. $configurationManager->expects($this->once())->method('postProcessConfiguration');
  116. $configurationManager->_call('loadConfiguration', \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, $mockPackages);
  117. $actualConfigurations = $configurationManager->_get('configurations');
  118. $expectedSettings = array(
  119. 'foo' => 'D',
  120. 'bar' => 'A'
  121. );
  122. $this->assertSame($expectedSettings, $actualConfigurations[\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS]['PackageA']);
  123. }
  124. /**
  125. * @test
  126. */
  127. public function loadConfigurationOverridesGlobalSettingsByContext() {
  128. $configurationManager = $this->getConfigurationManagerWithFlow3Package('packageSettingsCallback', 'Testing/System1');
  129. $mockPackages = $this->getMockPackages();
  130. $configurationManager->_call('loadConfiguration', \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, $mockPackages);
  131. $actualConfigurations = $configurationManager->_get('configurations');
  132. $expectedSettings = array(
  133. 'TYPO3' => array(
  134. 'FLOW3' => array(
  135. 'ex1' => 'global',
  136. 'foo' => 'quux',
  137. 'example' => 'fromTestingSystem1',
  138. 'core' => array('context' => 'Testing/System1'),
  139. )
  140. )
  141. );
  142. $this->assertSame($expectedSettings, $actualConfigurations[\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS]);
  143. }
  144. /**
  145. * Callback for the above test.
  146. *
  147. */
  148. public function packageSettingsCallback() {
  149. $filenameAndPath = func_get_arg(0);
  150. $settingsFlow3 = array(
  151. 'TYPO3' => array(
  152. 'FLOW3' => array(
  153. 'ex1' => 'global',
  154. 'foo' => 'global stuff'
  155. )
  156. )
  157. );
  158. $settingsFlow3Testing = array(
  159. 'TYPO3' => array(
  160. 'FLOW3' => array(
  161. 'foo' => 'quux',
  162. 'example' => 'fromTesting'
  163. )
  164. )
  165. );
  166. $settingsFlow3TestingSystem1 = array(
  167. 'TYPO3' => array(
  168. 'FLOW3' => array(
  169. 'foo' => 'quux',
  170. 'example' => 'fromTestingSystem1'
  171. )
  172. )
  173. );
  174. $settingsA = array(
  175. 'PackageA' => array(
  176. 'foo' => 'A',
  177. 'bar' => 'A'
  178. )
  179. );
  180. $settingsB = array(
  181. 'PackageA' => array(
  182. 'bar' => 'B'
  183. ),
  184. 'PackageB' => array(
  185. 'foo' => 'B',
  186. 'bar' => 'B'
  187. )
  188. );
  189. $settingsC = array(
  190. 'PackageA' => array(
  191. 'bar' => 'C'
  192. ),
  193. 'PackageC' => array(
  194. 'baz' => 'C'
  195. )
  196. );
  197. $settingsATesting = array(
  198. 'PackageA' => array(
  199. 'foo' => 'D'
  200. )
  201. );
  202. $globalSettings = array(
  203. 'TYPO3' => arraY(
  204. 'FLOW3' => array(
  205. 'foo' => 'bar'
  206. )
  207. )
  208. );
  209. switch ($filenameAndPath) {
  210. case 'FLOW3/Configuration/Settings' : return $settingsFlow3;
  211. case 'FLOW3/Configuration/SomeContext/Settings' : return array();
  212. case 'FLOW3/Configuration/Testing/Settings' : return $settingsFlow3Testing;
  213. case 'FLOW3/Configuration/Testing/System1/Settings' : return $settingsFlow3TestingSystem1;
  214. case 'PackageA/Configuration/Settings' : return $settingsA;
  215. case 'PackageA/Configuration/SomeContext/Settings' : return array();
  216. case 'PackageA/Configuration/Testing/Settings' : return $settingsATesting;
  217. case 'PackageB/Configuration/Settings' : return $settingsB;
  218. case 'PackageB/Configuration/SomeContext/Settings' : return array();
  219. case 'PackageB/Configuration/Testing/Settings' : return array();
  220. case 'PackageC/Configuration/Settings' : return $settingsC;
  221. case 'PackageC/Configuration/SomeContext/Settings' : return array();
  222. case 'PackageC/Configuration/Testing/Settings' : return array();
  223. case FLOW3_PATH_CONFIGURATION . 'Settings' : return $globalSettings;
  224. case FLOW3_PATH_CONFIGURATION . 'SomeContext/Settings' : return array();
  225. case FLOW3_PATH_CONFIGURATION . 'Testing/Settings' : return array();
  226. case FLOW3_PATH_CONFIGURATION . 'Testing/System1/Settings' : return array();
  227. default:
  228. throw new \Exception('Unexpected filename: ' . $filenameAndPath);
  229. }
  230. }
  231. /**
  232. * @test
  233. */
  234. public function loadConfigurationForObjectsOverridesConfigurationByContext() {
  235. $configurationManager = $this->getConfigurationManagerWithFlow3Package('packageObjectsCallback', 'Testing/System1');
  236. $mockPackages = $this->getMockPackages();
  237. $configurationManager->_call('loadConfiguration', \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_OBJECTS, $mockPackages);
  238. $actualConfigurations = $configurationManager->_get('configurations');
  239. $expectedSettings = array(
  240. 'TYPO3.FLOW3' => array(
  241. 'TYPO3\FLOW3\SomeClass' => array(
  242. 'className' => 'Bar',
  243. 'configPackageObjects' => 'correct',
  244. 'configGlobalObjects' => 'correct',
  245. 'configPackageContextObjects' => 'correct',
  246. 'configGlobalContextObjects' => 'correct',
  247. 'configPackageSubContextObjects' => 'correct',
  248. 'configGlobalSubContextObjects' => 'correct',
  249. )
  250. )
  251. );
  252. $this->assertSame($expectedSettings, $actualConfigurations[\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_OBJECTS]);
  253. }
  254. /**
  255. * Callback for the above test.
  256. */
  257. public function packageObjectsCallback() {
  258. $filenameAndPath = func_get_arg(0);
  259. // We expect the following overriding order:
  260. // - $packageObjects
  261. // - $globalObjects
  262. // - $packageContextObjects
  263. // - $globalContextObjects
  264. // - $packageSubContextObjects
  265. // - $globalSubContextObjects
  266. $packageObjects = array(
  267. 'TYPO3\FLOW3\SomeClass' => array(
  268. 'className' => 'Foo',
  269. 'configPackageObjects' => 'correct',
  270. 'configGlobalObjects' => 'overriddenWronglyFromPackageObjects',
  271. 'configPackageContextObjects' => 'overriddenWronglyFromPackageObjects',
  272. 'configGlobalContextObjects' => 'overriddenWronglyFromPackageObjects',
  273. 'configPackageSubContextObjects' => 'overriddenWronglyFromPackageObjects',
  274. 'configGlobalSubContextObjects' => 'overriddenWronglyFromPackageObjects',
  275. )
  276. );
  277. $globalObjects = array(
  278. 'TYPO3\FLOW3\SomeClass' => array(
  279. 'configGlobalObjects' => 'correct',
  280. 'configPackageContextObjects' => 'overriddenWronglyFromGlobalObjects',
  281. 'configGlobalContextObjects' => 'overriddenWronglyFromGlobalObjects',
  282. 'configPackageSubContextObjects' => 'overriddenWronglyFromGlobalObjects',
  283. 'configGlobalSubContextObjects' => 'overriddenWronglyFromGlobalObjects',
  284. )
  285. );
  286. $packageContextObjects = array(
  287. 'TYPO3\FLOW3\SomeClass' => array(
  288. 'className' => 'Bar',
  289. 'configPackageContextObjects' => 'correct',
  290. 'configGlobalContextObjects' => 'overriddenWronglyFromPackageContextObjects',
  291. 'configPackageSubContextObjects' => 'overriddenWronglyFromPackageContextObjects',
  292. 'configGlobalSubContextObjects' => 'overriddenWronglyFromPackageContextObjects',
  293. )
  294. );
  295. $globalContextObjects = array(
  296. 'TYPO3\FLOW3\SomeClass' => array(
  297. 'configGlobalContextObjects' => 'correct',
  298. 'configPackageSubContextObjects' => 'overriddenWronglyFromGlobalContextObjects',
  299. 'configGlobalSubContextObjects' => 'overriddenWronglyFromGlobalContextObjects',
  300. )
  301. );
  302. $packageSubContextObjects = array(
  303. 'TYPO3\FLOW3\SomeClass' => array(
  304. 'configPackageSubContextObjects' => 'correct',
  305. 'configGlobalSubContextObjects' => 'overriddenWronglyFromPackageSubContextObjects',
  306. )
  307. );
  308. $globalSubContextObjects = array(
  309. 'TYPO3\FLOW3\SomeClass' => array(
  310. 'configGlobalSubContextObjects' => 'correct',
  311. )
  312. );
  313. switch ($filenameAndPath) {
  314. case 'FLOW3/Configuration/Objects' : return $packageObjects;
  315. case 'FLOW3/Configuration/Testing/Objects' : return $packageContextObjects;
  316. case 'FLOW3/Configuration/Testing/System1/Objects' : return $packageSubContextObjects;
  317. case FLOW3_PATH_CONFIGURATION . 'Objects' : return $globalObjects;
  318. case FLOW3_PATH_CONFIGURATION . 'Testing/Objects' : return $globalContextObjects;
  319. case FLOW3_PATH_CONFIGURATION . 'Testing/System1/Objects' : return $globalSubContextObjects;
  320. default:
  321. throw new \Exception('Unexpected filename: ' . $filenameAndPath);
  322. }
  323. }
  324. /**
  325. * @test
  326. */
  327. public function loadConfigurationForCachesOverridesConfigurationByContext() {
  328. $configurationManager = $this->getConfigurationManagerWithFlow3Package('packageCachesCallback', 'Testing/System1');
  329. $mockPackages = $this->getMockPackages();
  330. $configurationManager->_call('loadConfiguration', \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_CACHES, $mockPackages);
  331. $actualConfigurations = $configurationManager->_get('configurations');
  332. $expectedCachesConfiguration = array(
  333. 'TYPO3_FLOW3_SomeCache' => array(
  334. 'configPackageCaches' => 'correct',
  335. 'configGlobalCaches' => 'correct',
  336. 'configPackageContextCaches' => 'correct',
  337. 'configGlobalContextCaches' => 'correct',
  338. 'configPackageSubContextCaches' => 'correct',
  339. 'configGlobalSubContextCaches' => 'correct',
  340. )
  341. );
  342. $this->assertSame($expectedCachesConfiguration, $actualConfigurations[\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_CACHES]);
  343. }
  344. /**
  345. * Callback for the above test.
  346. */
  347. public function packageCachesCallback() {
  348. $filenameAndPath = func_get_arg(0);
  349. // We expect the following overriding order:
  350. // - $packageCaches
  351. // - $globalCaches
  352. // - $packageContextCaches
  353. // - $globalContextCaches
  354. // - $packageSubContextCaches
  355. // - $globalSubContextCaches
  356. $packageCaches = array(
  357. 'TYPO3_FLOW3_SomeCache' => array(
  358. 'configPackageCaches' => 'correct',
  359. 'configGlobalCaches' => 'overriddenWronglyFromPackageCaches',
  360. 'configPackageContextCaches' => 'overriddenWronglyFromPackageCaches',
  361. 'configGlobalContextCaches' => 'overriddenWronglyFromPackageCaches',
  362. 'configPackageSubContextCaches' => 'overriddenWronglyFromPackageCaches',
  363. 'configGlobalSubContextCaches' => 'overriddenWronglyFromPackageCaches',
  364. )
  365. );
  366. $globalCaches = array(
  367. 'TYPO3_FLOW3_SomeCache' => array(
  368. 'configGlobalCaches' => 'correct',
  369. 'configPackageContextCaches' => 'overriddenWronglyFromGlobalCaches',
  370. 'configGlobalContextCaches' => 'overriddenWronglyFromGlobalCaches',
  371. 'configPackageSubContextCaches' => 'overriddenWronglyFromGlobalCaches',
  372. 'configGlobalSubContextCaches' => 'overriddenWronglyFromGlobalCaches',
  373. )
  374. );
  375. $packageContextCaches = array(
  376. 'TYPO3_FLOW3_SomeCache' => array(
  377. 'configPackageContextCaches' => 'correct',
  378. 'configGlobalContextCaches' => 'overriddenWronglyFromPackageContextCaches',
  379. 'configPackageSubContextCaches' => 'overriddenWronglyFromPackageContextCaches',
  380. 'configGlobalSubContextCaches' => 'overriddenWronglyFromPackageContextCaches',
  381. )
  382. );
  383. $globalContextCaches = array(
  384. 'TYPO3_FLOW3_SomeCache' => array(
  385. 'configGlobalContextCaches' => 'correct',
  386. 'configPackageSubContextCaches' => 'overriddenWronglyFromGlobalContextCaches',
  387. 'configGlobalSubContextCaches' => 'overriddenWronglyFromGlobalContextCaches',
  388. )
  389. );
  390. $packageSubContextCaches = array(
  391. 'TYPO3_FLOW3_SomeCache' => array(
  392. 'configPackageSubContextCaches' => 'correct',
  393. 'configGlobalSubContextCaches' => 'overriddenWronglyFromPackageSubContextCaches',
  394. )
  395. );
  396. $globalSubContextCaches = array(
  397. 'TYPO3_FLOW3_SomeCache' => array(
  398. 'configGlobalSubContextCaches' => 'correct',
  399. )
  400. );
  401. switch ($filenameAndPath) {
  402. case 'FLOW3/Configuration/Caches' : return $packageCaches;
  403. case 'FLOW3/Configuration/Testing/Caches' : return $packageContextCaches;
  404. case 'FLOW3/Configuration/Testing/System1/Caches' : return $packageSubContextCaches;
  405. case FLOW3_PATH_CONFIGURATION . 'Caches' : return $globalCaches;
  406. case FLOW3_PATH_CONFIGURATION . 'Testing/Caches' : return $globalContextCaches;
  407. case FLOW3_PATH_CONFIGURATION . 'Testing/System1/Caches' : return $globalSubContextCaches;
  408. default:
  409. throw new \Exception('Unexpected filename: ' . $filenameAndPath);
  410. }
  411. }
  412. /**
  413. * @test
  414. */
  415. public function loadConfigurationCacheLoadsConfigurationsFromCacheIfACacheFileExists() {
  416. \vfsStreamWrapper::register();
  417. \vfsStreamWrapper::setRoot(new \vfsStreamDirectory('FLOW3'));
  418. $configurationsCode = <<< "EOD"
  419. <?php
  420. return array('bar' => 'touched');
  421. ?>
  422. EOD;
  423. $includeCachedConfigurationsPathAndFilename = \vfsStream::url('FLOW3/IncludeCachedConfigurations.php');
  424. file_put_contents($includeCachedConfigurationsPathAndFilename, $configurationsCode);
  425. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('postProcessConfiguration'), array(), '', FALSE);
  426. $configurationManager->_set('includeCachedConfigurationsPathAndFilename', 'notfound.php');
  427. $configurationManager->_set('configurations', array('foo' => 'untouched'));
  428. $configurationManager->_call('loadConfigurationCache');
  429. $this->assertSame(array('foo' => 'untouched'), $configurationManager->_get('configurations'));
  430. $configurationManager->_set('includeCachedConfigurationsPathAndFilename', $includeCachedConfigurationsPathAndFilename);
  431. $configurationManager->_call('loadConfigurationCache');
  432. $this->assertSame(array('bar' => 'touched'), $configurationManager->_get('configurations'));
  433. }
  434. /**
  435. * @test
  436. */
  437. public function loadConfigurationCorrectlyMergesSettings() {
  438. $mockConfigurationSource = $this->getMock('TYPO3\FLOW3\Configuration\Source\YamlSource', array('load', 'save'));
  439. $mockConfigurationSource->expects($this->any())->method('load')->will($this->returnCallback(array($this, 'packageSettingsCallback')));
  440. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('postProcessConfiguration'), array(new ApplicationContext('Testing')));
  441. $configurationManager->_set('configurationSource', $mockConfigurationSource);
  442. $configurationManager->_call('loadConfiguration', \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, array());
  443. }
  444. /**
  445. * @test
  446. */
  447. public function saveConfigurationCacheSavesTheCurrentConfigurationAsPhpCode() {
  448. \vfsStreamWrapper::register();
  449. \vfsStreamWrapper::setRoot(new \vfsStreamDirectory('FLOW3'));
  450. mkdir(\vfsStream::url('FLOW3/Configuration'));
  451. $temporaryDirectoryPath = \vfsStream::url('FLOW3/TemporaryDirectory') . '/';
  452. $includeCachedConfigurationsPathAndFilename = \vfsStream::url('FLOW3/Configuration/IncludeCachedConfigurations.php');
  453. $mockConfigurations = array(
  454. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES => array('routes'),
  455. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_CACHES => array('caches'),
  456. \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS => array('settings' => array('foo' => 'bar'))
  457. );
  458. $mockEnvironment = $this->getMock('TYPO3\FLOW3\Utility\Environment', array('getPathToTemporaryDirectory'), array(), '', FALSE);
  459. $mockEnvironment->expects($this->once())->method('getPathToTemporaryDirectory')->will($this->returnValue($temporaryDirectoryPath));
  460. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('postProcessConfiguration'), array(), '', FALSE);
  461. $configurationManager->injectEnvironment($mockEnvironment);
  462. $configurationManager->_set('includeCachedConfigurationsPathAndFilename', $includeCachedConfigurationsPathAndFilename);
  463. $configurationManager->_set('context', 'FooContext');
  464. $configurationManager->_set('configurations', $mockConfigurations);
  465. $configurationManager->_call('saveConfigurationCache');
  466. $expectedInclusionCode = <<< "EOD"
  467. <?php
  468. if (FLOW3_PATH_ROOT !== 'XXX' || !file_exists('vfs://FLOW3/TemporaryDirectory/Configuration/FooContextConfigurations.php')) {
  469. unlink(__FILE__);
  470. return array();
  471. }
  472. return require 'vfs://FLOW3/TemporaryDirectory/Configuration/FooContextConfigurations.php';
  473. ?>
  474. EOD;
  475. $expectedInclusionCode = str_replace('XXX', FLOW3_PATH_ROOT, $expectedInclusionCode);
  476. $this->assertTrue(file_exists($temporaryDirectoryPath . 'Configuration'));
  477. $this->assertStringEqualsFile($includeCachedConfigurationsPathAndFilename, $expectedInclusionCode);
  478. $this->assertFileExists($temporaryDirectoryPath . 'Configuration/FooContextConfigurations.php');
  479. $this->assertSame($mockConfigurations, require($temporaryDirectoryPath . 'Configuration/FooContextConfigurations.php'));
  480. }
  481. /**
  482. * @test
  483. */
  484. public function postProcessConfigurationReplacesConstantMarkersByRealGlobalConstants() {
  485. $settings = array(
  486. 'foo' => 'bar',
  487. 'baz' => '%PHP_VERSION%',
  488. 'inspiring' => array(
  489. 'people' => array(
  490. 'to' => '%FLOW3_PATH_ROOT%'
  491. )
  492. )
  493. );
  494. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('dummy'), array(), '', FALSE);
  495. $configurationManager->_callRef('postProcessConfiguration', $settings);
  496. $this->assertSame(PHP_VERSION, $settings['baz']);
  497. $this->assertSame(FLOW3_PATH_ROOT, $settings['inspiring']['people']['to']);
  498. }
  499. /**
  500. * We expect that the context specific routes are loaded *first*
  501. *
  502. * @test
  503. */
  504. public function loadConfigurationForRoutesLoadsContextSpecificRoutesFirst() {
  505. $configurationManager = $this->getConfigurationManagerWithFlow3Package('packageRoutesCallback', 'Testing/System1');
  506. $mockPackages = $this->getMockPackages();
  507. $configurationManager->_call('loadConfiguration', \TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES, $mockPackages);
  508. $actualConfigurations = $configurationManager->_get('configurations');
  509. $expectedRoutesConfiguration = array(
  510. array(
  511. 'name' => 'GlobalSubContextRoute1',
  512. 'uriPattern' => 'globalSubContextRoute1'
  513. ),
  514. array(
  515. 'name' => 'GlobalSubContextRoute2',
  516. 'uriPattern' => 'globalSubContextRoute2'
  517. ),
  518. // BEGIN SUBROUTES
  519. array(
  520. 'name' => 'GlobalContextRoute1 :: PackageSubContextRoute1',
  521. 'uriPattern' => 'globalContextRoute1/packageSubContextRoute1'
  522. ),
  523. array(
  524. 'name' => 'GlobalContextRoute1 :: PackageSubContextRoute2',
  525. 'uriPattern' => 'globalContextRoute1/packageSubContextRoute2'
  526. ),
  527. array(
  528. 'name' => 'GlobalContextRoute1 :: PackageContextRoute1',
  529. 'uriPattern' => 'globalContextRoute1/packageContextRoute1'
  530. ),
  531. array(
  532. 'name' => 'GlobalContextRoute1 :: PackageContextRoute2',
  533. 'uriPattern' => 'globalContextRoute1/packageContextRoute2'
  534. ),
  535. array(
  536. 'name' => 'GlobalContextRoute1 :: PackageRoute1',
  537. 'uriPattern' => 'globalContextRoute1/packageRoute1'
  538. ),
  539. array(
  540. 'name' => 'GlobalContextRoute1 :: PackageRoute2',
  541. 'uriPattern' => 'globalContextRoute1/packageRoute2'
  542. ),
  543. // END SUBROUTES
  544. array(
  545. 'name' => 'GlobalContextRoute2',
  546. 'uriPattern' => 'globalContextRoute2'
  547. ),
  548. array(
  549. 'name' => 'GlobalRoute1',
  550. 'uriPattern' => 'globalRoute1'
  551. ),
  552. array(
  553. 'name' => 'GlobalRoute2',
  554. 'uriPattern' => 'globalRoute2'
  555. )
  556. );
  557. $this->assertSame($expectedRoutesConfiguration, $actualConfigurations[\TYPO3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES]);
  558. }
  559. /**
  560. * Callback for the above test.
  561. */
  562. public function packageRoutesCallback() {
  563. $filenameAndPath = func_get_arg(0);
  564. // The routes from the innermost context should be added FIRST, such that
  565. // they take precedence over more generic contexts
  566. $packageSubContextRoutes = array(
  567. array(
  568. 'name' => 'PackageSubContextRoute1',
  569. 'uriPattern' => 'packageSubContextRoute1'
  570. ),
  571. array(
  572. 'name' => 'PackageSubContextRoute2',
  573. 'uriPattern' => 'packageSubContextRoute2'
  574. ),
  575. );
  576. $packageContextRoutes = array(
  577. array(
  578. 'name' => 'PackageContextRoute1',
  579. 'uriPattern' => 'packageContextRoute1'
  580. ),
  581. array(
  582. 'name' => 'PackageContextRoute2',
  583. 'uriPattern' => 'packageContextRoute2'
  584. )
  585. );
  586. $packageRoutes = array(
  587. array(
  588. 'name' => 'PackageRoute1',
  589. 'uriPattern' => 'packageRoute1'
  590. ),
  591. array(
  592. 'name' => 'PackageRoute2',
  593. 'uriPattern' => 'packageRoute2'
  594. )
  595. );
  596. $globalSubContextRoutes = array(
  597. array(
  598. 'name' => 'GlobalSubContextRoute1',
  599. 'uriPattern' => 'globalSubContextRoute1'
  600. ),
  601. array(
  602. 'name' => 'GlobalSubContextRoute2',
  603. 'uriPattern' => 'globalSubContextRoute2'
  604. )
  605. );
  606. $globalContextRoutes = array(
  607. array(
  608. 'name' => 'GlobalContextRoute1',
  609. 'uriPattern' => 'globalContextRoute1/<PackageSubroutes>',
  610. 'subRoutes' => array(
  611. 'PackageSubroutes' => array(
  612. 'package' => 'TYPO3.FLOW3'
  613. )
  614. ),
  615. ),
  616. array(
  617. 'name' => 'GlobalContextRoute2',
  618. 'uriPattern' => 'globalContextRoute2'
  619. )
  620. );
  621. $globalRoutes = array(
  622. array(
  623. 'name' => 'GlobalRoute1',
  624. 'uriPattern' => 'globalRoute1'
  625. ),
  626. array(
  627. 'name' => 'GlobalRoute2',
  628. 'uriPattern' => 'globalRoute2'
  629. )
  630. );
  631. switch ($filenameAndPath) {
  632. case 'FLOW3/Configuration/Routes' : return $packageRoutes;
  633. case 'FLOW3/Configuration/Testing/Routes' : return $packageContextRoutes;
  634. case 'FLOW3/Configuration/Testing/System1/Routes' : return $packageSubContextRoutes;
  635. case FLOW3_PATH_CONFIGURATION . 'Routes' : return $globalRoutes;
  636. case FLOW3_PATH_CONFIGURATION . 'Testing/Routes' : return $globalContextRoutes;
  637. case FLOW3_PATH_CONFIGURATION . 'Testing/System1/Routes' : return $globalSubContextRoutes;
  638. default:
  639. throw new \Exception('Unexpected filename: ' . $filenameAndPath);
  640. }
  641. }
  642. /**
  643. * @test
  644. * @expectedException \TYPO3\FLOW3\Configuration\Exception\ParseErrorException
  645. */
  646. public function mergeRoutesWithSubRoutesThrowsExceptionIfRouteRefersToNonExistingOrInactivePackages() {
  647. $routesConfiguration = array(
  648. array(
  649. 'name' => 'Welcome',
  650. 'uriPattern' => '<WelcomeSubroutes>',
  651. 'subRoutes' => array(
  652. 'WelcomeSubroutes' => array(
  653. 'package' => 'Welcome'
  654. )
  655. )
  656. )
  657. );
  658. $subRoutesConfiguration = array();
  659. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('dummy'), array(new ApplicationContext('Testing')));
  660. $configurationManager->_callRef('mergeRoutesWithSubRoutes', $routesConfiguration, $subRoutesConfiguration);
  661. }
  662. /**
  663. * @test
  664. */
  665. public function buildSubrouteConfigurationsCorrectlyMergesRoutes() {
  666. $routesConfiguration = array(
  667. array(
  668. 'name' => 'Welcome',
  669. 'uriPattern' => '<WelcomeSubroutes>',
  670. 'defaults' => array(
  671. '@package' => 'Welcome'
  672. ),
  673. 'subRoutes' => array(
  674. 'WelcomeSubroutes' => array(
  675. 'package' => 'Welcome'
  676. )
  677. ),
  678. 'routeParts' => array(
  679. 'foo' => array(
  680. 'bar' => 'baz',
  681. 'baz' => 'Xyz'
  682. )
  683. ),
  684. 'toLowerCase' => TRUE
  685. )
  686. );
  687. $subRoutesConfiguration = array(
  688. array(
  689. 'name' => 'Standard route',
  690. 'uriPattern' => 'flow3/welcome',
  691. 'defaults' => array(
  692. '@package' => 'OverriddenPackage',
  693. '@controller' => 'Standard',
  694. '@action' => 'index'
  695. )
  696. ),
  697. array(
  698. 'name' => 'Redirect',
  699. 'uriPattern' => '',
  700. 'defaults' => array(
  701. '@controller' => 'Standard',
  702. '@action' => 'redirect'
  703. ),
  704. 'routeParts' => array(
  705. 'foo' => array(
  706. 'bar' => 'overridden',
  707. 'new' => 'ZZZ'
  708. )
  709. ),
  710. 'toLowerCase' => FALSE,
  711. 'appendExceedingArguments' => TRUE
  712. )
  713. );
  714. $expectedResult = array(
  715. array(
  716. 'name' => 'Welcome :: Standard route',
  717. 'uriPattern' => 'flow3/welcome',
  718. 'defaults' => array(
  719. '@package' => 'OverriddenPackage',
  720. '@controller' => 'Standard',
  721. '@action' => 'index'
  722. ),
  723. 'routeParts' => array(
  724. 'foo' => array(
  725. 'bar' => 'baz',
  726. 'baz' => 'Xyz'
  727. )
  728. ),
  729. 'toLowerCase' => TRUE,
  730. ),
  731. array(
  732. 'name' => 'Welcome :: Redirect',
  733. 'uriPattern' => '',
  734. 'defaults' => array(
  735. '@package' => 'Welcome',
  736. '@controller' => 'Standard',
  737. '@action' => 'redirect'
  738. ),
  739. 'routeParts' => array(
  740. 'foo' => array(
  741. 'bar' => 'overridden',
  742. 'baz' => 'Xyz',
  743. 'new' => 'ZZZ'
  744. )
  745. ),
  746. 'toLowerCase' => FALSE,
  747. 'appendExceedingArguments' => TRUE
  748. )
  749. );
  750. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('dummy'), array(new ApplicationContext('Testing')));
  751. $actualResult = $configurationManager->_call('buildSubrouteConfigurations', $routesConfiguration, $subRoutesConfiguration, 'WelcomeSubroutes');
  752. $this->assertEquals($expectedResult, $actualResult);
  753. }
  754. /**
  755. * @param string $configurationSourceCallbackName
  756. * @param string $contextName
  757. * @return \TYPO3\FLOW3\Configuration\ConfigurationManager
  758. */
  759. protected function getConfigurationManagerWithFlow3Package($configurationSourceCallbackName, $contextName) {
  760. $mockConfigurationSource = $this->getMock('TYPO3\FLOW3\Configuration\Source\YamlSource', array('load', 'save'));
  761. $mockConfigurationSource->expects($this->any())->method('load')->will($this->returnCallback(array($this, $configurationSourceCallbackName)));
  762. $configurationManager = $this->getAccessibleMock('TYPO3\FLOW3\Configuration\ConfigurationManager', array('postProcessConfiguration'), array(new ApplicationContext($contextName)));
  763. $configurationManager->_set('configurationSource', $mockConfigurationSource);
  764. $configurationManager->expects($this->once())->method('postProcessConfiguration');
  765. return $configurationManager;
  766. }
  767. /**
  768. * @return array
  769. */
  770. protected function getMockPackages() {
  771. $mockPackageFlow3 = $this->getMock('TYPO3\FLOW3\Package\Package', array(), array(), '', FALSE);
  772. $mockPackageFlow3->expects($this->any())->method('getConfigurationPath')->will($this->returnValue('FLOW3/Configuration/'));
  773. $mockPackageFlow3->expects($this->any())->method('getPackageKey')->will($this->returnValue('TYPO3.FLOW3'));
  774. $mockPackages = array(
  775. 'TYPO3.FLOW3' => $mockPackageFlow3
  776. );
  777. return $mockPackages;
  778. }
  779. }
  780. ?>