InterfaceTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. * This file is part of php-token-stream.
  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. use PHPUnit\Framework\TestCase;
  11. class PHP_Token_InterfaceTest extends TestCase
  12. {
  13. /**
  14. * @var PHP_Token_CLASS
  15. */
  16. private $class;
  17. /**
  18. * @var PHP_Token_INTERFACE[]
  19. */
  20. private $interfaces;
  21. protected function setUp()
  22. {
  23. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source4.php');
  24. $i = 0;
  25. foreach ($ts as $token) {
  26. if ($token instanceof PHP_Token_CLASS) {
  27. $this->class = $token;
  28. } elseif ($token instanceof PHP_Token_INTERFACE) {
  29. $this->interfaces[$i] = $token;
  30. $i++;
  31. }
  32. }
  33. }
  34. /**
  35. * @covers PHP_Token_INTERFACE::getName
  36. */
  37. public function testGetName()
  38. {
  39. $this->assertEquals(
  40. 'iTemplate', $this->interfaces[0]->getName()
  41. );
  42. }
  43. /**
  44. * @covers PHP_Token_INTERFACE::getParent
  45. */
  46. public function testGetParentNotExists()
  47. {
  48. $this->assertFalse(
  49. $this->interfaces[0]->getParent()
  50. );
  51. }
  52. /**
  53. * @covers PHP_Token_INTERFACE::hasParent
  54. */
  55. public function testHasParentNotExists()
  56. {
  57. $this->assertFalse(
  58. $this->interfaces[0]->hasParent()
  59. );
  60. }
  61. /**
  62. * @covers PHP_Token_INTERFACE::getParent
  63. */
  64. public function testGetParentExists()
  65. {
  66. $this->assertEquals(
  67. 'a', $this->interfaces[2]->getParent()
  68. );
  69. }
  70. /**
  71. * @covers PHP_Token_INTERFACE::hasParent
  72. */
  73. public function testHasParentExists()
  74. {
  75. $this->assertTrue(
  76. $this->interfaces[2]->hasParent()
  77. );
  78. }
  79. /**
  80. * @covers PHP_Token_INTERFACE::getInterfaces
  81. */
  82. public function testGetInterfacesExists()
  83. {
  84. $this->assertEquals(
  85. ['b'],
  86. $this->class->getInterfaces()
  87. );
  88. }
  89. /**
  90. * @covers PHP_Token_INTERFACE::hasInterfaces
  91. */
  92. public function testHasInterfacesExists()
  93. {
  94. $this->assertTrue(
  95. $this->class->hasInterfaces()
  96. );
  97. }
  98. /**
  99. * @covers PHP_Token_INTERFACE::getPackage
  100. */
  101. public function testGetPackageNamespace()
  102. {
  103. $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php');
  104. foreach ($tokenStream as $token) {
  105. if ($token instanceof PHP_Token_INTERFACE) {
  106. $package = $token->getPackage();
  107. $this->assertSame('Foo\\Bar', $package['namespace']);
  108. }
  109. }
  110. }
  111. public function provideFilesWithClassesWithinMultipleNamespaces()
  112. {
  113. return [
  114. [TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingBraces.php'],
  115. [TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingNonBraceSyntax.php'],
  116. ];
  117. }
  118. /**
  119. * @dataProvider provideFilesWithClassesWithinMultipleNamespaces
  120. * @covers PHP_Token_INTERFACE::getPackage
  121. */
  122. public function testGetPackageNamespaceForFileWithMultipleNamespaces($filepath)
  123. {
  124. $tokenStream = new PHP_Token_Stream($filepath);
  125. $firstClassFound = false;
  126. foreach ($tokenStream as $token) {
  127. if ($firstClassFound === false && $token instanceof PHP_Token_INTERFACE) {
  128. $package = $token->getPackage();
  129. $this->assertSame('TestClassInBar', $token->getName());
  130. $this->assertSame('Foo\\Bar', $package['namespace']);
  131. $firstClassFound = true;
  132. continue;
  133. }
  134. // Secound class
  135. if ($token instanceof PHP_Token_INTERFACE) {
  136. $package = $token->getPackage();
  137. $this->assertSame('TestClassInBaz', $token->getName());
  138. $this->assertSame('Foo\\Baz', $package['namespace']);
  139. return;
  140. }
  141. }
  142. $this->fail('Searching for 2 classes failed');
  143. }
  144. public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces()
  145. {
  146. foreach ($this->interfaces as $token) {
  147. $package = $token->getPackage();
  148. $this->assertSame('', $package['namespace']);
  149. }
  150. }
  151. /**
  152. * @covers PHP_Token_INTERFACE::getPackage
  153. */
  154. public function testGetPackageNamespaceWhenExtentingFromNamespaceClass()
  155. {
  156. $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classExtendsNamespacedClass.php');
  157. $firstClassFound = false;
  158. foreach ($tokenStream as $token) {
  159. if ($firstClassFound === false && $token instanceof PHP_Token_INTERFACE) {
  160. $package = $token->getPackage();
  161. $this->assertSame('Baz', $token->getName());
  162. $this->assertSame('Foo\\Bar', $package['namespace']);
  163. $firstClassFound = true;
  164. continue;
  165. }
  166. if ($token instanceof PHP_Token_INTERFACE) {
  167. $package = $token->getPackage();
  168. $this->assertSame('Extender', $token->getName());
  169. $this->assertSame('Other\\Space', $package['namespace']);
  170. return;
  171. }
  172. }
  173. $this->fail('Searching for 2 classes failed');
  174. }
  175. }