Skip to content

Add Mock Client strategy. #90

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 3 commits into from
Feb 12, 2017
Merged

Conversation

samrap
Copy link

@samrap samrap commented Jan 27, 2017

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets mentioned in #89
Documentation php-http/documentation#173
License MIT

What's in this PR?

Please see #89 for a full description of this PR.

Essentially, we need a way to test DI within our tests which in many cases cannot be fully implemented without having some way to discover the Mock Client. This new class allows us to add the MockClientStrategy to the HttpClientDiscovery's strategies as shown in @Nyholm 's example in the above mentioned PR.

Example Usage

use ThirdPartyService

class ThirdPartyServiceTest extends PHPUnit_Framework_TestCase
{
    public function setUpBeforeClass()
    {
        HttpClientDiscovery::prependStrategy(MockClientStrategy::class);
    }

    public function setUp()
    {
        $this->service = new ThirdPartyService;
    }
}

Checklist

  • Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix
  • Documentation pull request created (if not simply a bugfix)

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Good. Make sure to create a PR to the docs explaining this strategy, why, how and that it is not loaded by default.

* Find the Mock client.
*
* @internal
* @final
Copy link
Member

Choose a reason for hiding this comment

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

No need for final annotation

Copy link
Author

Choose a reason for hiding this comment

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

👍

/**
* Find the Mock client.
*
* @internal
Copy link
Member

Choose a reason for hiding this comment

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

Is this strategy really internal? The others are, but it is meant that this class should be used by others, right?

*/
public static function getCandidates($type)
{
return ($type === 'Http\Client\HttpClient')
Copy link
Member

Choose a reason for hiding this comment

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

You could use the ::class constant here

Copy link
Author

Choose a reason for hiding this comment

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

Good call, changing that now.

@sagikazarmark
Copy link
Member

sagikazarmark commented Jan 27, 2017

@samrap thanks for your contribution.

What do you think about #89 (comment) ?

@samrap
Copy link
Author

samrap commented Jan 27, 2017

@sagikazarmark I prefer sticking with a manual setup in the test class i.e:

public function setUp()
{
    HttpClientDiscovery::prependStrategy(MockClientStrategy::class);
}

Adding a trait or method(s) to the Mock Client to add itself to the Discovery means the strategy now depends on the Discovery which seems like an unnecessary dependency to me. Unless I'm understanding this wrong.

As far as @barryvdh suggestion for Discovery::enableMockClient(), I like that idea. What do you think?

@sagikazarmark
Copy link
Member

Adding a trait or method(s) to the Mock Client to add itself to the

I rather meant a trait or base class for PHPUnit test cases, but I realized that MockClient is not limited to be used in PHPUnit, so that's probably not really good idea.

@Nyholm
Copy link
Member

Nyholm commented Jan 27, 2017

I do not like Discovery::enableMockClient. No discovery strategy should be treated in special manners.

@samrap
Copy link
Author

samrap commented Jan 27, 2017

@Nyholm good point. So I think we should keep this as is, and I will work on getting those docs completed.

@samrap
Copy link
Author

samrap commented Jan 31, 2017

Just FYI still active on this, been busy at work but will get these docs by end of week.

@samrap
Copy link
Author

samrap commented Feb 7, 2017

See php-http/documentation#173 👍

@Nyholm
Copy link
Member

Nyholm commented Feb 12, 2017

Thank you for this PR.

@Nyholm Nyholm merged commit 101c68e into php-http:master Feb 12, 2017
Nyholm pushed a commit to php-http/documentation that referenced this pull request Feb 12, 2017
* Add documentation for the MockClientStrategy

See [#90](php-http/discovery#90).

* Update discovery.rst

* Break lines at 80 characters.
@samrap
Copy link
Author

samrap commented Feb 13, 2017

Thank you guys!

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.

4 participants