Prophet.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of the Prophecy.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. * Marcello Duarte <marcello.duarte@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Prophecy;
  11. use Prophecy\Doubler\Doubler;
  12. use Prophecy\Doubler\LazyDouble;
  13. use Prophecy\Doubler\ClassPatch;
  14. use Prophecy\Prophecy\ObjectProphecy;
  15. use Prophecy\Prophecy\RevealerInterface;
  16. use Prophecy\Prophecy\Revealer;
  17. use Prophecy\Call\CallCenter;
  18. use Prophecy\Util\StringUtil;
  19. use Prophecy\Exception\Prediction\PredictionException;
  20. use Prophecy\Exception\Prediction\AggregateException;
  21. /**
  22. * Prophet creates prophecies.
  23. *
  24. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  25. */
  26. class Prophet
  27. {
  28. private $doubler;
  29. private $revealer;
  30. private $util;
  31. /**
  32. * @var ObjectProphecy[]
  33. */
  34. private $prophecies = array();
  35. /**
  36. * Initializes Prophet.
  37. *
  38. * @param null|Doubler $doubler
  39. * @param null|RevealerInterface $revealer
  40. * @param null|StringUtil $util
  41. */
  42. public function __construct(Doubler $doubler = null, RevealerInterface $revealer = null,
  43. StringUtil $util = null)
  44. {
  45. if (null === $doubler) {
  46. $doubler = new Doubler;
  47. $doubler->registerClassPatch(new ClassPatch\SplFileInfoPatch);
  48. $doubler->registerClassPatch(new ClassPatch\TraversablePatch);
  49. $doubler->registerClassPatch(new ClassPatch\ThrowablePatch);
  50. $doubler->registerClassPatch(new ClassPatch\DisableConstructorPatch);
  51. $doubler->registerClassPatch(new ClassPatch\ProphecySubjectPatch);
  52. $doubler->registerClassPatch(new ClassPatch\ReflectionClassNewInstancePatch);
  53. $doubler->registerClassPatch(new ClassPatch\HhvmExceptionPatch());
  54. $doubler->registerClassPatch(new ClassPatch\MagicCallPatch);
  55. $doubler->registerClassPatch(new ClassPatch\KeywordPatch);
  56. }
  57. $this->doubler = $doubler;
  58. $this->revealer = $revealer ?: new Revealer;
  59. $this->util = $util ?: new StringUtil;
  60. }
  61. /**
  62. * Creates new object prophecy.
  63. *
  64. * @param null|string $classOrInterface Class or interface name
  65. *
  66. * @return ObjectProphecy
  67. */
  68. public function prophesize($classOrInterface = null)
  69. {
  70. $this->prophecies[] = $prophecy = new ObjectProphecy(
  71. new LazyDouble($this->doubler),
  72. new CallCenter($this->util),
  73. $this->revealer
  74. );
  75. if ($classOrInterface && class_exists($classOrInterface)) {
  76. return $prophecy->willExtend($classOrInterface);
  77. }
  78. if ($classOrInterface && interface_exists($classOrInterface)) {
  79. return $prophecy->willImplement($classOrInterface);
  80. }
  81. return $prophecy;
  82. }
  83. /**
  84. * Returns all created object prophecies.
  85. *
  86. * @return ObjectProphecy[]
  87. */
  88. public function getProphecies()
  89. {
  90. return $this->prophecies;
  91. }
  92. /**
  93. * Returns Doubler instance assigned to this Prophet.
  94. *
  95. * @return Doubler
  96. */
  97. public function getDoubler()
  98. {
  99. return $this->doubler;
  100. }
  101. /**
  102. * Checks all predictions defined by prophecies of this Prophet.
  103. *
  104. * @throws Exception\Prediction\AggregateException If any prediction fails
  105. */
  106. public function checkPredictions()
  107. {
  108. $exception = new AggregateException("Some predictions failed:\n");
  109. foreach ($this->prophecies as $prophecy) {
  110. try {
  111. $prophecy->checkProphecyMethodsPredictions();
  112. } catch (PredictionException $e) {
  113. $exception->append($e);
  114. }
  115. }
  116. if (count($exception->getExceptions())) {
  117. throw $exception;
  118. }
  119. }
  120. }