https://github.com/tomkyle/mock-psr
Mock common PSR standard components in PhpUnit tests
https://github.com/tomkyle/mock-psr
Last synced: 4 months ago
JSON representation
Mock common PSR standard components in PhpUnit tests
- Host: GitHub
- URL: https://github.com/tomkyle/mock-psr
- Owner: tomkyle
- License: mit
- Created: 2021-09-21T09:11:17.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-06-28T11:26:45.000Z (about 1 year ago)
- Last Synced: 2025-09-02T11:40:03.459Z (10 months ago)
- Language: PHP
- Homepage:
- Size: 377 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
tomkyle · Mock PSR
**Mock common PSR components in PhpUnit Tests.**
[](https://github.com/tomkyle/mock-psr/actions/workflows/php.yml)
---
## Installation
```bash
$ composer require --dev tomkyle/mock-psr
```
## Usage
```php
mockServerRequest();
$attributes = array();
$headers = array();
$server_request = $this->mockServerRequest($attributes, $headers);
// Psr\Http\Message\UriInterface
$uri = $this->mockUri("https://test.com");
// Psr\Http\Message\RequestInterface
$request = $this->mockRequest("GET", $uri);
$request = $this->mockRequest("GET", "/home");
// Psr\Http\Message\StreamInterface
$stream = $this->mockStream("body string");
// Psr\Http\Message\ResponseInterface
$response = $this->mockResponse(200, $stream);
$response = $this->mockResponse(404, "body string");
}
}
```
### PSR-11 Container
Pass an optional array with things the Container has; calling *has* and *get* methods will behave like expected, including throwing `Psr\Container\NotFoundExceptionInterface`.
```php
mockContainer();
$container = $this->mockContainer([
'foo' => 'bar',
'qux' => 'baz'
]);
$container->has("foo"); // true
$container->has("hello"); // false
$container->get("hello"); // throws 'NotFoundExceptionInterface'
}
}
```
### PSR-15 RequestHandler
Includes *MockPsr7MessagesTrait*
```php
mockRequestHandler();
$response = $this->mockResponse(404, "body string");
$request_handler = $this->mockRequestHandler( $response );
}
}
```
### PSR-17 HTTP Factories
Includes *MockPsr7MessagesTrait*
```php
mockRequestFactory();
$request = $this->mockRequest();
$request_factory = $this->mockRequestFactory( $request );
// Psr\Http\Message\ResponseFactoryInterface
$response_factory = $this->mockResponseFactory();
$response = $this->mockResponse(404, "body string");
$response_factory = $this->mockResponseFactory( $response );
}
}
```
### PSR-18 HTTP Client
Includes *MockPsr7MessagesTrait*
```php
mockClient();
$response = $this->mockResponse(404, "body string");
$client = $this->mockClient( $response );
}
}
```
### PDO and PDOStatements
```php
mockPdoStatement($execution_result);
$stmt = $this->mockPdoStatement(true, array("foo" => "bar"));
// \PDO
$pdo = $this->mockPdo();
$pdo = $this->mockPdo($stmt);
$stmt_2 = $pdo->prepare("SELECT");
$stmt_2 == $stmt
}
}
```
## Development
### Run all tests
This packages has predefined test setups for code quality, code readability and unit tests. Check them out at the `scripts` section of **[composer.json](./composer.json)**.
```bash
$ composer test
# ... which includes
$ composer phpstan
$ composer phpcs
$ composer phpunit
```
### Unit tests
Default configuration is **[phpunit.xml.dist](./phpunit.xml.dist).** Create a custom **phpunit.xml** to apply your own settings.
Also visit [phpunit.readthedocs.io](https://phpunit.readthedocs.io/) · [Packagist](https://packagist.org/packages/phpunit/phpunit)
```bash
$ composer phpunit
# ... or
$ vendor/bin/phpunit
```
### PhpStan
Default configuration is **[phpstan.neon.dist](./phpstan.neon.dist).** Create a custom **phpstan.neon** to apply your own settings. Also visit [phpstan.org](https://phpstan.org/) · [GitHub](https://github.com/phpstan/phpstan) · [Packagist](https://packagist.org/packages/phpstan/phpstan)
```bash
$ composer phpstan
# ... which includes
$ vendor/bin/phpstan analyse
```
### PhpCS
Default configuration is **[.php-cs-fixer.dist.php](./.php-cs-fixer.dist.php).** Create a custom **.php-cs-fixer.php** to apply your own settings. Also visit [cs.symfony.com](https://cs.symfony.com/) · [GitHub](https://github.com/FriendsOfPHP/PHP-CS-Fixer) · [Packagist](https://packagist.org/packages/friendsofphp/php-cs-fixer)
```bash
$ composer phpcs
# ... which aliases
$ vendor/bin/php-cs-fixer fix --verbose --diff --dry-run
```
Apply all CS fixes:
```bash
$ composer phpcs:apply
# ... which aliases
$ vendor/bin/php-cs-fixer fix --verbose --diff
```
**On PHP 8.2, setting environment variable `PHP_CS_FIXER_IGNORE_ENV` is needed:**
```bash
$ PHP_CS_FIXER_IGNORE_ENV=1 composer phpcs
```