ClassFileLocatorTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-2011 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-2011 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. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  51. foreach ($locator as $file) {
  52. $this->assertRegexp('/\.php$/', $file->getFilename());
  53. }
  54. }
  55. public function testIterationShouldReturnOnlyPhpFilesContainingClasses()
  56. {
  57. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  58. $found = false;
  59. foreach ($locator as $file) {
  60. if (preg_match('/locator-should-skip-this\.php$/', $file->getFilename())) {
  61. $found = true;
  62. }
  63. }
  64. $this->assertFalse($found, "Found PHP file not containing a class?");
  65. }
  66. public function testIterationShouldReturnInterfaces()
  67. {
  68. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  69. $found = false;
  70. foreach ($locator as $file) {
  71. if (preg_match('/LocatorShouldFindThis\.php$/', $file->getFilename())) {
  72. $found = true;
  73. }
  74. }
  75. $this->assertTrue($found, "Locator skipped an interface?");
  76. }
  77. public function testIterationShouldInjectNamespaceInFoundItems()
  78. {
  79. if (version_compare(PHP_VERSION, '5.3', 'lt')) {
  80. $this->markTestSkipped('Test can only be run under 5.3 or later');
  81. }
  82. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  83. $found = false;
  84. foreach ($locator as $file) {
  85. $classes = $file->getClasses();
  86. foreach ($classes as $class) {
  87. if (strpos($class, '\\', 1)) {
  88. $found = true;
  89. }
  90. }
  91. }
  92. $this->assertTrue($found);
  93. }
  94. public function testIterationShouldInjectClassInFoundItems()
  95. {
  96. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  97. $found = false;
  98. foreach ($locator as $file) {
  99. $classes = $file->getClasses();
  100. foreach ($classes as $class) {
  101. $found = true;
  102. break;
  103. }
  104. }
  105. $this->assertTrue($found);
  106. }
  107. public function testIterationShouldFindMultipleClassesInMultipleNamespacesInSinglePhpFile()
  108. {
  109. if (version_compare(PHP_VERSION, '5.3', 'lt')) {
  110. $this->markTestSkipped('Test can only be run under 5.3 or later');
  111. }
  112. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  113. $foundFirst = false;
  114. $foundSecond = false;
  115. $foundThird = false;
  116. $foundFourth = false;
  117. foreach ($locator as $file) {
  118. if (preg_match('/MultipleClassesInMultipleNamespaces\.php$/', $file->getFilename())) {
  119. $classes = $file->getClasses();
  120. foreach ($classes as $class) {
  121. if ($class === 'ZendTest\File\TestAsset\LocatorShouldFindFirstClass') {
  122. $foundFirst = true;
  123. }
  124. if ($class === 'ZendTest\File\TestAsset\LocatorShouldFindSecondClass') {
  125. $foundSecond = true;
  126. }
  127. if ($class === 'ZendTest\File\TestAsset\SecondTestNamespace\LocatorShouldFindThirdClass') {
  128. $foundThird = true;
  129. }
  130. if ($class === 'ZendTest\File\TestAsset\SecondTestNamespace\LocatorShouldFindFourthClass') {
  131. $foundFourth = true;
  132. }
  133. }
  134. }
  135. }
  136. $this->assertTrue($foundFirst);
  137. $this->assertTrue($foundSecond);
  138. $this->assertTrue($foundThird);
  139. $this->assertTrue($foundFourth);
  140. }
  141. }
  142. if (PHPUnit_MAIN_METHOD == 'Zend_File_ClassFileLocatorTest::main') {
  143. Zend_File_ClassFileLocatorTest::main();
  144. }