Skip to content

[WIP] Enhancement: Implement StrictClient #42

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

Closed
wants to merge 1 commit into from

Conversation

localheinz
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets fixes #41
Documentation todo
License MIT

What's in this PR?

This PR

  • introduces a StrictClient which allows configuration of a sequence of matchers, and which will throw an exception when a request was sent that was not expected

Example Usage

Without configured sequence
<?php

use Http\Mock\StrictClient;
use Nyholm\Psr7\Request;

$httpClient = new StrictClient();

var_dump($httpClient->hasCompletedSequence()); // true

// UnexpectedRequestException will be thrown
$httpClient->sendRequest(new Request(
    'GET',
    '/foo/bar'
));
With configured sequence and an unexpected additional request
<?php

use Http\Message\RequestMatcher\CallbackRequestMatcher;
use Http\Mock\StrictClient;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use Psr\Http\Message\RequestInterface;

$httpClient = new StrictClient();

var_dump($httpClient->hasCompletedSequence()); // true

$httpClient->on(
    new CallbackRequestMatcher(static function (RequestInterface $request): bool {
        return 'POST' === $request->getMethod() && '/foo/bar' === (string) $request->getUri();
    }),
    new Response(
        200,
        [],
        'Sounds good!'
    )
);

var_dump($httpClient->hasCompletedSequence()); // false

$httpClient->sendRequest(new Request(
    'POST',
    '/foo/bar'
));

var_dump($httpClient->hasCompletedSequence()); // true

// UnexpectedRequestException will be thrown
$httpClient->sendRequest(new Request(
    'POST',
    '/foo/bar'
));
With configured sequence and an unexpected request
<?php

use Http\Message\RequestMatcher\CallbackRequestMatcher;
use Http\Mock\StrictClient;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use Psr\Http\Message\RequestInterface;

$httpClient = new StrictClient();

var_dump($httpClient->hasCompletedSequence()); // true

$httpClient->on(
    new CallbackRequestMatcher(static function (RequestInterface $request): bool {
        return 'POST' === $request->getMethod() && '/foo/bar' === (string) $request->getUri();
    }),
    new Response(
        200,
        [],
        'Sounds good!'
    )
);

var_dump($httpClient->hasCompletedSequence()); // false

$httpClient->sendRequest(new Request(
    'POST',
    '/foo/bar'
));

var_dump($httpClient->hasCompletedSequence()); // true

// UnexpectedRequestException will be thrown
$httpClient->sendRequest(new Request(
    'GET',
    '/foo/bar'
));
With configured sequence, but not all requests have been sent yet
<?php

use Http\Message\RequestMatcher\CallbackRequestMatcher;
use Http\Mock\StrictClient;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use Psr\Http\Message\RequestInterface;

$httpClient = new StrictClient();

var_dump($httpClient->hasCompletedSequence()); // true

$httpClient->on(
    new CallbackRequestMatcher(static function (RequestInterface $request): bool {
        return 'POST' === $request->getMethod() && '/foo/bar' === (string) $request->getUri();
    }),
    new Response(
        200,
        [],
        'Sounds good!'
    )
);

$httpClient->on(
    new CallbackRequestMatcher(static function (RequestInterface $request): bool {
        return 'GET' === $request->getMethod() && '/foo/bar' === (string) $request->getUri();
    }),
    new Response(
        400,
        [],
        'Now is not a good time'
    )
);

var_dump($httpClient->hasCompletedSequence()); // false

$httpClient->sendRequest(new Request(
    'POST',
    '/foo/bar'
));

var_dump($httpClient->hasCompletedSequence()); // false
With configured sequence and all requests have been sent in correct order
<?php

use Http\Message\RequestMatcher\CallbackRequestMatcher;
use Http\Mock\StrictClient;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use Psr\Http\Message\RequestInterface;

$httpClient = new StrictClient();

var_dump($httpClient->hasCompletedSequence()); // true

$httpClient->on(
    new CallbackRequestMatcher(static function (RequestInterface $request): bool {
        return 'POST' === $request->getMethod() && '/foo/bar' === (string) $request->getUri();
    }),
    new Response(
        200,
        [],
        'Sounds good!'
    )
);

$httpClient->on(
    new CallbackRequestMatcher(static function (RequestInterface $request): bool {
        return 'GET' === $request->getMethod() && '/foo/bar' === (string) $request->getUri();
    }),
    new Response(
        400,
        [],
        'Now is not a good time'
    )
);

var_dump($httpClient->hasCompletedSequence()); // false

$httpClient->sendRequest(new Request(
    'POST',
    '/foo/bar'
));

var_dump($httpClient->hasCompletedSequence()); // false

$httpClient->sendRequest(new Request(
    'GET',
    '/foo/bar'
));

var_dump($httpClient->hasCompletedSequence()); // true

Checklist

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

@localheinz localheinz force-pushed the feature/strict-client branch 3 times, most recently from a43a2a3 to 285a71b Compare June 5, 2019 13:17
@localheinz localheinz force-pushed the feature/strict-client branch from 285a71b to afde8e5 Compare June 5, 2019 13:19
@localheinz
Copy link
Contributor Author

@dbu

I probably should have openend a draft pull request.

We're currently testing this in a project, perhaps we'll wait a bit to see how well this goes, and then I'll circle back to see if this needs improvements.

@localheinz localheinz changed the title Enhancement: Implement StrictClient [WIP] Enhancement: Implement StrictClient Jun 17, 2019
@localheinz localheinz closed this Mar 6, 2020
@localheinz localheinz deleted the feature/strict-client branch March 6, 2020 23:23
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.

Implement StrictClient that expects a sequence of requests
1 participant