/vendor/phpspec/prophecy/src/Prophecy/Prediction/CallPrediction.php

https://bitbucket.org/alan_cordova/api-sb-map · PHP · 86 lines · 48 code · 11 blank · 27 comment · 2 complexity · 67daacc4a6b9459c26296c802dc1deb9 MD5 · raw file

  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\Prediction;
  11. use Prophecy\Call\Call;
  12. use Prophecy\Prophecy\ObjectProphecy;
  13. use Prophecy\Prophecy\MethodProphecy;
  14. use Prophecy\Argument\ArgumentsWildcard;
  15. use Prophecy\Argument\Token\AnyValuesToken;
  16. use Prophecy\Util\StringUtil;
  17. use Prophecy\Exception\Prediction\NoCallsException;
  18. /**
  19. * Call prediction.
  20. *
  21. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  22. */
  23. class CallPrediction implements PredictionInterface
  24. {
  25. private $util;
  26. /**
  27. * Initializes prediction.
  28. *
  29. * @param StringUtil $util
  30. */
  31. public function __construct(StringUtil $util = null)
  32. {
  33. $this->util = $util ?: new StringUtil;
  34. }
  35. /**
  36. * Tests that there was at least one call.
  37. *
  38. * @param Call[] $calls
  39. * @param ObjectProphecy $object
  40. * @param MethodProphecy $method
  41. *
  42. * @throws \Prophecy\Exception\Prediction\NoCallsException
  43. */
  44. public function check(array $calls, ObjectProphecy $object, MethodProphecy $method)
  45. {
  46. if (count($calls)) {
  47. return;
  48. }
  49. $methodCalls = $object->findProphecyMethodCalls(
  50. $method->getMethodName(),
  51. new ArgumentsWildcard(array(new AnyValuesToken))
  52. );
  53. if (count($methodCalls)) {
  54. throw new NoCallsException(sprintf(
  55. "No calls have been made that match:\n".
  56. " %s->%s(%s)\n".
  57. "but expected at least one.\n".
  58. "Recorded `%s(...)` calls:\n%s",
  59. get_class($object->reveal()),
  60. $method->getMethodName(),
  61. $method->getArgumentsWildcard(),
  62. $method->getMethodName(),
  63. $this->util->stringifyCalls($methodCalls)
  64. ), $method);
  65. }
  66. throw new NoCallsException(sprintf(
  67. "No calls have been made that match:\n".
  68. " %s->%s(%s)\n".
  69. "but expected at least one.",
  70. get_class($object->reveal()),
  71. $method->getMethodName(),
  72. $method->getArgumentsWildcard()
  73. ), $method);
  74. }
  75. }