ApcClassLoaderTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ClassLoader\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\ClassLoader\ApcClassLoader;
  13. use Symfony\Component\ClassLoader\ClassLoader;
  14. class ApcClassLoaderTest extends TestCase
  15. {
  16. protected function setUp()
  17. {
  18. if (!(filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) && filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN))) {
  19. $this->markTestSkipped('The apc extension is not enabled.');
  20. } else {
  21. apcu_clear_cache();
  22. }
  23. }
  24. protected function tearDown()
  25. {
  26. if (filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) && filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) {
  27. apcu_clear_cache();
  28. }
  29. }
  30. public function testConstructor()
  31. {
  32. $loader = new ClassLoader();
  33. $loader->addPrefix('Apc\Namespaced', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  34. $loader = new ApcClassLoader('test.prefix.', $loader);
  35. $this->assertEquals($loader->findFile('\Apc\Namespaced\FooBar'), apcu_fetch('test.prefix.\Apc\Namespaced\FooBar'), '__construct() takes a prefix as its first argument');
  36. }
  37. /**
  38. * @dataProvider getLoadClassTests
  39. */
  40. public function testLoadClass($className, $testClassName, $message)
  41. {
  42. $loader = new ClassLoader();
  43. $loader->addPrefix('Apc\Namespaced', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  44. $loader->addPrefix('Apc_Pearlike_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  45. $loader = new ApcClassLoader('test.prefix.', $loader);
  46. $loader->loadClass($testClassName);
  47. $this->assertTrue(class_exists($className), $message);
  48. }
  49. public function getLoadClassTests()
  50. {
  51. return array(
  52. array('\\Apc\\Namespaced\\Foo', 'Apc\\Namespaced\\Foo', '->loadClass() loads Apc\Namespaced\Foo class'),
  53. array('Apc_Pearlike_Foo', 'Apc_Pearlike_Foo', '->loadClass() loads Apc_Pearlike_Foo class'),
  54. );
  55. }
  56. /**
  57. * @dataProvider getLoadClassFromFallbackTests
  58. */
  59. public function testLoadClassFromFallback($className, $testClassName, $message)
  60. {
  61. $loader = new ClassLoader();
  62. $loader->addPrefix('Apc\Namespaced', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  63. $loader->addPrefix('Apc_Pearlike_', __DIR__.\DIRECTORY_SEPARATOR.'Fixtures');
  64. $loader->addPrefix('', array(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/fallback'));
  65. $loader = new ApcClassLoader('test.prefix.fallback', $loader);
  66. $loader->loadClass($testClassName);
  67. $this->assertTrue(class_exists($className), $message);
  68. }
  69. public function getLoadClassFromFallbackTests()
  70. {
  71. return array(
  72. array('\\Apc\\Namespaced\\Baz', 'Apc\\Namespaced\\Baz', '->loadClass() loads Apc\Namespaced\Baz class'),
  73. array('Apc_Pearlike_Baz', 'Apc_Pearlike_Baz', '->loadClass() loads Apc_Pearlike_Baz class'),
  74. array('\\Apc\\Namespaced\\FooBar', 'Apc\\Namespaced\\FooBar', '->loadClass() loads Apc\Namespaced\Baz class from fallback dir'),
  75. array('Apc_Pearlike_FooBar', 'Apc_Pearlike_FooBar', '->loadClass() loads Apc_Pearlike_Baz class from fallback dir'),
  76. );
  77. }
  78. /**
  79. * @dataProvider getLoadClassNamespaceCollisionTests
  80. */
  81. public function testLoadClassNamespaceCollision($namespaces, $className, $message)
  82. {
  83. $loader = new ClassLoader();
  84. $loader->addPrefixes($namespaces);
  85. $loader = new ApcClassLoader('test.prefix.collision.', $loader);
  86. $loader->loadClass($className);
  87. $this->assertTrue(class_exists($className), $message);
  88. }
  89. public function getLoadClassNamespaceCollisionTests()
  90. {
  91. return array(
  92. array(
  93. array(
  94. 'Apc\\NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
  95. 'Apc\\NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
  96. ),
  97. 'Apc\NamespaceCollision\A\Foo',
  98. '->loadClass() loads NamespaceCollision\A\Foo from alpha.',
  99. ),
  100. array(
  101. array(
  102. 'Apc\\NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
  103. 'Apc\\NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
  104. ),
  105. 'Apc\NamespaceCollision\A\Bar',
  106. '->loadClass() loads NamespaceCollision\A\Bar from alpha.',
  107. ),
  108. array(
  109. array(
  110. 'Apc\\NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
  111. 'Apc\\NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
  112. ),
  113. 'Apc\NamespaceCollision\A\B\Foo',
  114. '->loadClass() loads NamespaceCollision\A\B\Foo from beta.',
  115. ),
  116. array(
  117. array(
  118. 'Apc\\NamespaceCollision\\A\\B' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta',
  119. 'Apc\\NamespaceCollision\\A' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha',
  120. ),
  121. 'Apc\NamespaceCollision\A\B\Bar',
  122. '->loadClass() loads NamespaceCollision\A\B\Bar from beta.',
  123. ),
  124. );
  125. }
  126. /**
  127. * @dataProvider getLoadClassPrefixCollisionTests
  128. */
  129. public function testLoadClassPrefixCollision($prefixes, $className, $message)
  130. {
  131. $loader = new ClassLoader();
  132. $loader->addPrefixes($prefixes);
  133. $loader = new ApcClassLoader('test.prefix.collision.', $loader);
  134. $loader->loadClass($className);
  135. $this->assertTrue(class_exists($className), $message);
  136. }
  137. public function getLoadClassPrefixCollisionTests()
  138. {
  139. return array(
  140. array(
  141. array(
  142. 'ApcPrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
  143. 'ApcPrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
  144. ),
  145. 'ApcPrefixCollision_A_Foo',
  146. '->loadClass() loads ApcPrefixCollision_A_Foo from alpha.',
  147. ),
  148. array(
  149. array(
  150. 'ApcPrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
  151. 'ApcPrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
  152. ),
  153. 'ApcPrefixCollision_A_Bar',
  154. '->loadClass() loads ApcPrefixCollision_A_Bar from alpha.',
  155. ),
  156. array(
  157. array(
  158. 'ApcPrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
  159. 'ApcPrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
  160. ),
  161. 'ApcPrefixCollision_A_B_Foo',
  162. '->loadClass() loads ApcPrefixCollision_A_B_Foo from beta.',
  163. ),
  164. array(
  165. array(
  166. 'ApcPrefixCollision_A_B_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
  167. 'ApcPrefixCollision_A_' => __DIR__.\DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
  168. ),
  169. 'ApcPrefixCollision_A_B_Bar',
  170. '->loadClass() loads ApcPrefixCollision_A_B_Bar from beta.',
  171. ),
  172. );
  173. }
  174. }