TraitUseAdaptationTest.php 3.5 KB

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