ClassFileLocatorTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  80. $found = false;
  81. foreach ($locator as $file) {
  82. $classes = $file->getClasses();
  83. foreach ($classes as $class) {
  84. if (strpos($class, '\\', 1)) {
  85. $found = true;
  86. }
  87. }
  88. }
  89. $this->assertTrue($found);
  90. }
  91. public function testIterationShouldInjectClassInFoundItems()
  92. {
  93. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  94. $found = false;
  95. foreach ($locator as $file) {
  96. $classes = $file->getClasses();
  97. foreach ($classes as $class) {
  98. $found = true;
  99. break;
  100. }
  101. }
  102. $this->assertTrue($found);
  103. }
  104. public function testIterationShouldFindMultipleClassesInMultipleNamespacesInSinglePhpFile()
  105. {
  106. $locator = new Zend_File_ClassFileLocator(dirname(__FILE__));
  107. $foundFirst = false;
  108. $foundSecond = false;
  109. $foundThird = false;
  110. $foundFourth = false;
  111. foreach ($locator as $file) {
  112. if (preg_match('/MultipleClassesInMultipleNamespaces\.php$/', $file->getFilename())) {
  113. $classes = $file->getClasses();
  114. foreach ($classes as $class) {
  115. if ($class === 'ZendTest\File\TestAsset\LocatorShouldFindFirstClass') {
  116. $foundFirst = true;
  117. }
  118. if ($class === 'ZendTest\File\TestAsset\LocatorShouldFindSecondClass') {
  119. $foundSecond = true;
  120. }
  121. if ($class === 'ZendTest\File\TestAsset\SecondTestNamespace\LocatorShouldFindThirdClass') {
  122. $foundThird = true;
  123. }
  124. if ($class === 'ZendTest\File\TestAsset\SecondTestNamespace\LocatorShouldFindFourthClass') {
  125. $foundFourth = true;
  126. }
  127. }
  128. }
  129. }
  130. $this->assertTrue($foundFirst);
  131. $this->assertTrue($foundSecond);
  132. $this->assertTrue($foundThird);
  133. $this->assertTrue($foundFourth);
  134. }
  135. }
  136. if (PHPUnit_MAIN_METHOD == 'Zend_File_ClassFileLocatorTest::main') {
  137. Zend_File_ClassFileLocatorTest::main();
  138. }