Comments (22)
My blog was a blog, not a bliki. So no, I will not go back and update the blog post you mention. The proposed alternative is to not use static methods.
from phpunit-documentation.
I would rather remove the feature (it does not do what most people expect anyway) than document it.
from phpunit-documentation.
👍 On throwing that out
from phpunit-documentation.
Can we do that in PHPUnit 3.8 already?
from phpunit-documentation.
I'd say it should throw deprecation notices for one release?
from phpunit-documentation.
Makes sense. So we'll deprecate it in PHPUnit 3.8 and remove it in PHPUnit 3.9.
from phpunit-documentation.
Alright, I will close this in favor of deprecating the feature in 3.8.
from phpunit-documentation.
@sebastianbergmann Could you update this blog post then explaining that this feature's deprecated, and/or proposing alternatives?
http://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html
Thanks,
Kevin
from phpunit-documentation.
Given that only newer versions of PHPUnit support the abstracted runtime environment (php vs hhvm), we are forced to upgrade to a version of PHPUnit that doesn't support static mocking. We have a large codebase and cannot just remove our static methods. What is your suggestion in this case?
from phpunit-documentation.
Also, for what it's worth, it's odd that a feature that your users rely on is being removed in a minor version bump. Deferring the removal of this feature to 4.x would have more closely followed the guidelines set by semantic versioning.
from phpunit-documentation.
I am actually considering to rename 3.8 to 4.0 (and 3.9 to 4.1). But bumping the major version number does not solve your problem.
staticExpects()
, which was slated for removal and marked as deprecated for a while, did not actually allow stubbing and mocking arbitrary static method calls. It only worked within the scope of the same class. Since it was limited in use I have rarely seen it being used. And considering that it provided little gain but considerable maintenance pain we decided to remove it.
from phpunit-documentation.
Understood that the intended use was only when using static resolution within the same class, however it was always possible to inject the classname of mocked objects, as we have done and were able to mock arbitrary static methods. I know this will make you cringe, but it was something we relied on:
//
// in the code under test
$TheClass = ClassTable::find("TheClass");
return $TheClass::myStaticMethod();
// in the test itself
$obj = $this->getMock("TheClass");
/* ... set up static expectations ... */
ClassTable::set("TheClass", get_class($obj));
This was the only way to test some of our most procedural code and was a lifesaver. Obviously in an ideal world we wouldn't have this problem since we'd only ever be using non-static methods, but we aren't in that world and need this feature for the foreseeable future. However, I understand that we may have been relying on unsupported behavior in PHPUnit.
from phpunit-documentation.
Might it be possible to backport the environment / runtime stuff to 3.7?
from phpunit-documentation.
Everything is possible (technically). But the 3.7 branch is closed for anything but bugfixes.
from phpunit-documentation.
So I guess we now aren't supposed to test static factory methods?
from phpunit-documentation.
After it removed, is there any way to stub static methods now?
from phpunit-documentation.
No.
from phpunit-documentation.
It feels gross, but it's something. You can use an inheritance approach, e.g.:
In Experiment.php
class Experiment
{
public static function getVariant($userId, $experimentName)
{
$experiment = self::loadExperimentJson($experimentName):
return $userId % 10 > 5; // some sort of bucketing
}
protected static function loadExperimentJson($experimentName)
{
// ... do something
}
}
In my ExperimentTest.php
class ExperimentTest extends \Experiment
{
public static function loadExperimentJson($experimentName)
{
return "{
"name": "TestExperiment",
"variants": ["a", "b"],
... etc
}"
}
}
And then I would use it like so:
public function test_Experiment_getVariantForExperiment()
{
$variant = ExperimentTest::getVariant(123, 'blah');
$this->assertEquals($variant, 'a');
$variant = ExperimentTest::getVariant(124, 'blah');
$this->assertEquals($variant, 'b');
}
from phpunit-documentation.
@timbrownsf No, you can't, if you are calling the static method with self::
- this will always reference the class itself, not respecting any inherited class. You'd have to use static::
instead - and that is what this useless discussion is all about. Sebastian had a feature allowing to mock static functions that are being called using static::
, but the world thought it would allow mocking calls made with self::
, which it did not. While it was a nice idea trying to help test untestable code, he said he has seen very few useful usage, and much wrong usage - that's why the feature went out again.
from phpunit-documentation.
So there is absolutely NO way to test static methods with the most recent version of PHPUnit?
from phpunit-documentation.
You can test them. You cannot mock them. That's why using static calls is bad for testability.
Am 29. Mai 2016 20:33:36 MESZ, schrieb jannikkeller [email protected]:
So there is absolutely NO way to test static methods with the most
recent version of PHPUnit?
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#77 (comment)
from phpunit-documentation.
This is not a support forum, sorry.
from phpunit-documentation.
Related Issues (20)
- No up top button HOT 1
- Malformed jUnit file with multiple Test Suite HOT 1
- namespace should be used before the use statement in example 3.1: Named data sets. HOT 1
- Document limitations related to the fact that closures cannot be serialized HOT 1
- Document that execution of a test that raises a PHPUnit warning is aborted HOT 1
- (Hopefully) Everything that is wrong with PHPUnit's documentation HOT 10
- How about that? "The Mock Builder API can be[...]" instead HOT 1
- 建议在http://www.phpunit.cn/manual/current/zh_cn/textui.html 此页面上加一些样例,大多数人关心怎么样运行所有测试。 HOT 4
- Document "Test code or tested code did not (only) close its own output buffers" HOT 1
- Add @usesDefaultClass to documentation HOT 1
- open a download link in phpunit selenium test
- can't fund the class Guestbook HOT 1
- Obscure reaction on DataProvider exception HOT 2
- Add willReturnOnConsecutiveCalls to documentation HOT 1
- Confusion around PHPUnit_Extensions_TestDecorator since it no longer exists HOT 1
- When it comes to the database part of this doc, it's quite confusing. HOT 1
- Add missing documentation for "@depends shallowclone <method>" HOT 1
- Use assertSame() instead of assertEquals() where possible HOT 1
- doesNotPerformAssertions is not documented HOT 1
- Testdox attribute in phpunit.xml HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpunit-documentation.