File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ vendor
2+ composer.lock
Original file line number Diff line number Diff line change 1+ language : php
2+ php :
3+ - ' 7.0'
4+ - ' 7.1'
5+ before_script : composer install --dev
6+ script :
7+ - composer test
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace TryPhp ;
3+
4+ class PredictExceptionFailure extends \Exception
5+ {
6+
7+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments