TraitUseAdaptationTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Name;
  6. use PhpParser\Node\Stmt;
  7. use PhpParser\Node\Stmt\Class_;
  8. use PHPUnit\Framework\TestCase;
  9. class TraitUseAdaptationTest extends TestCase
  10. {
  11. protected function createTraitUseAdaptationBuilder($trait, $method) {
  12. return new TraitUseAdaptation($trait, $method);
  13. }
  14. public function testAsMake() {
  15. $builder = $this->createTraitUseAdaptationBuilder(null, 'foo');
  16. $this->assertEquals(
  17. new Stmt\TraitUseAdaptation\Alias(null, 'foo', null, 'bar'),
  18. (clone $builder)->as('bar')->getNode()
  19. );
  20. $this->assertEquals(
  21. new Stmt\TraitUseAdaptation\Alias(null, 'foo', Class_::MODIFIER_PUBLIC, null),
  22. (clone $builder)->makePublic()->getNode()
  23. );
  24. $this->assertEquals(
  25. new Stmt\TraitUseAdaptation\Alias(null, 'foo', Class_::MODIFIER_PROTECTED, null),
  26. (clone $builder)->makeProtected()->getNode()
  27. );
  28. $this->assertEquals(
  29. new Stmt\TraitUseAdaptation\Alias(null, 'foo', Class_::MODIFIER_PRIVATE, null),
  30. (clone $builder)->makePrivate()->getNode()
  31. );
  32. }
  33. public function testInsteadof() {
  34. $node = $this->createTraitUseAdaptationBuilder('SomeTrait', 'foo')
  35. ->insteadof('AnotherTrait')
  36. ->getNode()
  37. ;
  38. $this->assertEquals(
  39. new Stmt\TraitUseAdaptation\Precedence(
  40. new Name('SomeTrait'),
  41. 'foo',
  42. [new Name('AnotherTrait')]
  43. ),
  44. $node
  45. );
  46. }
  47. public function testAsOnNotAlias() {
  48. $this->expectException(\LogicException::class);
  49. $this->expectExceptionMessage('Cannot set alias for not alias adaptation buider');
  50. $this->createTraitUseAdaptationBuilder('Test', 'foo')
  51. ->insteadof('AnotherTrait')
  52. ->as('bar')
  53. ;
  54. }
  55. public function testInsteadofOnNotPrecedence() {
  56. $this->expectException(\LogicException::class);
  57. $this->expectExceptionMessage('Cannot add overwritten traits for not precedence adaptation buider');
  58. $this->createTraitUseAdaptationBuilder('Test', 'foo')
  59. ->as('bar')
  60. ->insteadof('AnotherTrait')
  61. ;
  62. }
  63. public function testInsteadofWithoutTrait() {
  64. $this->expectException(\LogicException::class);
  65. $this->expectExceptionMessage('Precedence adaptation must have trait');
  66. $this->createTraitUseAdaptationBuilder(null, 'foo')
  67. ->insteadof('AnotherTrait')
  68. ;
  69. }
  70. public function testMakeOnNotAlias() {
  71. $this->expectException(\LogicException::class);
  72. $this->expectExceptionMessage('Cannot set access modifier for not alias adaptation buider');
  73. $this->createTraitUseAdaptationBuilder('Test', 'foo')
  74. ->insteadof('AnotherTrait')
  75. ->makePublic()
  76. ;
  77. }
  78. public function testMultipleMake() {
  79. $this->expectException(\LogicException::class);
  80. $this->expectExceptionMessage('Multiple access type modifiers are not allowed');
  81. $this->createTraitUseAdaptationBuilder(null, 'foo')
  82. ->makePrivate()
  83. ->makePublic()
  84. ;
  85. }
  86. public function testUndefinedType() {
  87. $this->expectException(\LogicException::class);
  88. $this->expectExceptionMessage('Type of adaptation is not defined');
  89. $this->createTraitUseAdaptationBuilder(null, 'foo')
  90. ->getNode()
  91. ;
  92. }
  93. }