Skip to content
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

chubbyphp-mock-2.x #25

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
- name: sonarcloud.io
uses: sonarsource/sonarqube-scan-action@v4.1.0
uses: sonarsource/sonarqube-scan-action@v5.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ A minimal, highly [performant][1] middleware [PSR-15][8] microframework built wi
## Requirements

* php: ^8.2
* [chubbyphp/chubbyphp-http-exception][20]: ^1.1
* [chubbyphp/chubbyphp-http-exception][20]: ^1.2
* [psr/container][21]: ^1.1.2|^2.0.2
* [psr/http-factory-implementation][22]: ^1.0
* [psr/http-factory][23]: ^1.1
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"require": {
"php": "^8.2",
"chubbyphp/chubbyphp-http-exception": "^1.1",
"chubbyphp/chubbyphp-http-exception": "^1.2",
"psr/container": "^1.1.2|^2.0.2",
"psr/http-factory-implementation": "^1.0",
"psr/http-factory": "^1.1",
Expand All @@ -35,18 +35,18 @@
},
"require-dev": {
"chubbyphp/chubbyphp-dev-helper": "dev-master",
"chubbyphp/chubbyphp-mock": "^1.8",
"chubbyphp/chubbyphp-mock": "^2.0@dev",
"guzzlehttp/psr7": "^2.7",
"http-interop/http-factory-guzzle": "^1.2",
"infection/infection": "^0.29.8",
"infection/infection": "^0.29.13",
"laminas/laminas-diactoros": "^3.5",
"nyholm/psr7": "^1.8.2",
"php-coveralls/php-coveralls": "^2.7.0",
"phpstan/extension-installer": "^1.4.3",
"phpstan/phpstan": "^2.0.3",
"phpunit/phpunit": "^11.5.0",
"phpstan/phpstan": "^2.1.6",
"phpunit/phpunit": "^11.5.10",
"slim/psr7": "^1.7",
"sunrise/http-message": "^3.2"
"sunrise/http-message": "^3.4.2"
},
"autoload": {
"psr-4": { "Chubbyphp\\Framework\\": "src/" }
Expand Down
3 changes: 0 additions & 3 deletions tests/Integration/MiddlewareDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Chubbyphp\Framework\Middleware\SlimCallbackMiddleware;
use Chubbyphp\Framework\RequestHandler\CallbackRequestHandler;
use Chubbyphp\Framework\RequestHandler\SlimCallbackRequestHandler;
use Chubbyphp\Mock\MockByCallsTrait;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -24,8 +23,6 @@
*/
final class MiddlewareDispatcherTest extends TestCase
{
use MockByCallsTrait;

public function testCallback(): void
{
$responseFactory = new SlimResponseFactory();
Expand Down
115 changes: 57 additions & 58 deletions tests/Unit/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Chubbyphp\Framework\Emitter\EmitterInterface;
use Chubbyphp\Framework\Middleware\MiddlewareDispatcherInterface;
use Chubbyphp\Framework\Router\RouteInterface;
use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use Chubbyphp\Mock\MockMethod\WithCallback;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockObjectBuilder;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -24,104 +26,101 @@
*/
final class ApplicationTest extends TestCase
{
use MockByCallsTrait;

public function testInvoke(): void
{
/** @var MiddlewareInterface|MockObject $routeIndependMiddleware */
$routeIndependMiddleware = $this->getMockByCalls(MiddlewareInterface::class);
$builder = new MockObjectBuilder();

/** @var MiddlewareInterface $routeIndependentMiddleware */
$routeIndependentMiddleware = $builder->create(MiddlewareInterface::class, []);

/** @var MiddlewareInterface|MockObject $middleware */
$middleware = $this->getMockByCalls(MiddlewareInterface::class);
/** @var MiddlewareInterface $middleware */
$middleware = $builder->create(MiddlewareInterface::class, []);

/** @var MockObject|RequestHandlerInterface $handler */
$handler = $this->getMockByCalls(RequestHandlerInterface::class);
$handler = $builder->create(RequestHandlerInterface::class, []);

/** @var MockObject|RouteInterface $route */
$route = $this->getMockByCalls(RouteInterface::class, [
Call::create('getMiddlewares')->with()->willReturn([$middleware]),
Call::create('getRequestHandler')->with()->willReturn($handler),
$route = $builder->create(RouteInterface::class, [
new WithReturn('getMiddlewares', [], [$middleware]),
new WithReturn('getRequestHandler', [], $handler),
]);

/** @var MockObject|ServerRequestInterface $request */
$request = $this->getMockByCalls(ServerRequestInterface::class, [
Call::create('getAttribute')->with('route', null)->willReturn($route),
$request = $builder->create(ServerRequestInterface::class, [
new WithReturn('getAttribute', ['route', null], $route),
]);

/** @var MockObject|ResponseInterface $response */
$response = $this->getMockByCalls(ResponseInterface::class);

/** @var MiddlewareDispatcherInterface|MockObject $middlewareDispatcher */
$middlewareDispatcher = $this->getMockByCalls(MiddlewareDispatcherInterface::class, [
Call::create('dispatch')
->willReturnCallback(
static function (
array $middlewares,
RequestHandlerInterface $requestHandler,
ServerRequestInterface $request
) use ($routeIndependMiddleware) {
self::assertSame([$routeIndependMiddleware], $middlewares);

return $requestHandler->handle($request);
}
),
Call::create('dispatch')->with([$middleware], $handler, $request)->willReturn($response),
$response = $builder->create(ResponseInterface::class, []);

/** @var MiddlewareDispatcherInterface $middlewareDispatcher */
$middlewareDispatcher = $builder->create(MiddlewareDispatcherInterface::class, [
new WithCallback(
'dispatch',
static function (array $middlewares, RequestHandlerInterface $requestHandler, ServerRequestInterface $req) use ($routeIndependentMiddleware): ResponseInterface {
TestCase::assertSame([$routeIndependentMiddleware], $middlewares);

return $requestHandler->handle($req);
}
),
new WithReturn('dispatch', [[$middleware], $handler, $request], $response),
]);

$application = new Application([
$routeIndependMiddleware,
$routeIndependentMiddleware,
], $middlewareDispatcher);

self::assertSame($response, $application($request));
}

public function testHandle(): void
{
/** @var MiddlewareInterface|MockObject $routeIndependMiddleware */
$routeIndependMiddleware = $this->getMockByCalls(MiddlewareInterface::class);
$builder = new MockObjectBuilder();

/** @var MiddlewareInterface $routeIndependentMiddleware */
$routeIndependentMiddleware = $builder->create(MiddlewareInterface::class, []);

/** @var MockObject|ServerRequestInterface $request */
$request = $this->getMockByCalls(ServerRequestInterface::class);
$request = $builder->create(ServerRequestInterface::class, []);

/** @var MockObject|ResponseInterface $response */
$response = $this->getMockByCalls(ResponseInterface::class);

/** @var MiddlewareDispatcherInterface|MockObject $middlewareDispatcher */
$middlewareDispatcher = $this->getMockByCalls(MiddlewareDispatcherInterface::class, [
Call::create('dispatch')
->willReturnCallback(
static function (
array $middlewares,
RequestHandlerInterface $requestHandler,
ServerRequestInterface $request
) use ($routeIndependMiddleware) {
self::assertSame([$routeIndependMiddleware], $middlewares);

return $requestHandler->handle($request);
}
),
$response = $builder->create(ResponseInterface::class, []);

/** @var MiddlewareDispatcherInterface $middlewareDispatcher */
$middlewareDispatcher = $builder->create(MiddlewareDispatcherInterface::class, [
new WithCallback(
'dispatch',
static function (array $middlewares, RequestHandlerInterface $requestHandler, ServerRequestInterface $req) use ($routeIndependentMiddleware): ResponseInterface {
TestCase::assertSame([$routeIndependentMiddleware], $middlewares);

return $requestHandler->handle($req);
}
),
]);

/** @var MockObject|RequestHandlerInterface $requestHandler */
$requestHandler = $this->getMockByCalls(RequestHandlerInterface::class, [
Call::create('handle')->with($request)->willReturn($response),
$requestHandler = $builder->create(RequestHandlerInterface::class, [
new WithReturn('handle', [$request], $response),
]);

$application = new Application([
$routeIndependMiddleware,
$routeIndependentMiddleware,
], $middlewareDispatcher, $requestHandler);

self::assertSame($response, $application->handle($request));
}

#[DoesNotPerformAssertions]
public function testEmit(): void
{
$builder = new MockObjectBuilder();

/** @var MockObject|ResponseInterface $response */
$response = $this->getMockByCalls(ResponseInterface::class);
$response = $builder->create(ResponseInterface::class, []);

/** @var EmitterInterface|MockObject $emitter */
$emitter = $this->getMockByCalls(EmitterInterface::class, [
Call::create('emit')->with($response),
/** @var EmitterInterface $emitter */
$emitter = $builder->create(EmitterInterface::class, [
new WithReturn('emit', [$response], null),
]);

$application = new Application([], null, null, $emitter);
Expand Down
38 changes: 19 additions & 19 deletions tests/Unit/Emitter/EmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ function header(string $header, bool $replace = true, ?int $http_response_code =
{
use Chubbyphp\Framework\Emitter\Emitter;
use Chubbyphp\Framework\Emitter\TestHeader;
use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use PHPUnit\Framework\MockObject\MockObject;
use Chubbyphp\Mock\MockMethod\WithCallback;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockObjectBuilder;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand All @@ -52,26 +52,26 @@ function header(string $header, bool $replace = true, ?int $http_response_code =
*/
final class EmitterTest extends TestCase
{
use MockByCallsTrait;

public function testEmit(): void
{
/** @var MockObject|StreamInterface $responseBody */
$responseBody = $this->getMockByCalls(StreamInterface::class, [
Call::create('isSeekable')->with()->willReturn(true),
Call::create('rewind')->with(),
Call::create('eof')->with()->willReturn(false),
Call::create('read')->with(256)->willReturn('sample body'),
Call::create('eof')->with()->willReturn(true),
$builder = new MockObjectBuilder();

/** @var StreamInterface $responseBody */
$responseBody = $builder->create(StreamInterface::class, [
new WithReturn('isSeekable', [], true),
new WithCallback('rewind', static fn () => null),
new WithReturn('eof', [], false),
new WithReturn('read', [256], 'sample body'),
new WithReturn('eof', [], true),
]);

/** @var MockObject|ResponseInterface $response */
$response = $this->getMockByCalls(ResponseInterface::class, [
Call::create('getStatusCode')->with()->willReturn(200),
Call::create('getProtocolVersion')->with()->willReturn('1.1'),
Call::create('getReasonPhrase')->with()->willReturn('OK'),
Call::create('getHeaders')->with()->willReturn(['X-Name' => ['value1', 'value2']]),
Call::create('getBody')->with()->willReturn($responseBody),
/** @var ResponseInterface $response */
$response = $builder->create(ResponseInterface::class, [
new WithReturn('getStatusCode', [], 200),
new WithReturn('getProtocolVersion', [], '1.1'),
new WithReturn('getReasonPhrase', [], 'OK'),
new WithReturn('getHeaders', [], ['X-Name' => ['value1', 'value2']]),
new WithReturn('getBody', [], $responseBody),
]);

$emitter = new Emitter();
Expand Down
23 changes: 11 additions & 12 deletions tests/Unit/Middleware/CallbackMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace Chubbyphp\Tests\Framework\Unit\Middleware;

use Chubbyphp\Framework\Middleware\CallbackMiddleware;
use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use PHPUnit\Framework\MockObject\MockObject;
use Chubbyphp\Mock\MockMethod\WithReturn;
use Chubbyphp\Mock\MockObjectBuilder;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -20,19 +19,19 @@
*/
final class CallbackMiddlewareTest extends TestCase
{
use MockByCallsTrait;

public function testHandle(): void
{
/** @var MockObject|ServerRequestInterface $request */
$request = $this->getMockByCalls(ServerRequestInterface::class);
$builder = new MockObjectBuilder();

/** @var ServerRequestInterface $request */
$request = $builder->create(ServerRequestInterface::class, []);

/** @var MockObject|ResponseInterface $response */
$response = $this->getMockByCalls(ResponseInterface::class);
/** @var ResponseInterface $response */
$response = $builder->create(ResponseInterface::class, []);

/** @var MockObject|RequestHandlerInterface $handler */
$handler = $this->getMockByCalls(RequestHandlerInterface::class, [
Call::create('handle')->with($request)->willReturn($response),
/** @var RequestHandlerInterface $handler */
$handler = $builder->create(RequestHandlerInterface::class, [
new WithReturn('handle', [$request], $response),
]);

$middleware = new CallbackMiddleware(
Expand Down
Loading