Bez popisu

InterfaceTest.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /*
  3. * This file is part of the PHP_TokenStream package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Tests for the PHP_Token_INTERFACE class.
  12. *
  13. * @package PHP_TokenStream
  14. * @subpackage Tests
  15. * @author Sebastian Bergmann <sebastian@phpunit.de>
  16. * @author Laurent Laville <pear@laurent-laville.org>
  17. * @copyright Sebastian Bergmann <sebastian@phpunit.de>
  18. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  19. * @version Release: @package_version@
  20. * @link http://github.com/sebastianbergmann/php-token-stream/
  21. * @since Class available since Release 1.0.0
  22. */
  23. class PHP_Token_InterfaceTest extends PHPUnit_Framework_TestCase
  24. {
  25. protected $class;
  26. protected $interfaces;
  27. protected function setUp()
  28. {
  29. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source4.php');
  30. $i = 0;
  31. foreach ($ts as $token) {
  32. if ($token instanceof PHP_Token_CLASS) {
  33. $this->class = $token;
  34. }
  35. elseif ($token instanceof PHP_Token_INTERFACE) {
  36. $this->interfaces[$i] = $token;
  37. $i++;
  38. }
  39. }
  40. }
  41. /**
  42. * @covers PHP_Token_INTERFACE::getName
  43. */
  44. public function testGetName()
  45. {
  46. $this->assertEquals(
  47. 'iTemplate', $this->interfaces[0]->getName()
  48. );
  49. }
  50. /**
  51. * @covers PHP_Token_INTERFACE::getParent
  52. */
  53. public function testGetParentNotExists()
  54. {
  55. $this->assertFalse(
  56. $this->interfaces[0]->getParent()
  57. );
  58. }
  59. /**
  60. * @covers PHP_Token_INTERFACE::hasParent
  61. */
  62. public function testHasParentNotExists()
  63. {
  64. $this->assertFalse(
  65. $this->interfaces[0]->hasParent()
  66. );
  67. }
  68. /**
  69. * @covers PHP_Token_INTERFACE::getParent
  70. */
  71. public function testGetParentExists()
  72. {
  73. $this->assertEquals(
  74. 'a', $this->interfaces[2]->getParent()
  75. );
  76. }
  77. /**
  78. * @covers PHP_Token_INTERFACE::hasParent
  79. */
  80. public function testHasParentExists()
  81. {
  82. $this->assertTrue(
  83. $this->interfaces[2]->hasParent()
  84. );
  85. }
  86. /**
  87. * @covers PHP_Token_INTERFACE::getInterfaces
  88. */
  89. public function testGetInterfacesExists()
  90. {
  91. $this->assertEquals(
  92. array('b'),
  93. $this->class->getInterfaces()
  94. );
  95. }
  96. /**
  97. * @covers PHP_Token_INTERFACE::hasInterfaces
  98. */
  99. public function testHasInterfacesExists()
  100. {
  101. $this->assertTrue(
  102. $this->class->hasInterfaces()
  103. );
  104. }
  105. /**
  106. * @covers PHP_Token_INTERFACE::getPackage
  107. */
  108. public function testGetPackageNamespace() {
  109. $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php');
  110. foreach($tokenStream as $token) {
  111. if($token instanceOf PHP_Token_INTERFACE) {
  112. $package = $token->getPackage();
  113. $this->assertSame('Foo\\Bar', $package['namespace']);
  114. }
  115. }
  116. }
  117. public function provideFilesWithClassesWithinMultipleNamespaces() {
  118. return array(
  119. array(TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingBraces.php'),
  120. array(TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingNonBraceSyntax.php'),
  121. );
  122. }
  123. /**
  124. * @dataProvider provideFilesWithClassesWithinMultipleNamespaces
  125. * @covers PHP_Token_INTERFACE::getPackage
  126. */
  127. public function testGetPackageNamespaceForFileWithMultipleNamespaces($filepath) {
  128. $tokenStream = new PHP_Token_Stream($filepath);
  129. $firstClassFound = false;
  130. foreach($tokenStream as $token) {
  131. if($firstClassFound === false && $token instanceOf PHP_Token_INTERFACE) {
  132. $package = $token->getPackage();
  133. $this->assertSame('TestClassInBar', $token->getName());
  134. $this->assertSame('Foo\\Bar', $package['namespace']);
  135. $firstClassFound = true;
  136. continue;
  137. }
  138. // Secound class
  139. if($token instanceOf PHP_Token_INTERFACE) {
  140. $package = $token->getPackage();
  141. $this->assertSame('TestClassInBaz', $token->getName());
  142. $this->assertSame('Foo\\Baz', $package['namespace']);
  143. return;
  144. }
  145. }
  146. $this->fail("Seachring for 2 classes failed");
  147. }
  148. public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces() {
  149. foreach($this->interfaces as $token) {
  150. $package = $token->getPackage();
  151. $this->assertSame("", $package['namespace']);
  152. }
  153. }
  154. /**
  155. * @covers PHP_Token_INTERFACE::getPackage
  156. */
  157. public function testGetPackageNamespaceWhenExtentingFromNamespaceClass() {
  158. $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classExtendsNamespacedClass.php');
  159. $firstClassFound = false;
  160. foreach($tokenStream as $token) {
  161. if($firstClassFound === false && $token instanceOf PHP_Token_INTERFACE) {
  162. $package = $token->getPackage();
  163. $this->assertSame('Baz', $token->getName());
  164. $this->assertSame('Foo\\Bar', $package['namespace']);
  165. $firstClassFound = true;
  166. continue;
  167. }
  168. if($token instanceOf PHP_Token_INTERFACE) {
  169. $package = $token->getPackage();
  170. $this->assertSame('Extender', $token->getName());
  171. $this->assertSame('Other\\Space', $package['namespace']);
  172. return;
  173. }
  174. }
  175. $this->fail("Searching for 2 classes failed");
  176. }
  177. }