Skip to content

Added tests for Httplug factory #66

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

Merged
merged 1 commit into from
Mar 3, 2016
Merged

Added tests for Httplug factory #66

merged 1 commit into from
Mar 3, 2016

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Mar 3, 2016

I did also rewrite the evaluateCondition function to support associative arrays.

This will address one issue in #64

@@ -95,11 +93,14 @@ protected function evaluateCondition($condition)
$evaluatedCondition = true;

// Immediately stop execution if the condition is false
for ($i = 0; $i < count($condition) && false !== $evaluatedCondition; ++$i) {
$evaluatedCondition &= $this->evaluateCondition($condition[$i]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can not see any reson why we need &=

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because $evaluatedCondition is checked before the next loop and breaks it if false.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not remove this as it is a wonderful solution for sparing that extra if you added.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will only "look nice". You will still do the same number of comparisons. The current solution does not support assoc. arrays.

Anyhow. I updated the PR so it gets the best of both worlds... What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same number of comparisons, but less branches IMO. LGTM.

{
public function testEvaluateCondition()
{
$method = new \ReflectionMethod(HttplugFactory::class, 'evaluateCondition');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct way would be mocking the Puli discovery, returning a mocked Class Binding and using it for testing here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And not using Reflection? Would that allow me to isolate the tests for evaluateCondition?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. You can mock the hasParameter and getParameter methods of the class binding.

Generally you should always test the public API. It is part of the public API that if a class binding has a depends parameter, it gets evaluated "somehow".

So what you really want to test in this case is what separate depends parameters return a valid class binding, which of them end up in an exception.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally you should always test the public API.

I know that is "generally" the idea. Your suggestion would require a lot of mocking and is very likely to break if the class changes. That's why I chose to use reflection to just test the protected function.

So what you really want to test in this case is what separate depends parameters return a valid class binding, which of them end up in an exception.

Haha. No 😄 I really wanted to only test the evaluateCondition and leave the rest of the class untested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion would require a lot of mocking and is very likely to break if the class changes.

What class?

I think it requires an acceptable amount of mocks. Not to mention you can reuse mock builders. (Even better: as of PHPUnit 4.5 you can use prophecy known from phpspec. You could even use phpspec in httplugbundle as well along with phpunit, that would probably be even better)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What class?

This, HttplugFactory

Okey,
I'll give it a try and we take it from there. =)

@Nyholm
Copy link
Member Author

Nyholm commented Mar 3, 2016

Ive updated the PR using Prophecy.

@Nyholm Nyholm force-pushed the plug_factory branch 2 times, most recently from 8503184 to 8fa2c99 Compare March 3, 2016 20:37
/**
* @var \Prophecy\Prophet
*/
private $prophet;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to manually instantiate a prophet object. Try $this->prophesize()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

sagikazarmark added a commit that referenced this pull request Mar 3, 2016
Added tests for Httplug factory
@sagikazarmark sagikazarmark merged commit 3f38b04 into master Mar 3, 2016
@sagikazarmark sagikazarmark deleted the plug_factory branch March 3, 2016 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants