2017 © Pedro Peláez
 

library assert

Assertion library

image

joebengalen/assert

Assertion library

  • Tuesday, April 26, 2016
  • by JoeBengalen
  • Repository
  • 1 Watchers
  • 1 Stars
  • 35 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Assert

Build Status Coverage Status SensioLabsInsight Total Downloads License, (*1)

As we all know when using an existing library we wish some things would be a little different. To make sure I can use assertions exactly how I feel is best, I created my own implementation., (*2)

The assert methods are meant to be used to check whether incoming arguments are what you expect them to be. If the variable does not meet the assertion an InvalidArgumentException is thrown., (*3)

I needed all available assertions to be actual methods instead of virtual ones in the class PHPDoc, because virtual ones do not work properly with static methods in netbeans., (*4)

Installation

Via Composer, (*5)

``` bash $ composer require joebengalen/assert, (*6)


## Usage ```php use JoeBengalen\Assert\Assert; /** * @param int $arg1 * @param bool $arg2 */ function foo($arg1, $arg2) { // Make sure the arguments are indeed what they should be Assert::isInteger($arg1); Assert::isBoolean($arg2); // Actual code ... } // This will be fine ... foo(12, true); // ... but this will throw an InvalidArgumentException foo(2, 4);

Custom exception messages

Sometimes you may want to throw the exception with a different message. This can be done by passing a second string argument into the assertion method., (*7)

The error messages are run trough sprintf(), where the passed variable, transformed into a useful string, will be available., (*8)

Assert::isBoolean(3, 'Custom message: got %s, but expected boolean');
// Throws: "Custom message: got integer, but expected boolean"

Some assertion methods take additional arguments which are needed for the assertion. The custom message shall always be the latest argument. Additional arguments will also be passed into sprintf(), in the order of the arguments. This means the value to assert will always be the first string passed into sprintf()., (*9)

Assert::isInstanceOf(new Foo, 'Bar', 'Custom message: got %s, but expected instance of %s');
// Throws: "Custom message: got Foo, but expected instance of Bar"

If you want to use the arguments in a different order in the message you can swap them by referring to their number., (*10)

Assert::isInstanceOf(new Foo, 'Bar', 'Custom message: expected instance of %2$s, but got %s');
// Throws: "Custom message: expected instance of Bar, but got Foo"

For more information on swapping the arguments checkout the php documentation of sprintf()., (*11)

Change log

Please see the CHANGELOG for more information what has changed recently., (*12)

Testing

This project uses PHPUnit for testing. PHPUnit is not included as requirement, so to run the tests phpunit has to be manually installed. The reason for this is that I prefer having phpunit installed globally., (*13)

The Versions

26/04 2016

dev-master

9999999-dev https://github.com/JoeBengalen/Assert

Assertion library

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Martijn Wennink

assert assertion

30/08 2015

dev-feature-custom-exceptions

dev-feature-custom-exceptions https://github.com/JoeBengalen/Assert

Assertion library

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Martijn Wennink

assert assertion

28/08 2015

1.0.x-dev

1.0.9999999.9999999-dev https://github.com/JoeBengalen/Assert

Assertion library

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Martijn Wennink

assert assertion

28/08 2015

1.0.0

1.0.0.0 https://github.com/JoeBengalen/Assert

Assertion library

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Martijn Wennink

assert assertion