Skip to content

Commit

Permalink
Prepare tests for PHPUnit 10 (#162)
Browse files Browse the repository at this point in the history
* Avoid mandatory `void` return type of the inherited `setUp` method in PHPUnit 10

This is done by renaming the inherited `setUp` method to a new `setUpTestCase` method and adding an `@before` annotation.

* 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()

---------

Co-authored-by: Aad Mathijssen <aad.mathijssen@iodigital.com>
  • Loading branch information
aadmathijssen and Aad Mathijssen authored Aug 22, 2024
1 parent 9569a83 commit 4b9538f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
42 changes: 21 additions & 21 deletions tests/Constraint/ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class ConstraintTest extends TestCase
*/
protected $versionProvide;

protected function setUp()
/**
* @before
* @return void
*/
public function setUpTestCase()
{
$this->constraint = new Constraint('==', '1');
$this->versionProvide = new Constraint('==', 'dev-foo');
Expand Down Expand Up @@ -373,33 +377,29 @@ public function testVersionMatchFails($requireOperator, $requireVersion, $provid
public function testInverseMatchingOtherConstraints()
{
$constraint = new Constraint('>', '1.0.0');
$otherConstraintClasses = array(
'Composer\Semver\Constraint\MultiConstraint',
'Composer\Semver\Constraint\MatchAllConstraint'
);

$multiConstraint = $this
->getMockBuilder('Composer\Semver\Constraint\MultiConstraint')
->disableOriginalConstructor()
->setMethods(array('matches'))
->getMock()
;

$matchAllConstraint = $this
->getMockBuilder('Composer\Semver\Constraint\MatchAllConstraint')
->setMethods(array('matches'))
->getMock()
;

foreach (array($multiConstraint, $matchAllConstraint) as $mock) {
$mock
foreach ($otherConstraintClasses as $otherConstraintClass) {
$otherConstraintMockBuilder = $this->getMockBuilder($otherConstraintClass);
$otherConstraintMockBuilder->disableOriginalConstructor();
if (method_exists($otherConstraintMockBuilder, 'onlyMethods')) {
$otherConstraintMockBuilder->onlyMethods(array('matches'));
} elseif (method_exists($otherConstraintMockBuilder, 'setMethods')) {
$otherConstraintMockBuilder->setMethods(array('matches'));
}
$otherConstraintMock = $otherConstraintMockBuilder->getMock();
$otherConstraintMock
->expects($this->once())
->method('matches')
->with($constraint)
->willReturn(true)
;
// @phpstan-ignore-next-line
$this->assertTrue($constraint->matches($otherConstraintMock));
}

// @phpstan-ignore-next-line
$this->assertTrue($constraint->matches($multiConstraint));
// @phpstan-ignore-next-line
$this->assertTrue($constraint->matches($matchAllConstraint));
}

public function testComparableBranches()
Expand Down
6 changes: 5 additions & 1 deletion tests/Constraint/MatchAllConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ class MatchAllConstraintTest extends TestCase
*/
protected $matchAllConstraint;

protected function setUp()
/**
* @before
* @return void
*/
public function setUpTestCase()
{
$this->versionProvide = new Constraint('==', '1.1');
$this->matchAllConstraint = new MatchAllConstraint();
Expand Down
6 changes: 5 additions & 1 deletion tests/Constraint/MatchNoneConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ class MatchNoneConstraintTest extends TestCase
*/
protected $matchNoneConstraint;

protected function setUp()
/**
* @before
* @return void
*/
public function setUpTestCase()
{
$this->matchNoneConstraint = new MatchNoneConstraint();
}
Expand Down
6 changes: 5 additions & 1 deletion tests/Constraint/MultiConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class MultiConstraintTest extends TestCase
*/
protected $versionRequireEnd;

protected function setUp()
/**
* @before
* @return void
*/
public function setUpTestCase()
{
$this->versionRequireStart = new Constraint('>', '1.0');
$this->versionRequireEnd = new Constraint('<', '1.2');
Expand Down

0 comments on commit 4b9538f

Please sign in to comment.