DoublerSpec.php 4.4 KB

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