Skip to content

Commit

Permalink
Tests/AbstractArrayDeclarationSniffTest: work around for `MockBuilder…
Browse files Browse the repository at this point in the history
…::setMethods()` removal

Please note:
* The `getMockBuilder()` class is also deprecated now, so replacing with another method call on the `MockBuilder` class will only work in the short/medium term and in a future iteration, this will have to be refactored again.
* Also the `getMockForAbstractClass method is also deprecated and expected to be removed in PHPUnit 12.

Refs:
* sebastianbergmann/phpunit 3687#issuecomment-492537584
* sebastianbergmann/phpunit 3770
* sebastianbergmann/phpunit 3769
* sebastianbergmann/phpunit 4775
  • Loading branch information
jrfnl committed Jun 12, 2023
1 parent ddcecc9 commit 6653cdd
Showing 1 changed file with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public function testShortList()
{
$target = $this->getTargetToken('/* testShortList */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->never())
->method('processOpenClose');
Expand Down Expand Up @@ -86,9 +84,7 @@ public function testEmptyArray()
{
$target = $this->getTargetToken('/* testEmptyArray */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose');
Expand Down Expand Up @@ -124,9 +120,7 @@ public function testSingleLineShortArrayNoKeysNoTrailingComma()
Collections::arrayOpenTokensBC()
);

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose')
Expand Down Expand Up @@ -187,9 +181,7 @@ public function testMultiLineLongArrayKeysTrailingComma()
Collections::arrayOpenTokensBC()
);

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose')
Expand Down Expand Up @@ -265,9 +257,7 @@ public function testMultiLineShortArrayMixedKeysNoKeys()
Collections::arrayOpenTokensBC()
);

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose')
Expand Down Expand Up @@ -346,9 +336,7 @@ public function testEmptyArrayItem()
{
$target = $this->getTargetToken('/* testEmptyArrayItem */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose');
Expand Down Expand Up @@ -377,9 +365,7 @@ public function testShortCircuitOnProcessOpenClose()
{
$target = $this->getTargetToken('/* testShortCircuit */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose')
Expand Down Expand Up @@ -412,9 +398,7 @@ public function testShortCircuitOnProcessKey()
{
$target = $this->getTargetToken('/* testShortCircuit */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose');
Expand Down Expand Up @@ -447,9 +431,7 @@ public function testShortCircuitOnProcessNoKey()
{
$target = $this->getTargetToken('/* testShortCircuit */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose');
Expand Down Expand Up @@ -482,9 +464,7 @@ public function testShortCircuitOnProcessArrow()
{
$target = $this->getTargetToken('/* testShortCircuit */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose');
Expand Down Expand Up @@ -517,9 +497,7 @@ public function testShortCircuitOnProcessValue()
{
$target = $this->getTargetToken('/* testShortCircuit */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose');
Expand Down Expand Up @@ -552,9 +530,7 @@ public function testShortCircuitOnProcessComma()
{
$target = $this->getTargetToken('/* testShortCircuit */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->once())
->method('processOpenClose');
Expand Down Expand Up @@ -587,9 +563,7 @@ public function testBowOutOnUnfinishedArray()
{
$target = $this->getTargetToken('/* testLiveCoding */', Collections::arrayOpenTokensBC());

$mockObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff')
->setMethods($this->methodsToMock)
->getMockForAbstractClass();
$mockObj = $this->getMockedClassUnderTest();

$mockObj->expects($this->never())
->method('processOpenClose');
Expand All @@ -611,4 +585,30 @@ public function testBowOutOnUnfinishedArray()

$mockObj->process(self::$phpcsFile, $target);
}

/**
* Helper method to retrieve a mock object for the abstract class.
*
* The `setMethods()` method was silently deprecated in PHPUnit 9 and removed in PHPUnit 10.
*
* Note: direct access to the `getMockBuilder()` method is soft deprecated as of PHPUnit 10,
* and expected to be hard deprecated in PHPUnit 11 and removed in PHPUnit 12.
* Dealing with that is something for a later iteration of the test suite.
*
* @return \PHPUnit\Framework\MockObject\MockObject
*/
private function getMockedClassUnderTest()
{
$mockedObj = $this->getMockBuilder('\PHPCSUtils\AbstractSniffs\AbstractArrayDeclarationSniff');

if (\method_exists($mockedObj, 'onlyMethods')) {
// PHPUnit 8+.
return $mockedObj->onlyMethods($this->methodsToMock)
->getMockForAbstractClass();
}

// PHPUnit < 8.
return $mockedObj->setMethods($this->methodsToMock)
->getMockForAbstractClass();
}
}

0 comments on commit 6653cdd

Please sign in to comment.