MethodInvokedAtIndex.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\TestFramework\Unit\Matcher;
  7. use PHPUnit\Framework\ExpectationFailedException;
  8. use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
  9. /**
  10. * Class MethodInvokedAtIndex
  11. * Matches invocations per 'method' at 'position'
  12. * Example:
  13. * $mock->expects(new MethodInvokedAtIndex(0))->method('getMethod')->willReturn(1);
  14. * $mock->expects(new MethodInvokedAtIndex(1))->method('getMethod')->willReturn(2);
  15. *
  16. * $mock->getMethod(); // returns 1
  17. * $mock->getMethod(); // returns 2
  18. *
  19. * @package Magento\TestFramework\Matcher
  20. */
  21. class MethodInvokedAtIndex implements \PHPUnit\Framework\MockObject\Matcher\Invocation
  22. {
  23. /**
  24. * @var int
  25. */
  26. private $sequenceIndex;
  27. /**
  28. * @var int
  29. */
  30. private $currentIndex = -1;
  31. /**
  32. * @var array
  33. */
  34. private $indexes = [];
  35. /**
  36. * @param int $sequenceIndex
  37. */
  38. public function __construct($sequenceIndex)
  39. {
  40. $this->sequenceIndex = $sequenceIndex;
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function toString(): string
  46. {
  47. return 'invoked at sequence index ' . $this->sequenceIndex;
  48. }
  49. /**
  50. * @param \PHPUnit\Framework\MockObject\Invocation $invocation
  51. * @return boolean
  52. */
  53. public function matches(BaseInvocation $invocation): bool
  54. {
  55. /** @noinspection PhpUndefinedFieldInspection */
  56. if (!isset($this->indexes[$invocation->getMethodName()])) {
  57. /** @noinspection PhpUndefinedFieldInspection */
  58. $this->indexes[$invocation->getMethodName()] = 0;
  59. } else {
  60. /** @noinspection PhpUndefinedFieldInspection */
  61. $this->indexes[$invocation->getMethodName()]++;
  62. }
  63. $this->currentIndex++;
  64. /** @noinspection PhpUndefinedFieldInspection */
  65. return $this->indexes[$invocation->getMethodName()] == $this->sequenceIndex;
  66. }
  67. /**
  68. * @param BaseInvocation $invocation
  69. * @return mixed
  70. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  71. */
  72. public function invoked(BaseInvocation $invocation)
  73. {
  74. }
  75. /**
  76. * Verifies that the current expectation is valid. If everything is OK the
  77. * code should just return, if not it must throw an exception.
  78. *
  79. * @throws ExpectationFailedException
  80. */
  81. public function verify(): void
  82. {
  83. if ($this->currentIndex < $this->sequenceIndex) {
  84. throw new ExpectationFailedException(
  85. \sprintf(
  86. 'The expected invocation at index %s was never reached.',
  87. $this->sequenceIndex
  88. )
  89. );
  90. }
  91. }
  92. }