ApcUniversalClassLoaderTest.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Tests\Component\ClassLoader;
  11. use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
  12. class ApcUniversalClassLoaderTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected function setUp()
  15. {
  16. if (!extension_loaded('apc')) {
  17. $this->markTestSkipped('The apc extension is not available.');
  18. }
  19. if (!(ini_get('apc.enabled') && ini_get('apc.enable_cli'))) {
  20. $this->markTestSkipped('The apc extension is available, but not enabled.');
  21. } else {
  22. apc_clear_cache('user');
  23. }
  24. }
  25. protected function tearDown()
  26. {
  27. if (ini_get('apc.enabled') && ini_get('apc.enable_cli')) {
  28. apc_clear_cache('user');
  29. }
  30. }
  31. public function testConstructor()
  32. {
  33. $loader = new ApcUniversalClassLoader('test.prefix.');
  34. $loader->registerNamespace('Apc\Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  35. $this->assertEquals($loader->findFile('\Apc\Namespaced\FooBar'), apc_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 ApcUniversalClassLoader('test.prefix.');
  43. $loader->registerNamespace('Apc\Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  44. $loader->registerPrefix('Apc_Pearlike_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  45. $loader->loadClass($testClassName);
  46. $this->assertTrue(class_exists($className), $message);
  47. }
  48. public function getLoadClassTests()
  49. {
  50. return array(
  51. array('\\Apc\\Namespaced\\Foo', '\\Apc\\Namespaced\\Foo', '->loadClass() loads Apc\Namespaced\Foo class'),
  52. array('Apc_Pearlike_Foo', 'Apc_Pearlike_Foo', '->loadClass() loads Apc_Pearlike_Foo class'),
  53. array('\\Apc\\Namespaced\\Bar', '\\Apc\\Namespaced\\Bar', '->loadClass() loads Apc\Namespaced\Bar class with a leading slash'),
  54. array('Apc_Pearlike_Bar', '\\Apc_Pearlike_Bar', '->loadClass() loads Apc_Pearlike_Bar class with a leading slash'),
  55. );
  56. }
  57. /**
  58. * @dataProvider getLoadClassFromFallbackTests
  59. */
  60. public function testLoadClassFromFallback($className, $testClassName, $message)
  61. {
  62. $loader = new ApcUniversalClassLoader('test.prefix.fallback');
  63. $loader->registerNamespace('Apc\Namespaced', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  64. $loader->registerPrefix('Apc_Pearlike_', __DIR__.DIRECTORY_SEPARATOR.'Fixtures');
  65. $loader->registerNamespaceFallbacks(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/fallback'));
  66. $loader->registerPrefixFallbacks(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/fallback'));
  67. $loader->loadClass($testClassName);
  68. $this->assertTrue(class_exists($className), $message);
  69. }
  70. public function getLoadClassFromFallbackTests()
  71. {
  72. return array(
  73. array('\\Apc\\Namespaced\\Baz', '\\Apc\\Namespaced\\Baz', '->loadClass() loads Apc\Namespaced\Baz class'),
  74. array('Apc_Pearlike_Baz', 'Apc_Pearlike_Baz', '->loadClass() loads Apc_Pearlike_Baz class'),
  75. array('\\Apc\\Namespaced\\FooBar', '\\Apc\\Namespaced\\FooBar', '->loadClass() loads Apc\Namespaced\Baz class from fallback dir'),
  76. array('Apc_Pearlike_FooBar', 'Apc_Pearlike_FooBar', '->loadClass() loads Apc_Pearlike_Baz class from fallback dir'),
  77. );
  78. }
  79. /**
  80. * @dataProvider getLoadClassNamespaceCollisionTests
  81. */
  82. public function testLoadClassNamespaceCollision($namespaces, $className, $message)
  83. {
  84. $loader = new ApcUniversalClassLoader('test.prefix.collision.');
  85. $loader->registerNamespaces($namespaces);
  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 ApcUniversalClassLoader('test.prefix.collision.');
  132. $loader->registerPrefixes($prefixes);
  133. $loader->loadClass($className);
  134. $this->assertTrue(class_exists($className), $message);
  135. }
  136. public function getLoadClassPrefixCollisionTests()
  137. {
  138. return array(
  139. array(
  140. array(
  141. 'ApcPrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
  142. 'ApcPrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
  143. ),
  144. 'ApcPrefixCollision_A_Foo',
  145. '->loadClass() loads ApcPrefixCollision_A_Foo from alpha.',
  146. ),
  147. array(
  148. array(
  149. 'ApcPrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
  150. 'ApcPrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
  151. ),
  152. 'ApcPrefixCollision_A_Bar',
  153. '->loadClass() loads ApcPrefixCollision_A_Bar from alpha.',
  154. ),
  155. array(
  156. array(
  157. 'ApcPrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
  158. 'ApcPrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
  159. ),
  160. 'ApcPrefixCollision_A_B_Foo',
  161. '->loadClass() loads ApcPrefixCollision_A_B_Foo from beta.',
  162. ),
  163. array(
  164. array(
  165. 'ApcPrefixCollision_A_B_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/beta/Apc',
  166. 'ApcPrefixCollision_A_' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/Apc/alpha/Apc',
  167. ),
  168. 'ApcPrefixCollision_A_B_Bar',
  169. '->loadClass() loads ApcPrefixCollision_A_B_Bar from beta.',
  170. ),
  171. );
  172. }
  173. }