Skip to content

Commit 8796506

Browse files
committed
(pkg) init commit
1 parent eda1fca commit 8796506

8 files changed

Lines changed: 172 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: php
2+
php:
3+
- '7.0'
4+
- '7.1'
5+
before_script: composer install --dev
6+
script:
7+
- composer test

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [1.0.0] - 25.10.2017
10+
11+
### [Added]
12+
* `PredictExceptionTrait` with a `predictException` Method that checks if a capture throws a specific Kind of exception
13+
* Docs
14+
* Tests

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# predict-exception
2+
3+
> Simplified predictions if a piece of codes throws an Exception
4+
5+
[![Build Status](https://travis-ci.org/try-php/predict-exception.svg?branch=master)](https://travis-ci.org/try-php/predict-exception)
6+
7+
## Install
8+
9+
```bash
10+
$ composer require try/predict-exception
11+
```
12+
13+
## Usage
14+
15+
```php
16+
<?php
17+
require_once '/path/to/autoload.php';
18+
19+
use TryPhp\PredictExeptionTrait;
20+
21+
$assertions = new class() {
22+
use PredictExeptionTrait();
23+
}
24+
25+
$assertions->predictException(function () {
26+
throw new \RuntimeException('Oooops. Something broke.')
27+
}, \RuntimeException::class); // won't throw an exception
28+
29+
$assertions->predictException(function () {
30+
}, \Exception::class); // will throw an exception
31+
32+
$assertions->predictException(function () {
33+
throw new \RuntimeException('something else happened.');
34+
}, \Exception::class); // will throw an exception
35+
```
36+
37+
## API
38+
39+
### Methods
40+
41+
#### `predictException($capture, $exceptionClass)`
42+
43+
Method to check if a given piece of code throws an Exception of the expected type.
44+
45+
##### Arguments
46+
47+
| Arguments | Type | Description |
48+
|---|---|---|
49+
| $capture | `callable` | Closure in which the Exception shall be thrown. |
50+
| $exceptionClass | `string` | Class of the Exception that is expected to be thrown. |
51+
52+
## License
53+
54+
GPL-2.0 © Willi Eßer

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "try/predict-exception",
3+
"description": "Simplified predictions if a piece of codes throws an Exception",
4+
"type": "library",
5+
"license": "GPL-2.0",
6+
"authors": [
7+
{
8+
"name": "Willi Eßer",
9+
"email": "willi.esser@troublete.com"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"autoload": {
14+
"psr-4": {
15+
"TryPhp\\": "./src"
16+
}
17+
},
18+
"scripts": {
19+
"test": "@php test.php"
20+
},
21+
"require": {}
22+
}

src/PredictExceptionFailure.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace TryPhp;
3+
4+
class PredictExceptionFailure extends \Exception
5+
{
6+
7+
}

src/PredictExceptionTrait.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace TryPhp;
3+
4+
trait PredictExceptionTrait
5+
{
6+
/**
7+
* Method to assure that a piece of code executed inside of a capture
8+
* throws an exception and compare the type of it with a given class name
9+
* @param callable $capture
10+
* @param string $exceptionClass
11+
* @throws PredictExceptionFailure
12+
*/
13+
public function predictException(callable $capture, string $exceptionClass)
14+
{
15+
try {
16+
call_user_func($capture);
17+
throw new PredictExceptionFailure("Capture was expected to throw '$exceptionClass' but did not.");
18+
} catch (\Exception $ex) {
19+
$actualExceptionClass = get_class($ex);
20+
21+
if ($actualExceptionClass === PredictExceptionFailure::class) {
22+
throw $ex;
23+
}
24+
25+
if ($actualExceptionClass !== $exceptionClass) {
26+
throw new PredictExceptionFailure("Capture threw an Exception. Expected '$exceptionClass', but got '$actualExceptionClass'.");
27+
}
28+
}
29+
}
30+
}

test.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
require_once __DIR__ . '/vendor/autoload.php';
3+
4+
use TryPhp\PredictExceptionTrait;
5+
6+
$executionClass = new class() {
7+
use PredictExceptionTrait;
8+
};
9+
10+
try {
11+
$executionClass->predictException(function () {
12+
}, \Exception::class);
13+
trigger_error('test failed', E_USER_ERROR);
14+
} catch (\Exception $ex) {
15+
if ($ex->getMessage() !== "Capture was expected to throw 'Exception' but did not.") {
16+
trigger_error('test failed', E_USER_ERROR);
17+
}
18+
}
19+
20+
try {
21+
$executionClass->predictException(function () {
22+
throw new Exception('something happened');
23+
}, \Exception::class);
24+
} catch (\Exception $ex) {
25+
trigger_error('test failed', E_USER_ERROR);
26+
}
27+
28+
try {
29+
$executionClass->predictException(function () {
30+
throw new Exception('something happened');
31+
}, \RuntimeException::class);
32+
} catch (\Exception $ex) {
33+
if ($ex->getMessage() !== "Capture threw an Exception. Expected 'RuntimeException', but got 'Exception'.") {
34+
trigger_error('test failed', E_USER_ERROR);
35+
}
36+
}

0 commit comments

Comments
 (0)