AppTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 2.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Core;
  15. use Cake\Core\App;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\TestSuite\TestCase;
  19. use TestApp\Core\TestApp;
  20. /**
  21. * AppTest class
  22. *
  23. */
  24. class AppTest extends TestCase {
  25. /**
  26. * tearDown method
  27. *
  28. * @return void
  29. */
  30. public function tearDown() {
  31. parent::tearDown();
  32. Plugin::unload();
  33. }
  34. /**
  35. * testClassname
  36. *
  37. * $checkCake and $existsInCake are derived from the input parameters
  38. *
  39. * @param string $class Class name
  40. * @param string $type Class type
  41. * @param string $suffix Class suffix
  42. * @param bool $existsInBase Whether class exists in base.
  43. * @param mixed $expected Expected value.
  44. * @return void
  45. * @dataProvider classnameProvider
  46. */
  47. public function testClassname($class, $type, $suffix = '', $existsInBase = false, $expected = false) {
  48. Configure::write('App.namespace', 'TestApp');
  49. $i = 0;
  50. TestApp::$existsInBaseCallback = function($name, $namespace) use ($existsInBase, $class, $expected, &$i) {
  51. if ($i++ === 0) {
  52. return $existsInBase;
  53. }
  54. $checkCake = (!$existsInBase || strpos('.', $class));
  55. if ($checkCake) {
  56. return (bool)$expected;
  57. }
  58. return false;
  59. };
  60. $return = TestApp::classname($class, $type, $suffix);
  61. $this->assertSame($expected, $return);
  62. }
  63. /**
  64. * classnameProvider
  65. *
  66. * Return test permutations for testClassname method. Format:
  67. * classname
  68. * type
  69. * suffix
  70. * existsInBase (Base meaning App or plugin namespace)
  71. * expected return value
  72. *
  73. * @return void
  74. */
  75. public function classnameProvider() {
  76. return [
  77. ['Does', 'Not', 'Exist'],
  78. ['Exists', 'In', 'App', true, 'TestApp\In\ExistsApp'],
  79. ['Also/Exists', 'In', 'App', true, 'TestApp\In\Also\ExistsApp'],
  80. ['Also', 'Exists/In', 'App', true, 'TestApp\Exists\In\AlsoApp'],
  81. ['Also', 'Exists/In/Subfolder', 'App', true, 'TestApp\Exists\In\Subfolder\AlsoApp'],
  82. ['No', 'Suffix', '', true, 'TestApp\Suffix\No'],
  83. ['MyPlugin.Exists', 'In', 'Suffix', true, 'MyPlugin\In\ExistsSuffix'],
  84. ['MyPlugin.Also/Exists', 'In', 'Suffix', true, 'MyPlugin\In\Also\ExistsSuffix'],
  85. ['MyPlugin.Also', 'Exists/In', 'Suffix', true, 'MyPlugin\Exists\In\AlsoSuffix'],
  86. ['MyPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'MyPlugin\Exists\In\Subfolder\AlsoSuffix'],
  87. ['MyPlugin.No', 'Suffix', '', true, 'MyPlugin\Suffix\No'],
  88. ['Vend/MPlugin.Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\ExistsSuffix'],
  89. ['Vend/MPlugin.Also/Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\Also\ExistsSuffix'],
  90. ['Vend/MPlugin.Also', 'Exists/In', 'Suffix', true, 'Vend\MPlugin\Exists\In\AlsoSuffix'],
  91. ['Vend/MPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix'],
  92. ['Vend/MPlugin.No', 'Suffix', '', true, 'Vend\MPlugin\Suffix\No'],
  93. ['Exists', 'In', 'Cake', false, 'Cake\In\ExistsCake'],
  94. ['Also/Exists', 'In', 'Cake', false, 'Cake\In\Also\ExistsCake'],
  95. ['Also', 'Exists/In', 'Cake', false, 'Cake\Exists\In\AlsoCake'],
  96. ['Also', 'Exists/In/Subfolder', 'Cake', false, 'Cake\Exists\In\Subfolder\AlsoCake'],
  97. ['No', 'Suffix', '', false, 'Cake\Suffix\No'],
  98. // Realistic examples returning nothing
  99. ['App', 'Core', 'Suffix'],
  100. ['Auth', 'Controller/Component'],
  101. ['Unknown', 'Controller', 'Controller'],
  102. // Real examples returning classnames
  103. ['App', 'Core', '', false, 'Cake\Core\App'],
  104. ['Auth', 'Controller/Component', 'Component', false, 'Cake\Controller\Component\AuthComponent'],
  105. ['File', 'Cache/Engine', 'Engine', false, 'Cake\Cache\Engine\FileEngine'],
  106. ['Command', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\CommandTask'],
  107. ['Upgrade/Locations', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\Upgrade\LocationsTask'],
  108. ['Pages', 'Controller', 'Controller', true, 'TestApp\Controller\PagesController'],
  109. ];
  110. }
  111. /**
  112. * test path() with a plugin.
  113. *
  114. * @return void
  115. */
  116. public function testPathWithPlugins() {
  117. $basepath = TEST_APP . 'Plugin' . DS;
  118. Plugin::load('TestPlugin');
  119. $result = App::path('Controller', 'TestPlugin');
  120. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'src' . DS . 'Controller' . DS, $result[0]);
  121. Plugin::load('Company/TestPluginThree');
  122. $result = App::path('Controller', 'Company/TestPluginThree');
  123. $expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Controller' . DS;
  124. $this->assertPathEquals($expected, $result[0]);
  125. }
  126. /**
  127. * testCore method
  128. *
  129. * @return void
  130. */
  131. public function testCore() {
  132. $model = App::core('Model');
  133. $this->assertEquals(array(CAKE . 'Model' . DS), $model);
  134. $view = App::core('View');
  135. $this->assertEquals(array(CAKE . 'View' . DS), $view);
  136. $controller = App::core('Controller');
  137. $this->assertEquals(array(CAKE . 'Controller' . DS), $controller);
  138. $component = App::core('Controller/Component');
  139. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS), str_replace('/', DS, $component));
  140. $auth = App::core('Controller/Component/Auth');
  141. $this->assertEquals(array(CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS), str_replace('/', DS, $auth));
  142. $datasource = App::core('Model/Datasource');
  143. $this->assertEquals(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
  144. }
  145. }