Skip to content

Commit

Permalink
add expectations for plugins and request pagination (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonPurvis authored Jul 26, 2024
1 parent 66b2218 commit 6d47ed2
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 6 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 jp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

A PestPHP Plugin for SaloonPHP that helps you enforce architectural rules with your API integrations.

[![Static Analysis](https://github.com/JonPurvis/pest-plugin-saloon/actions/workflows/static.yml/badge.svg)](https://github.com/JonPurvis/pest-plugin-saloon/actions/workflows/static.yml)
[![Tests](https://github.com/JonPurvis/pest-plugin-saloon/actions/workflows/tests.yml/badge.svg)](https://github.com/JonPurvis/pest-plugin-saloon/actions/workflows/tests.yml)
[![Static Analysis](https://github.com/JonPurvis/lawman/actions/workflows/static.yml/badge.svg)](https://github.com/JonPurvis/lawman/actions/workflows/static.yml)
[![Tests](https://github.com/JonPurvis/lawman/actions/workflows/tests.yml/badge.svg)](https://github.com/JonPurvis/lawman/actions/workflows/tests.yml)
![GitHub last commit](https://img.shields.io/github/last-commit/jonpurvis/lawman)
![Packagist PHP Version](https://img.shields.io/packagist/dependency-v/jonpurvis/lawman/php)
![GitHub issues](https://img.shields.io/github/issues/jonpurvis/lawman)
![GitHub](https://img.shields.io/github/license/jonpurvis/lawman)
![Packagist Downloads](https://img.shields.io/packagist/dt/jonpurvis/lawman)

## Introduction
Lawman is a PestPHP Plugin for SaloonPHP which allows you to easily write architecture tests for your API integrations
Expand Down
1 change: 1 addition & 0 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_once __DIR__.'/Expectations/Cache.php';
require_once __DIR__.'/Expectations/Connector.php';
require_once __DIR__.'/Expectations/Pagination.php';
require_once __DIR__.'/Expectations/Plugin.php';
require_once __DIR__.'/Expectations/Properties.php';
require_once __DIR__.'/Expectations/RateLimit.php';
require_once __DIR__.'/Expectations/Request.php';
Expand Down
10 changes: 10 additions & 0 deletions src/Expectations/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@
->getReturnType()->getName())
->toEqual('Saloon\PaginationPlugin\Paginator')
);

expect()->extend(
'toUseRequestPagination',
fn (): Expectation => // @phpstan-ignore-next-line
$this->toImplement('Saloon\PaginationPlugin\Contracts\HasRequestPagination')
->and((new ReflectionClass($this->value)) // @phpstan-ignore-line
->getMethod('paginate')
->getReturnType()->getName())
->toEqual('Saloon\PaginationPlugin\Paginator')
);
11 changes: 11 additions & 0 deletions src/Expectations/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Pest\Arch\Contracts\ArchExpectation;

expect()->extend(
'toBeSaloonPlugin',
fn (): ArchExpectation => // @phpstan-ignore-next-line
$this->toBeTrait()
);
5 changes: 5 additions & 0 deletions tests/Feature/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@
expect('Tests\Fixtures\Arch\ToUseCustomPagination\ToUseCustomPagination')
->toUseCustomPagination();
});

it('checks that a class uses request pagination', function () {
expect('Tests\Fixtures\Arch\ToUseRequestPagination\ToUseRequestPagination')
->toUseRequestPagination();
});
8 changes: 8 additions & 0 deletions tests/Feature/PluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

it('saloon plugin', function () {
expect('Tests\Fixtures\Arch\ToBeSaloonPlugin\ToBeSaloonPlugin')
->toBeSaloonPlugin();
});
3 changes: 1 addition & 2 deletions tests/Fixtures/Arch/Requests/GetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class GetRequest extends Request
{
public function __construct(
protected string $test,
) {
}
) {}

/**
* Define the HTTP method
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Arch/Requests/PostRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class PostRequest extends Request
{
public function __construct(
protected string $test,
) {
}
) {}

/**
* Define the HTTP method
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/Arch/ToBeSaloonPlugin/ToBeSaloonPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToBeSaloonPlugin;

use Saloon\Http\PendingRequest;

trait ToBeSaloonPlugin
{
public function bootToBeSaloonPlugin(PendingRequest $pendingRequest): void
{
$pendingRequest->headers()->add('Saloon', 'Plugin');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Arch\ToUseRequestPagination;

use Saloon\Http\Connector;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\PaginationPlugin\Contracts\HasRequestPagination;
use Saloon\PaginationPlugin\PagedPaginator;
use Saloon\PaginationPlugin\Paginator;

class ToUseRequestPagination extends Connector implements HasRequestPagination
{
public function paginate(Connector $connector): Paginator
{
return new class(connector: $connector, request: $this) extends PagedPaginator
{
protected function isLastPage(Response $response): bool
{
return is_null($response->json('next_page_url'));
}

protected function getPageItems(Response $response, Request $request): array
{
return $response->json('items');
}
};
}

public function resolveBaseUrl(): string
{
return '';
}
}

0 comments on commit 6d47ed2

Please # to comment.