New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce the constant mocker #520
Conversation
@@ -392,7 +406,8 @@ public function setAssertionManager(test\assertion\manager $assertionManager = n | |||
->setHandler('stop', function() use ($test) { if ($test->debugModeIsEnabled() === true) { throw new test\exceptions\stop(); } return $test; }) | |||
->setHandler('executeOnFailure', function($callback) use ($test) { if ($test->debugModeIsEnabled() === true) { $test->executeOnFailure($callback); } return $test; }) | |||
->setHandler('dumpOnFailure', function($variable) use ($test) { if ($test->debugModeIsEnabled() === true) { $test->executeOnFailure(function() use ($variable) { var_dump($variable); }); } return $test; }) | |||
->setPropertyHandler('function', function() use ($test) { return $test->getPhpMocker(); }) | |||
->setPropertyHandler('function', function() use ($test) { return $this->getPhpFunctionMocker(); }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One morning, you will wake up, and atoum will be PSR-4…
Tests are failing on PHP5.3 but this not seems to be related to my PR. I will fix them though. |
@Hywan No, it's your fault! Should be fixed. |
@Hywan master is stable : https://travis-ci.org/atoum/atoum/branches -- EDIT -- |
Green. |
For the record, i'm not agree to merge this PR in atoum, because this new tool is an open door to bad practice about test creation and code conception. |
@mageekguy It is used to mock constants, not class constants. |
So your PR description should be improved. |
@mageekguy ❤️ |
I'm trying to find a class layout to avoid BC break. What about:
The external API will look identical, thanks to the Thoughts @Hywan @mageekguy ? -- EDIT -- should be clearer like this : <?php
namespace atoum\php\mocker {
abstract class builder {}
}
namespace atoum\php\mocker\builders {
use atoum\php\mocker\builder;
class funktion extends builder {}
class constant extends builder {}
}
namespace {
use atoum\php\mocker;
use atoum\php\mocker\builder;
use atoum\php\mocker\builders\funktion;
use atoum\php\mocker\builders\constant;
class_alias(funktion::class, 'atoum\\php\\mocker');
var_dump(new funktion() instanceof builder);
var_dump(new constant() instanceof builder);
var_dump(new funktion() instanceof mocker);
} |
@jubianchi Not the whole API will be the same. See |
And now? |
@@ -282,14 +284,35 @@ public function getAdapter() | |||
|
|||
public function setPhpMocker(php\mocker $phpMocker = null) | |||
{ | |||
$this->phpMocker = $phpMocker ?: new php\mocker(); | |||
$phpMocker = $phpMocker ?: new php\mocker(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure it can work here since php\mocker
does no longer exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, it should be php\mocker\funktion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And what about:
public function setPhpMocker(php\mocker $phpMocker = null)
{
return $this->setPhpFunctionMocker($phpMocker);
}
I guess the type-hint will warn something…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the polymorphism, as suggested by @mageekguy, is the best solution. It allows us to gracefully handle both mocker.
@jubianchi So, do we need class alias? |
@Hywan I'll try to finish this PR this week, I was busy working on the atom-plugin (which will soon be released) |
@jubianchi Excellent, thanks! |
@jubianchi Do you want me to rebase? |
With the unit tests added, everything should be OK. But there are some limitations to correctly document. Given the following class and test: <?php
namespace foo {
class foo {
public function hello()
{
return PHP_VERSION_ID;
}
}
}
namespace tests\units\foo {
use atoum;
/**
* @engine inline
*/
class foo extends atoum
{
public function testFoo()
{
$this
->given($this->newTestedInstance())
->then
->variable($this->testedInstance->hello())->isEqualTo(PHP_VERSION_ID)
->if($this->constant->PHP_VERSION_ID = uniqid())
->then
->variable($this->testedInstance->hello())->isEqualTo(PHP_VERSION_ID)
;
}
public function testBar()
{
$this
->given($this->newTestedInstance())
->if($this->constant->PHP_VERSION_ID = $mockVersionId = uniqid()) // inline engine will fail here
->then
->variable($this->testedInstance->hello())->isEqualTo($mockVersionId)
->if($this->constant->PHP_VERSION_ID = $mockVersionId = uniqid()) // isolate/concurrent engines will fail here
->then
->variable($this->testedInstance->hello())->isEqualTo($mockVersionId)
;
}
}
} Note the two comments: depending on the engine, the test will fail (atoum will throw exceptions) at different lines. To "mock" constant, we With the |
@Hywan yes, please do a rebase to fix merge conflicts :) |
@jubianchi I would say we have the same issue with function mocking, isn't it? |
2169cae
to
55db167
Compare
@jubianchi Done. Maybe we can squash some commits too. |
@Hywan go ahead ;) |
@jubianchi On PHP7 we have a segfault (https://travis-ci.org/atoum/atoum/jobs/92951761), I guess this is an error from Travis PHP build. On PHP5.3, we have a strange error, I can't take a look yet (https://travis-ci.org/atoum/atoum/jobs/92951754). If you have time, feel free to fix it, else, tell me and I will check. |
@Hywan for the segfault I think the only workaround is to disable code coverage for some time until we figure out why it produces segfaults. |
@jubianchi I cannot reproduce. This is probably an issue with Travis' build, don't you think? |
@Hywan if you try to reproduce it on OSX I think you won't be able to get the segfault. I never had any problem on OSX but people reported me many segfaults with xdebug on Linux (mainly Debian) and Travis envs. are built on top of it. |
@jubianchi Ok. So what's the status of this? |
Seems good to me. One last thing I did not explain in my last comment: there is another limitation on this feature. Take a look at the This is because of how PHP works and resolves names : if we mock something after it has been resolved once, we won't get the mocked value. The same applies to native function mocks. I'll let you merge here and update the doc. issue. |
@jubianchi Yup, need to update the documentation. |
0219a87
to
17ab828
Compare
The PHP mocker is splitted into 2 layers: 1. Abstract mocker, containing all namespaces logic, 2. Function mocker. The goal is to be able to introduce another mocker. So we must decouple this part first.
We use it the same way we use the function mocker: $this->constant->PHP_VERSION_ID = '606060'; // troll \o/
17ab828
to
6bc3c6c
Compare
The inspection completed: Array |
Fix #511.
In 2 steps
Split PHP mocker
The PHP mocker is splitted into 2 layers:
The goal is to be able to introduce another mocker. So we must decouple this part first.
Introduce the constant mocker
We use it the same way we use the function mocker:
Note: This is used to mock constants, not class constants.
Thoughts?