Prophet.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\DisableConstructorPatch);
  50. $doubler->registerClassPatch(new ClassPatch\ProphecySubjectPatch);
  51. $doubler->registerClassPatch(new ClassPatch\ReflectionClassNewInstancePatch);
  52. $doubler->registerClassPatch(new ClassPatch\HhvmExceptionPatch());
  53. $doubler->registerClassPatch(new ClassPatch\MagicCallPatch);
  54. $doubler->registerClassPatch(new ClassPatch\KeywordPatch);
  55. }
  56. $this->doubler = $doubler;
  57. $this->revealer = $revealer ?: new Revealer;
  58. $this->util = $util ?: new StringUtil;
  59. }
  60. /**
  61. * Creates new object prophecy.
  62. *
  63. * @param null|string $classOrInterface Class or interface name
  64. *
  65. * @return ObjectProphecy
  66. */
  67. public function prophesize($classOrInterface = null)
  68. {
  69. $this->prophecies[] = $prophecy = new ObjectProphecy(
  70. new LazyDouble($this->doubler),
  71. new CallCenter($this->util),
  72. $this->revealer
  73. );
  74. if ($classOrInterface && class_exists($classOrInterface)) {
  75. return $prophecy->willExtend($classOrInterface);
  76. }
  77. if ($classOrInterface && interface_exists($classOrInterface)) {
  78. return $prophecy->willImplement($classOrInterface);
  79. }
  80. return $prophecy;
  81. }
  82. /**
  83. * Returns all created object prophecies.
  84. *
  85. * @return ObjectProphecy[]
  86. */
  87. public function getProphecies()
  88. {
  89. return $this->prophecies;
  90. }
  91. /**
  92. * Returns Doubler instance assigned to this Prophet.
  93. *
  94. * @return Doubler
  95. */
  96. public function getDoubler()
  97. {
  98. return $this->doubler;
  99. }
  100. /**
  101. * Checks all predictions defined by prophecies of this Prophet.
  102. *
  103. * @throws Exception\Prediction\AggregateException If any prediction fails
  104. */
  105. public function checkPredictions()
  106. {
  107. $exception = new AggregateException("Some predictions failed:\n");
  108. foreach ($this->prophecies as $prophecy) {
  109. try {
  110. $prophecy->checkProphecyMethodsPredictions();
  111. } catch (PredictionException $e) {
  112. $exception->append($e);
  113. }
  114. }
  115. if (count($exception->getExceptions())) {
  116. throw $exception;
  117. }
  118. }
  119. }