ClassFileLocatorTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. if (!defined('PHPUnit_MAIN_METHOD')) {
  22. define('PHPUnit_MAIN_METHOD', 'Zend_File_ClassFileLocatorTest::main');
  23. }
  24. require_once 'Zend/File/ClassFileLocator.php';
  25. /**
  26. * Test class for Zend_File_ClassFileLocator
  27. *
  28. * @category Zend
  29. * @package Zend_File
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_File
  34. */
  35. class Zend_File_ClassFileLocatorTest extends PHPUnit_Framework_TestCase
  36. {
  37. public function testConstructorThrowsInvalidArgumentExceptionForInvalidStringDirectory()
  38. {
  39. $this->setExpectedException('InvalidArgumentException');
  40. $locator = new Zend_File_ClassFileLocator('__foo__');
  41. }
  42. public function testConstructorThrowsInvalidArgumentExceptionForNonDirectoryIteratorArgument()
  43. {
  44. $iterator = new ArrayIterator(array());
  45. $this->setExpectedException('InvalidArgumentException');
  46. $locator = new Zend_File_ClassFileLocator($iterator);
  47. }
  48. public function testIterationShouldReturnOnlyPhpFiles()
  49. {
  50. if (version_compare(PHP_VERSION, '5.3', 'lt')) {
  51. $this->markTestSkipped('Test can only be run under 5.3 or later');
  52. }
  53. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  54. foreach ($locator as $file) {
  55. $this->assertRegexp('/\.php$/', $file->getFilename());
  56. }
  57. }
  58. public function testIterationShouldReturnOnlyPhpFilesContainingClasses()
  59. {
  60. if (version_compare(PHP_VERSION, '5.3', 'lt')) {
  61. $this->markTestSkipped('Test can only be run under 5.3 or later');
  62. }
  63. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  64. $found = false;
  65. foreach ($locator as $file) {
  66. if (preg_match('/locator-should-skip-this\.php$/', $file->getFilename())) {
  67. $found = true;
  68. }
  69. }
  70. $this->assertFalse($found, "Found PHP file not containing a class?");
  71. }
  72. public function testIterationShouldReturnInterfaces()
  73. {
  74. if (version_compare(PHP_VERSION, '5.3', 'lt')) {
  75. $this->markTestSkipped('Test can only be run under 5.3 or later');
  76. }
  77. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  78. $found = false;
  79. foreach ($locator as $file) {
  80. if (preg_match('/LocatorShouldFindThis\.php$/', $file->getFilename())) {
  81. $found = true;
  82. }
  83. }
  84. $this->assertTrue($found, "Locator skipped an interface?");
  85. }
  86. public function testIterationShouldInjectNamespaceInFoundItems()
  87. {
  88. if (version_compare(PHP_VERSION, '5.3', 'lt')) {
  89. $this->markTestSkipped('Test can only be run under 5.3 or later');
  90. }
  91. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  92. $found = false;
  93. foreach ($locator as $file) {
  94. $classes = $file->getClasses();
  95. foreach ($classes as $class) {
  96. if (strpos($class, '\\', 1)) {
  97. $found = true;
  98. }
  99. }
  100. }
  101. $this->assertTrue($found);
  102. }
  103. public function testIterationShouldInjectClassInFoundItems()
  104. {
  105. if (version_compare(PHP_VERSION, '5.3', 'lt')) {
  106. $this->markTestSkipped('Test can only be run under 5.3 or later');
  107. }
  108. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  109. $found = false;
  110. foreach ($locator as $file) {
  111. $classes = $file->getClasses();
  112. foreach ($classes as $class) {
  113. $found = true;
  114. break;
  115. }
  116. }
  117. $this->assertTrue($found);
  118. }
  119. public function testIterationShouldFindMultipleClassesInMultipleNamespacesInSinglePhpFile()
  120. {
  121. if (version_compare(PHP_VERSION, '5.3', 'lt')) {
  122. $this->markTestSkipped('Test can only be run under 5.3 or later');
  123. }
  124. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  125. $foundFirst = false;
  126. $foundSecond = false;
  127. $foundThird = false;
  128. $foundFourth = false;
  129. foreach ($locator as $file) {
  130. if (preg_match('/MultipleClassesInMultipleNamespaces\.php$/', $file->getFilename())) {
  131. $classes = $file->getClasses();
  132. foreach ($classes as $class) {
  133. if ($class === 'ZendTest\File\TestAsset\LocatorShouldFindFirstClass') {
  134. $foundFirst = true;
  135. }
  136. if ($class === 'ZendTest\File\TestAsset\LocatorShouldFindSecondClass') {
  137. $foundSecond = true;
  138. }
  139. if ($class === 'ZendTest\File\TestAsset\SecondTestNamespace\LocatorShouldFindThirdClass') {
  140. $foundThird = true;
  141. }
  142. if ($class === 'ZendTest\File\TestAsset\SecondTestNamespace\LocatorShouldFindFourthClass') {
  143. $foundFourth = true;
  144. }
  145. }
  146. }
  147. }
  148. $this->assertTrue($foundFirst);
  149. $this->assertTrue($foundSecond);
  150. $this->assertTrue($foundThird);
  151. $this->assertTrue($foundFourth);
  152. }
  153. }
  154. if (PHPUnit_MAIN_METHOD == 'Zend_File_ClassFileLocatorTest::main') {
  155. Zend_File_ClassFileLocatorTest::main();
  156. }