DoublerSpec.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace spec\Prophecy\Doubler;
  3. use PhpSpec\ObjectBehavior;
  4. use Prophecy\Doubler\ClassPatch\ClassPatchInterface;
  5. use Prophecy\Doubler\Generator\ClassCreator;
  6. use Prophecy\Doubler\Generator\ClassMirror;
  7. use Prophecy\Doubler\Generator\Node\ClassNode;
  8. use Prophecy\Doubler\NameGenerator;
  9. class DoublerSpec extends ObjectBehavior
  10. {
  11. function let(ClassMirror $mirror, ClassCreator $creator, NameGenerator $namer)
  12. {
  13. $this->beConstructedWith($mirror, $creator, $namer);
  14. }
  15. function it_does_not_have_patches_by_default()
  16. {
  17. $this->getClassPatches()->shouldHaveCount(0);
  18. }
  19. function its_registerClassPatch_adds_a_patch_to_the_doubler(ClassPatchInterface $patch)
  20. {
  21. $this->registerClassPatch($patch);
  22. $this->getClassPatches()->shouldReturn(array($patch));
  23. }
  24. function its_getClassPatches_sorts_patches_by_priority(
  25. ClassPatchInterface $alt1,
  26. ClassPatchInterface $alt2,
  27. ClassPatchInterface $alt3,
  28. ClassPatchInterface $alt4
  29. ) {
  30. $alt1->getPriority()->willReturn(2);
  31. $alt2->getPriority()->willReturn(50);
  32. $alt3->getPriority()->willReturn(10);
  33. $alt4->getPriority()->willReturn(0);
  34. $this->registerClassPatch($alt1);
  35. $this->registerClassPatch($alt2);
  36. $this->registerClassPatch($alt3);
  37. $this->registerClassPatch($alt4);
  38. $this->getClassPatches()->shouldReturn(array($alt2, $alt3, $alt1, $alt4));
  39. }
  40. function its_double_mirrors_alterates_and_instantiates_provided_class(
  41. $mirror,
  42. $creator,
  43. $namer,
  44. ClassPatchInterface $alt1,
  45. ClassPatchInterface $alt2,
  46. \ReflectionClass $class,
  47. \ReflectionClass $interface1,
  48. \ReflectionClass $interface2,
  49. ClassNode $node
  50. ) {
  51. $mirror->reflect($class, array($interface1, $interface2))->willReturn($node);
  52. $alt1->supports($node)->willReturn(true);
  53. $alt2->supports($node)->willReturn(false);
  54. $alt1->getPriority()->willReturn(1);
  55. $alt2->getPriority()->willReturn(2);
  56. $namer->name($class, array($interface1, $interface2))->willReturn('SplStack');
  57. $class->getName()->willReturn('stdClass');
  58. $interface1->getName()->willReturn('ArrayAccess');
  59. $interface2->getName()->willReturn('Iterator');
  60. $alt1->apply($node)->shouldBeCalled();
  61. $alt2->apply($node)->shouldNotBeCalled();
  62. $creator->create('SplStack', $node)->shouldBeCalled();
  63. $this->registerClassPatch($alt1);
  64. $this->registerClassPatch($alt2);
  65. $this->double($class, array($interface1, $interface2))
  66. ->shouldReturnAnInstanceOf('SplStack');
  67. }
  68. function it_double_instantiates_a_class_with_constructor_argument(
  69. $mirror,
  70. \ReflectionClass $class,
  71. ClassNode $node,
  72. $namer
  73. ) {
  74. $class->getName()->willReturn('ReflectionClass');
  75. $mirror->reflect($class, array())->willReturn($node);
  76. $namer->name($class, array())->willReturn('ReflectionClass');
  77. $double = $this->double($class, array(), array('stdClass'));
  78. $double->shouldBeAnInstanceOf('ReflectionClass');
  79. $double->getName()->shouldReturn('stdClass');
  80. }
  81. function it_can_instantiate_class_with_final_constructor(
  82. $mirror,
  83. \ReflectionClass $class,
  84. ClassNode $node,
  85. $namer
  86. ) {
  87. $class->getName()->willReturn('spec\Prophecy\Doubler\WithFinalConstructor');
  88. $mirror->reflect($class, array())->willReturn($node);
  89. $namer->name($class, array())->willReturn('spec\Prophecy\Doubler\WithFinalConstructor');
  90. $double = $this->double($class, array());
  91. $double->shouldBeAnInstanceOf('spec\Prophecy\Doubler\WithFinalConstructor');
  92. }
  93. }
  94. class WithFinalConstructor
  95. {
  96. final public function __construct() {}
  97. }