- Issue created by @tr
- 🇺🇸United States tr Cascadia
I'm not at all sure about this because PHPUnit didn't replace this method with anything, but here's a try at it.
- Merge request !42Issue #3457486 by TR: Phpstan: Call to deprecated method getMockForAbstractClass() → (Open) created by tr
- 🇮🇳India abhiyanshu
Absolutely correct @tr,
Since getMockForAbstractClass() has been removed without a direct replacement method within PHPUnit itself, the recommended approach now is to use the getMockBuilder() method directly to create a mock object for an abstract class.i.e..,
use PHPUnit\Framework\TestCase; abstract class AbstractClass { abstract public function method1(); abstract public function method2(); } class MyTest extends TestCase { public function testSomething() { $mock = $this->getMockBuilder(AbstractClass::class) ->getMock(); // Define mock behavior or expectations $mock->expects($this->any()) ->method('method1') ->willReturn('foo'); // Assertions or test logic with $mock $this->assertEquals('foo', $mock->method1()); } }
- Status changed to Needs work
9 months ago 7:08am 27 June 2024 - 🇺🇸United States tr Cascadia
Yeah, that's what I did. But it's not quite right - the tests failed. Maybe you can take a look and see what I missed?
- 🇮🇳India abhiyanshu
Sure, I revisited
Merge Request !42
and created a patch for it.
Included some missing code, Hope it works. Thanks. - 🇺🇸United States tr Cascadia
@abhiyanshu
So I understand your change to use $methods instead of [$methods] - that was my error, and I fixed it in the MR.But the other changes I don't think are correct. For example:
$rules_action_base = $this->getMockBuilder(RulesActionBase::class) ->setConstructorArgs([[], '', ['label' => 'something']]) + ->onlyMethods(['getPluginLabel']) ->getMock(); + + $rules_action_base + ->expects($this->once()) + ->method('getPluginLabel') + ->willReturn('something'); + $this->assertEquals('something', $rules_action_base->summary());
We were formerly using
getMockForAbstractClass()
here ... but I changed it togetMockBuilder()
for this issue.
Regardless, the point is that we have an abstract base class with some concrete methods, and we want to test one of those concrete methods here. So withgetMockForAbstractClass()
what happened was it returned a mock for the abstract class where the actual concrete methods worked and the abstract methods were mocked.What you've done with this change is to mock
getPluginLabel()
and specify what it should return, then test that it does return that.That's not what we want, because of course that will work.
We want to test that the actual concrete
getPluginLabel()
(not a mock of that method), will return the correct value. That's what used to happen withgetMockForAbstractClass()
- 🇺🇸United States tr Cascadia
MR needs to be rebased and conflicts resolved.
- Status changed to Active
6 months ago 7:46am 15 September 2024 - 🇺🇸United States tr Cascadia
Previous MR branch was totally screwed up through months of rebasing, so I decided to start with a new branch and try this again.
So to summarize, there are three tests that use getMockForAbstractClass() that need to be changed for PHPUnit 12 so there are no PHPSTAN errors in the D11 branch.
These are:
RulesAdminAccessTest
RulesActionBaseTest
RulesConditionContainerTestThe changes in the MR currently fix the first two tests, but the third fails with no output so it's not clear why or where it's failing. Working on that ...
- Status changed to Fixed
6 months ago 9:14am 15 September 2024 - 🇺🇸United States tr Cascadia
Well I made that about 10 times harder than it needed to be.
Regardless, this is fixed to my satisfaction. Automatically closed - issue fixed for 2 weeks with no activity.