Nessuna descrizione

InterfaceTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. public function testGetName()
  35. {
  36. $this->assertEquals(
  37. 'iTemplate', $this->interfaces[0]->getName()
  38. );
  39. }
  40. public function testGetParentNotExists()
  41. {
  42. $this->assertFalse(
  43. $this->interfaces[0]->getParent()
  44. );
  45. }
  46. public function testHasParentNotExists()
  47. {
  48. $this->assertFalse(
  49. $this->interfaces[0]->hasParent()
  50. );
  51. }
  52. public function testGetParentExists()
  53. {
  54. $this->assertEquals(
  55. 'a', $this->interfaces[2]->getParent()
  56. );
  57. }
  58. public function testHasParentExists()
  59. {
  60. $this->assertTrue(
  61. $this->interfaces[2]->hasParent()
  62. );
  63. }
  64. public function testGetInterfacesExists()
  65. {
  66. $this->assertEquals(
  67. ['b'],
  68. $this->class->getInterfaces()
  69. );
  70. }
  71. public function testHasInterfacesExists()
  72. {
  73. $this->assertTrue(
  74. $this->class->hasInterfaces()
  75. );
  76. }
  77. public function testGetPackageNamespace()
  78. {
  79. foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php') as $token) {
  80. if ($token instanceof PHP_Token_INTERFACE) {
  81. $package = $token->getPackage();
  82. $this->assertSame('Foo\\Bar', $package['namespace']);
  83. }
  84. }
  85. }
  86. public function provideFilesWithClassesWithinMultipleNamespaces()
  87. {
  88. return [
  89. [TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingBraces.php'],
  90. [TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingNonBraceSyntax.php'],
  91. ];
  92. }
  93. /**
  94. * @dataProvider provideFilesWithClassesWithinMultipleNamespaces
  95. */
  96. public function testGetPackageNamespaceForFileWithMultipleNamespaces($filepath)
  97. {
  98. $tokenStream = new PHP_Token_Stream($filepath);
  99. $firstClassFound = false;
  100. foreach ($tokenStream as $token) {
  101. if ($firstClassFound === false && $token instanceof PHP_Token_INTERFACE) {
  102. $package = $token->getPackage();
  103. $this->assertSame('TestClassInBar', $token->getName());
  104. $this->assertSame('Foo\\Bar', $package['namespace']);
  105. $firstClassFound = true;
  106. continue;
  107. }
  108. // Secound class
  109. if ($token instanceof PHP_Token_INTERFACE) {
  110. $package = $token->getPackage();
  111. $this->assertSame('TestClassInBaz', $token->getName());
  112. $this->assertSame('Foo\\Baz', $package['namespace']);
  113. return;
  114. }
  115. }
  116. $this->fail('Searching for 2 classes failed');
  117. }
  118. public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces()
  119. {
  120. foreach ($this->interfaces as $token) {
  121. $package = $token->getPackage();
  122. $this->assertSame('', $package['namespace']);
  123. }
  124. }
  125. public function testGetPackageNamespaceWhenExtentingFromNamespaceClass()
  126. {
  127. $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classExtendsNamespacedClass.php');
  128. $firstClassFound = false;
  129. foreach ($tokenStream as $token) {
  130. if ($firstClassFound === false && $token instanceof PHP_Token_INTERFACE) {
  131. $package = $token->getPackage();
  132. $this->assertSame('Baz', $token->getName());
  133. $this->assertSame('Foo\\Bar', $package['namespace']);
  134. $firstClassFound = true;
  135. continue;
  136. }
  137. if ($token instanceof PHP_Token_INTERFACE) {
  138. $package = $token->getPackage();
  139. $this->assertSame('Extender', $token->getName());
  140. $this->assertSame('Other\\Space', $package['namespace']);
  141. return;
  142. }
  143. }
  144. $this->fail('Searching for 2 classes failed');
  145. }
  146. }