-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
Recommend using Prophecy for mocking #89
Comments
If I can give an opinion, Prophecy is nice and people that like it should be able to use it, as it's part of phpunit. Should we allow using prophecy in tests: 👍 |
In my experience it does not cause any issue, I already did it because lazyness, but totally agree with you, I would recommend not mixing them to avoid confusing people. |
Especially for |
This voting is for contributors only? In my opinion Sonata should move to prophecy because it can do the same but in a cleaner, concise and readable way... getMock will be deprecated, and we will have to refactor at some point the getMock calls in favor of getMockBuilder or prophesize, so I think it's a good point to start thinking in which direction should the test move. Examples of code: $this->admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
->disableOriginalConstructor()
->getMock();
$this->admin->expects($this->once())
->method('getModelManager')
->will($this->returnValue($this->modelManager));
$this->lockExtension->preUpdate($this->admin, $this->object); vs $this->admin = $this->prophesize('Sonata\AdminBundle\Admin\AbstractAdmin');
$this->admin->getModelManager()
->willReturn($this->modelManager);
$this->lockExtension->preUpdate($this->admin->reveal(), $this->object); You end up saving 2 lines per mock if you consider one mock and one mocked method which normally are more than just one. Another reason is that when you are using getMock, you can't mock classes, just interfaces. When you want to mock a class, you need to use getMockBuilder with the disableOriginalConstructor. With prophecy you can mock anything with the same code. Simpler. For the questions, I would vote all of them with a 👍 but taking into account that we should try to migrate to prophecy as we implement more test or we refactor some Test classes. Trying to move all the test to prophecy at the same time could be insane. For me, the test are just as important as the code that is being tested. And both needs to be refactor/improved at some point. |
@jordisala1991 Of course not! All opinions are welcomed! 👍 I like your comment. 😉 What about IDE completion for Prophecy? |
Thanks! @soullivaneuh. Hmm, I don't use any IDE for autocompletion. Actually using atom / sublimetext. You mean like this? https://github.com/maxfilatov/phpuaca The only negative point of prophecy is that is not well documented. It is easy to learn but when you don't remember something you have to look at the github README. AFAIK there is no more documentation, maybe @greg0ire knows. |
The documentation is not as good as phpunit's so you might end up reading the code in rare cases when you do advanced stuff, but the interface is more simple, more natural so you quickly don't need to read it. |
👍 |
I'll make one message per vote so that it is easy to count |
Should we allow using prophecy in tests ? |
Should we recommend using prophecy in new tests ? (SHOULD people use prophecy ?) |
Should we refuse phpunit mocks in new tests ? (MUST people use prophecy ?) |
Should we migrate all tests to prophecy, for consistency's sake ? |
Should we refuse mixing Prophecy and Mocks in the same test method ? |
About consistency, my point is : changing all the tests will be hard work, and refusing prophecy because we can't afford to change all the tests will be hard About refusing phpunit mocks, if a contributor works hard and provides something he has written with it, not sure he will want to amend his PR to switch… which means we will loose precious code coverage. |
@chalasr @jordisala1991 @sonata-project/contributors can you please cast your votes ? |
Should we refuse mixing Prophecy and Mocks in the same test class (having at least two methods using different mock systems)? |
If we decide to use Prophecy, there is my thinking:
After that, I didn't test Prophecy yet. I'll give you my feedback after sonata projects refactoring. |
@soullivaneuh : if you want to try it out, I suggest you try refactoring this test : https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/3.x/Tests/Datagrid/ProxyQueryTest.php |
From this page :
|
Actually I want to change my vote for this suggestion:
to "NO". Because for some methods we can just copy tests which are coded for similar methods. This will save time and we can use Prophecy to write tests for other methods. |
@ahmetakbn I don't fully understand your sentence. Could you please elaborate? |
For example here: https://github.com/sonata-project/SonataCoreBundle/pull/272/files I copied the test for the first method from test of another bundle. And I coded the rest of the tests. |
@ahmetakbn If we decide to use prophecy, it means that we move to something more modern and so basic mocking would be considered as the "old" way. |
@chalasr My only concern was not to write the same testing logic again if I have the option of just copying it, since we have mock and prophecy in our project. But I am ok with not mixing them in the same class also. |
@ahmetakbn Even if it's a copy, you are talking about a new test class. And we was talking about mixing method on the same test class, that should not be done.
If a developer can't write test at all, should we accept a PR without test? No. This is the same here. If a developer can not but want to learn, this is the good moment to learn him how to do. 👍 |
You're right. If one becomes the good, the other needs to be softly avoided. 👍 |
@jordisala1991 I'm not sure your example will work. The IDE completion is stuck because of the not recognized |
"Tools' constraints is out of the scope here!" |
If this avoid developer to code, it is not. 😉 |
BTW, should I close and sum up the poll? Or should we wait? |
Just wait for the moment. I did not get time to test. BTW, this not an emergency. 😉 |
Ok |
I would say: Use whatever works, I have used both Mocks and Prophecy and both have there place. Telling people witch solution to choose (without a good plausible reason) will only irritate them and creates a risk for a Golden Hammer Whenever you have a problem you look for one clear, minimal, plausible solution, every time. Giving recommendations and tips on how to best use a solution is always possible, but never force a solution, if the solution is good it will show it's purpose if not it will be replaced with something better. |
This can be also done with Prophecy, can't be?
Yes and no. We would like to provide TestCase to factorize test process (#116), and have both Prophecy and Mock usage on those classes would be a nightmare IMHO. |
Nope :) you cannot configure that you expect something to be called before something else, at least not straight forward, it requires some ugly hacks and breaks the philosophy of the Prophecy system. |
@sstok And what about this? https://github.com/Algatux/influxdb-bundle/blob/c234180f4a7af0d1951fe36c67225570456ac5d1/tests/unit/Events/Listeners/InfluxDbEventListenerTest.php#L22-L29 Or we are not talking about the same subject... |
I think we should pick one and stick with it for reusable test cases, and I think classical mocks would be the best fit for that, because people are more familiar with it. |
No I'm talking about the |
So for call order. I don't think we use it under sonata packages... |
@soullivaneuh can we reach a decision? I have to write some new tests, and using the basic API, even with good auto-completion, does not feel as much efficient as using Prophecy, and even if we do not resolve all decisions, knowing that using Prophecy will at least be allowed would be great. As per the vote it should be. |
@soullivaneuh Any news on this? |
Prophecy of Mock, we should restrict to one method. Apart of the system itself, moving to Prophecy will be heavier than keeping Mock because we will have both for a (long) time. After that, I'm not against this if a majority want it. The rules we should respect if we decide to use it:
Agree with that? Last thing: I would like a very simple vote on this comment: 👍 For moving to Prophecy Please share a lot this comment to have maximum of votes. After that, the documentation should be updated to make it official. And maybe a blog post (cc @rande)? |
I know I probably shouldn't jump to conclusions too fast, but after a thorough statistical analysis of the data at hand, I think it's fairly safe to say there might be some positive response regarding this proposal. I shall draft a new paragraph in the CONTRIBUTING.md soon, unless someone else beats me to it, in which case it would be great to give me a heads up about it. |
You are right @greg0ire 👍 |
See #198 |
How to run the test locally ? I didn't find any documentation about this |
FTR tests are (for now) run with phpunit as a global phar, running them with phpunit involves telling the bridge not to remove prophecy thanks to the |
Prophecy comes with phpunit since version 4.5 , and I think we should default to using it for mocking things, because it is way way more concise unless you are mocking a class with a no arguments constructor and adding no expectations.
So several questions follow this :
The text was updated successfully, but these errors were encountered: