Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 61ad262

Browse files
committed
Fix usages of invocation matcher for PHPUnit 7
1 parent 5c20742 commit 61ad262

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/Event/TransactionTest.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@
99
*/
1010
namespace Magento\Test\Event;
1111

12+
use PHPUnit\Framework\MockObject\Matcher\Invocation;
13+
1214
class TransactionTest extends \PHPUnit\Framework\TestCase
1315
{
1416
/**
15-
* @var \Magento\TestFramework\Event\Transaction|\PHPUnit_Framework_MockObject_MockObject
17+
* @var \Magento\TestFramework\Event\Transaction|\PHPUnit\Framework\MockObject\MockObject
1618
*/
1719
protected $_object;
1820

1921
/**
20-
* @var \Magento\TestFramework\EventManager|\PHPUnit_Framework_MockObject_MockObject
22+
* @var \Magento\TestFramework\EventManager|\PHPUnit\Framework\MockObject\MockObject
2123
*/
2224
protected $_eventManager;
2325

2426
/**
25-
* @var \Magento\TestFramework\Db\Adapter\TransactionInterface|\PHPUnit_Framework_MockObject_MockObject
27+
* @var \Magento\TestFramework\Db\Adapter\TransactionInterface|\PHPUnit\Framework\MockObject\MockObject
2628
*/
2729
protected $_adapter;
2830

@@ -69,9 +71,9 @@ protected function _imitateTransactionStartRequest($eventName)
6971
/**
7072
* Setup expectations for "transaction start" use case
7173
*
72-
* @param \PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
74+
* @param Invocation $invocationMatcher
7375
*/
74-
protected function _expectTransactionStart(\PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
76+
protected function _expectTransactionStart(Invocation $invocationMatcher)
7577
{
7678
$this->_eventManager->expects($invocationMatcher)->method('fireEvent')->with('startTransaction');
7779
$this->_adapter->expects($this->once())->method('beginTransaction');
@@ -103,9 +105,9 @@ protected function _imitateTransactionRollbackRequest($eventName)
103105
/**
104106
* Setup expectations for "transaction rollback" use case
105107
*
106-
* @param \PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
108+
* @param Invocation $invocationMatcher
107109
*/
108-
protected function _expectTransactionRollback(\PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
110+
protected function _expectTransactionRollback(Invocation $invocationMatcher)
109111
{
110112
$this->_eventManager->expects($invocationMatcher)->method('fireEvent')->with('rollbackTransaction');
111113
$this->_adapter->expects($this->once())->method('rollback');

lib/internal/Magento/Framework/TestFramework/Unit/Matcher/MethodInvokedAtIndex.php

+63-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
namespace Magento\Framework\TestFramework\Unit\Matcher;
88

9+
use PHPUnit\Framework\ExpectationFailedException;
10+
use PHPUnit\Framework\MockObject\Invocation;
11+
912
/**
1013
* Class MethodInvokedAtIndex
1114
* Matches invocations per 'method' at 'position'
12-
* Example:
15+
* Example:§
1316
* $mock->expects(new MethodInvokedAtIndex(0))->method('getMethod')->willReturn(1);
1417
* $mock->expects(new MethodInvokedAtIndex(1))->method('getMethod')->willReturn(2);
1518
*
@@ -18,30 +21,82 @@
1821
*
1922
* @package Magento\TestFramework\Matcher
2023
*/
21-
class MethodInvokedAtIndex extends \PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex
24+
class MethodInvokedAtIndex implements \PHPUnit\Framework\MockObject\Matcher\Invocation
2225
{
2326
/**
2427
* @var array
2528
*/
2629
protected $indexes = [];
2730

2831
/**
29-
* @param \PHPUnit_Framework_MockObject_Invocation $invocation
32+
* @var int
33+
*/
34+
private $currentIndex = -1;
35+
36+
/**
37+
* @var int
38+
*/
39+
private $sequenceIndex;
40+
41+
/**
42+
* @param int $sequenceIndex
43+
*/
44+
public function __construct($sequenceIndex)
45+
{
46+
$this->sequenceIndex = $sequenceIndex;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function toString(): string
53+
{
54+
return 'invoked at sequence index ' . $this->sequenceIndex;
55+
}
56+
57+
/**
58+
* @param \PHPUnit\Framework\MockObject\Invocation $invocation
3059
* @return boolean
3160
*/
32-
public function matches(\PHPUnit_Framework_MockObject_Invocation $invocation)
61+
public function matches(Invocation $invocation): bool
3362
{
3463
/** @noinspection PhpUndefinedFieldInspection */
35-
if (!isset($this->indexes[$invocation->methodName])) {
64+
if (!isset($this->indexes[$invocation->getMethodName()])) {
3665
/** @noinspection PhpUndefinedFieldInspection */
37-
$this->indexes[$invocation->methodName] = 0;
66+
$this->indexes[$invocation->getMethodName()] = 0;
3867
} else {
3968
/** @noinspection PhpUndefinedFieldInspection */
40-
$this->indexes[$invocation->methodName]++;
69+
$this->indexes[$invocation->getMethodName()]++;
4170
}
4271
$this->currentIndex++;
4372

4473
/** @noinspection PhpUndefinedFieldInspection */
45-
return $this->indexes[$invocation->methodName] == $this->sequenceIndex;
74+
return $this->indexes[$invocation->getMethodName()] === $this->sequenceIndex;
75+
}
76+
77+
/**
78+
* Verifies that the current expectation is valid. If everything is OK the
79+
* code should just return, if not it must throw an exception.
80+
*
81+
* @throws ExpectationFailedException
82+
*/
83+
public function verify(): void
84+
{
85+
if ($this->currentIndex < $this->sequenceIndex) {
86+
throw new ExpectationFailedException(
87+
\sprintf(
88+
'The expected invocation at index %s was never reached.',
89+
$this->sequenceIndex
90+
)
91+
);
92+
}
93+
}
94+
95+
/**
96+
* @param Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
97+
* @return mixed
98+
*/
99+
public function invoked(Invocation $invocation)
100+
{
46101
}
47102
}

0 commit comments

Comments
 (0)