AppTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  27. * tearDown method
  28. *
  29. * @return void
  30. */
  31. public function tearDown()
  32. {
  33. parent::tearDown();
  34. Plugin::unload();
  35. }
  36. /**
  37. * testClassname
  38. *
  39. * $checkCake and $existsInCake are derived from the input parameters
  40. *
  41. * @param string $class Class name
  42. * @param string $type Class type
  43. * @param string $suffix Class suffix
  44. * @param bool $existsInBase Whether class exists in base.
  45. * @param mixed $expected Expected value.
  46. * @return void
  47. * @dataProvider classnameProvider
  48. */
  49. public function testClassname($class, $type, $suffix = '', $existsInBase = false, $expected = false)
  50. {
  51. Configure::write('App.namespace', 'TestApp');
  52. $i = 0;
  53. TestApp::$existsInBaseCallback = function ($name, $namespace) use ($existsInBase, $class, $expected, &$i) {
  54. if ($i++ === 0) {
  55. return $existsInBase;
  56. }
  57. $checkCake = (!$existsInBase || strpos('.', $class));
  58. if ($checkCake) {
  59. return (bool)$expected;
  60. }
  61. return false;
  62. };
  63. $return = TestApp::classname($class, $type, $suffix);
  64. $this->assertSame($expected, $return);
  65. }
  66. /**
  67. * classnameProvider
  68. *
  69. * Return test permutations for testClassname method. Format:
  70. * classname
  71. * type
  72. * suffix
  73. * existsInBase (Base meaning App or plugin namespace)
  74. * expected return value
  75. *
  76. * @return void
  77. */
  78. public function classnameProvider()
  79. {
  80. return [
  81. ['Does', 'Not', 'Exist'],
  82. ['Exists', 'In', 'App', true, 'TestApp\In\ExistsApp'],
  83. ['Also/Exists', 'In', 'App', true, 'TestApp\In\Also\ExistsApp'],
  84. ['Also', 'Exists/In', 'App', true, 'TestApp\Exists\In\AlsoApp'],
  85. ['Also', 'Exists/In/Subfolder', 'App', true, 'TestApp\Exists\In\Subfolder\AlsoApp'],
  86. ['No', 'Suffix', '', true, 'TestApp\Suffix\No'],
  87. ['MyPlugin.Exists', 'In', 'Suffix', true, 'MyPlugin\In\ExistsSuffix'],
  88. ['MyPlugin.Also/Exists', 'In', 'Suffix', true, 'MyPlugin\In\Also\ExistsSuffix'],
  89. ['MyPlugin.Also', 'Exists/In', 'Suffix', true, 'MyPlugin\Exists\In\AlsoSuffix'],
  90. ['MyPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'MyPlugin\Exists\In\Subfolder\AlsoSuffix'],
  91. ['MyPlugin.No', 'Suffix', '', true, 'MyPlugin\Suffix\No'],
  92. ['Vend/MPlugin.Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\ExistsSuffix'],
  93. ['Vend/MPlugin.Also/Exists', 'In', 'Suffix', true, 'Vend\MPlugin\In\Also\ExistsSuffix'],
  94. ['Vend/MPlugin.Also', 'Exists/In', 'Suffix', true, 'Vend\MPlugin\Exists\In\AlsoSuffix'],
  95. ['Vend/MPlugin.Also', 'Exists/In/Subfolder', 'Suffix', true, 'Vend\MPlugin\Exists\In\Subfolder\AlsoSuffix'],
  96. ['Vend/MPlugin.No', 'Suffix', '', true, 'Vend\MPlugin\Suffix\No'],
  97. ['Exists', 'In', 'Cake', false, 'Cake\In\ExistsCake'],
  98. ['Also/Exists', 'In', 'Cake', false, 'Cake\In\Also\ExistsCake'],
  99. ['Also', 'Exists/In', 'Cake', false, 'Cake\Exists\In\AlsoCake'],
  100. ['Also', 'Exists/In/Subfolder', 'Cake', false, 'Cake\Exists\In\Subfolder\AlsoCake'],
  101. ['No', 'Suffix', '', false, 'Cake\Suffix\No'],
  102. // Realistic examples returning nothing
  103. ['App', 'Core', 'Suffix'],
  104. ['Auth', 'Controller/Component'],
  105. ['Unknown', 'Controller', 'Controller'],
  106. // Real examples returning classnames
  107. ['App', 'Core', '', false, 'Cake\Core\App'],
  108. ['Auth', 'Controller/Component', 'Component', false, 'Cake\Controller\Component\AuthComponent'],
  109. ['File', 'Cache/Engine', 'Engine', false, 'Cake\Cache\Engine\FileEngine'],
  110. ['Command', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\CommandTask'],
  111. ['Upgrade/Locations', 'Shell/Task', 'Task', false, 'Cake\Shell\Task\Upgrade\LocationsTask'],
  112. ['Pages', 'Controller', 'Controller', true, 'TestApp\Controller\PagesController'],
  113. ];
  114. }
  115. /**
  116. * test path() with a plugin.
  117. *
  118. * @return void
  119. */
  120. public function testPathWithPlugins()
  121. {
  122. $basepath = TEST_APP . 'Plugin' . DS;
  123. Plugin::load('TestPlugin');
  124. $result = App::path('Controller', 'TestPlugin');
  125. $this->assertPathEquals($basepath . 'TestPlugin' . DS . 'src' . DS . 'Controller' . DS, $result[0]);
  126. Plugin::load('Company/TestPluginThree');
  127. $result = App::path('Controller', 'Company/TestPluginThree');
  128. $expected = $basepath . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS . 'Controller' . DS;
  129. $this->assertPathEquals($expected, $result[0]);
  130. }
  131. /**
  132. * testCore method
  133. *
  134. * @return void
  135. */
  136. public function testCore()
  137. {
  138. $model = App::core('Model');
  139. $this->assertEquals([CAKE . 'Model' . DS], $model);
  140. $view = App::core('View');
  141. $this->assertEquals([CAKE . 'View' . DS], $view);
  142. $controller = App::core('Controller');
  143. $this->assertEquals([CAKE . 'Controller' . DS], $controller);
  144. $component = App::core('Controller/Component');
  145. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS], str_replace('/', DS, $component));
  146. $auth = App::core('Controller/Component/Auth');
  147. $this->assertEquals([CAKE . 'Controller' . DS . 'Component' . DS . 'Auth' . DS], str_replace('/', DS, $auth));
  148. $datasource = App::core('Model/Datasource');
  149. $this->assertEquals([CAKE . 'Model' . DS . 'Datasource' . DS], str_replace('/', DS, $datasource));
  150. }
  151. }