Skip to content

Commit

Permalink
Adding PHPUnit 11 support and allow Hybrid and Guzzler to run at once
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkelso committed Feb 3, 2024
1 parent 0436a17 commit 4bea27c
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 324 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.1.1] - 2024-02-03
- Update to support PHPUnit 11
- Fix type hinting in macros to support both Guzzler and Hybrid use at once

## [2.1.0] - 2023-07-14
- Changes support to PHPUnit 10. This version change required which classes within PHPUnit are used. As such, any apps still using PHPUnit 9 and below should lock to 2.0.3
- Dropping support for PHP 8.0, as PHPUnit 10 also dropped support for it.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"require": {
"php": ">=8.1",
"phpunit/phpunit": "^10.0",
"phpunit/phpunit": ">=10.0",
"guzzlehttp/guzzle": "^7.4.3",
"ext-json": "*",
"blastcloud/chassis": "^1.0.6"
Expand Down
539 changes: 275 additions & 264 deletions composer.lock

Large diffs are not rendered by default.

18 changes: 5 additions & 13 deletions src/Guzzler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Guzzler extends Chassis
/** @var MockHandler */
protected $mockHandler;

/** @var array [Expectation] */
protected $expectations = [];
/** @var Expectation[] */
protected array $expectations = [];

public function __construct(TestCase $testInstance)
{
Expand All @@ -35,9 +35,6 @@ public function __construct(TestCase $testInstance)

/**
* Create a client instance with the required handler stacks.
*
* @param array $options
* @return Client
*/
public function getClient(array $options = []): Client
{
Expand All @@ -50,27 +47,22 @@ public function getClient(array $options = []): Client

/**
* Get the handler stack to pass to a new Client instance.
*
* @return HandlerStack
*/
public function getHandlerStack()
public function getHandlerStack(): HandlerStack
{
return $this->handlerStack;
}

/**
* Create a new Expectation instance on which various pieces of the
* request can be asserted against.
*
* @param mixed $argument
* @return Expectation
*/
public function expects($argument)
public function expects(mixed $argument): \BlastCloud\Chassis\Expectation
{
return parent::expects($argument);
}

protected function createExpectation($argument = null)
protected function createExpectation($argument = null): \BlastCloud\Chassis\Expectation
{
return new Expectation($argument, $this);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Helpers/macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

use BlastCloud\Guzzler\Expectation;

Expectation::macro('synchronous', function (Expectation $e) {
Expectation::macro('synchronous', function (BlastCloud\Chassis\Expectation $e) {
return $e->withOption('synchronous', true);
});

Expectation::macro('asynchronous', function (Expectation $e) {
Expectation::macro('asynchronous', function (BlastCloud\Chassis\Expectation $e) {
// Set to null, because if the request was asynchronous, the
// "synchronous" key is not set in the options array.
return $e->withOption('synchronous', null);
});

foreach (Expectation::VERBS as $verb) { // @codeCoverageIgnore
Expectation::macro($verb, function (Expectation $e, $uri) use ($verb) {
Expectation::macro($verb, function (BlastCloud\Chassis\Expectation $e, $uri) use ($verb) {
return $e->withEndpoint($uri, strtoupper($verb));
});
}

Expectation::macro('endpoint', function (Expectation $e, $url, $method) {
Expectation::macro('endpoint', function (BlastCloud\Chassis\Expectation $e, $url, $method) {
return $e->withEndpoint($url, strtoupper($method));
});

Expectation::macro('withoutQuery', function (Expectation $e) {
Expectation::macro('withoutQuery', function (BlastCloud\Chassis\Expectation $e) {
return $e->withQuery([], true);
});
11 changes: 6 additions & 5 deletions src/UsesGuzzler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

namespace BlastCloud\Guzzler;

use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;

trait UsesGuzzler
{
public Guzzler $guzzler;

/**
* @before
*/
#[Before]
public function setUpGuzzler()
{
$this->guzzler = new Guzzler($this);
}

/**
* @after
* Run through the list of expectations that were made and
* evaluate all requests in the history. Closure::call()
* is used to hide this method from the user APIs.
*/
public function runGuzzlerAssertions()
#[After]
public function runGuzzlerAssertions(): void
{
(function () {
$this->runExpectations();
Expand Down
3 changes: 2 additions & 1 deletion tests/Filters/WithBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BlastCloud\Guzzler\Expectation;
use BlastCloud\Guzzler\UsesGuzzler;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
Expand All @@ -13,7 +14,7 @@ class WithBodyTest extends TestCase
{
use UsesGuzzler, ExceptionMessageRegex;

public $client;
public Client $client;

public function setUp(): void
{
Expand Down
4 changes: 2 additions & 2 deletions tests/GuzzlerAnnotationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GuzzlerAnnotationsTest extends \PHPUnit\Framework\TestCase
{
use UsesGuzzler;

public static $afterWasRun;
public static ?string $afterWasRun;

public function testWrapperIsSetupBeforeTest(): void
{
Expand All @@ -21,7 +21,7 @@ public function testExpectationsAreRunAfter()
{
$this->guzzler = new Class($this) extends Guzzler
{
public function runExpectations()
public function runExpectations(): void
{
GuzzlerAnnotationsTest::$afterWasRun = 'after has run';
}
Expand Down
8 changes: 3 additions & 5 deletions tests/Helpers/MacrosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
use Tests\ExceptionMessageRegex;

class MacrosTest extends TestCase
{
use UsesGuzzler, ExceptionMessageRegex;

/** @var Client */
public $client;
public Client $client;

public function setUp(): void
{
Expand Down Expand Up @@ -135,9 +135,7 @@ public function testOverrideInline()
$this->assertEquals(6, $this->guzzler->queueCount());
}

/**
* @runInSeparateProcess
*/
#[RunInSeparateProcess]
public function testOverrideProvidedMacro()
{
Expectation::macro('synchronous', function ($e) {
Expand Down
28 changes: 0 additions & 28 deletions tests/UsesGuzzlerTest.php

This file was deleted.

0 comments on commit 4bea27c

Please sign in to comment.