Skip to content

Commit 3ad9b77

Browse files
committed
Use onlyMethods instead of setMethods on MockBuilder instance when available
This resolves the following error when running the test suite using PHPUnit 10: Call to undefined method PHPUnit\Framework\MockObject\MockBuilder::setMethods()
1 parent f47a90e commit 3ad9b77

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

tests/Constraint/ConstraintTest.php

+16-20
Original file line numberDiff line numberDiff line change
@@ -377,33 +377,29 @@ public function testVersionMatchFails($requireOperator, $requireVersion, $provid
377377
public function testInverseMatchingOtherConstraints()
378378
{
379379
$constraint = new Constraint('>', '1.0.0');
380+
$otherConstraintClasses = array(
381+
'Composer\Semver\Constraint\MultiConstraint',
382+
'Composer\Semver\Constraint\MatchAllConstraint'
383+
);
380384

381-
$multiConstraint = $this
382-
->getMockBuilder('Composer\Semver\Constraint\MultiConstraint')
383-
->disableOriginalConstructor()
384-
->setMethods(array('matches'))
385-
->getMock()
386-
;
387-
388-
$matchAllConstraint = $this
389-
->getMockBuilder('Composer\Semver\Constraint\MatchAllConstraint')
390-
->setMethods(array('matches'))
391-
->getMock()
392-
;
393-
394-
foreach (array($multiConstraint, $matchAllConstraint) as $mock) {
395-
$mock
385+
foreach ($otherConstraintClasses as $otherConstraintClass) {
386+
$otherConstraintMockBuilder = $this->getMockBuilder($otherConstraintClass);
387+
$otherConstraintMockBuilder->disableOriginalConstructor();
388+
if (method_exists($otherConstraintMockBuilder, 'onlyMethods')) {
389+
$otherConstraintMockBuilder->onlyMethods(array('matches'));
390+
} elseif (method_exists($otherConstraintMockBuilder, 'setMethods')) {
391+
$otherConstraintMockBuilder->setMethods(array('matches'));
392+
}
393+
$otherConstraintMock = $otherConstraintMockBuilder->getMock();
394+
$otherConstraintMock
396395
->expects($this->once())
397396
->method('matches')
398397
->with($constraint)
399398
->willReturn(true)
400399
;
400+
// @phpstan-ignore-next-line
401+
$this->assertTrue($constraint->matches($otherConstraintMock));
401402
}
402-
403-
// @phpstan-ignore-next-line
404-
$this->assertTrue($constraint->matches($multiConstraint));
405-
// @phpstan-ignore-next-line
406-
$this->assertTrue($constraint->matches($matchAllConstraint));
407403
}
408404

409405
public function testComparableBranches()

0 commit comments

Comments
 (0)