std/assert

Assertions to validate method input/output with nice error messages.

dev-master 2019-11-07 07:36 UTC

This package is auto-updated.

Last update: 2024-12-07 20:01:13 UTC


README

NOTE This is a pre-v1.0.0 beta release. Minor details may change soon, such as the exception class and/or other minor details. However these should be, with the exception of the exception class itself (as long as you do not rely on this particular implementation detail) backwards compatible. No guarantees, though.

How to use (quick start)

Install

composer require std/assert

Call anywhere (domain logic, entities, application, infrastructure...)

    public function mySweetBusinessLogic(string $value) 
    {
        return substr($value, 0, 1); // We got a bug!
    }

    // Fixed!
    public function mySweetBusinessLogic(string $value) 
    {
        \Std\Assert::minLength($value, 1);
        return substr($value, 0, 1); // No longer have a bug!
    }

Further Instructions

The assertion class is all static methods that throw \InvalidArgumentException if the supplied assertion is NOT true. In other words, your assertion, like a unit test, should be what the application expects. Don't get confused -- if a value must be true, then you should call Assert::true($val), not the other way around.

This can be used as "simplified argument handling", as well as anywhere else. It honestly took me a while to get used to calling assertions in this manner, as they feel like unit tests, and perhaps initially awkward feeling because of this fact. However, once you get used to the idea, you will understand their value as a simple method to perform sanity checks throughout your system (particularly, in your domain/business logic).

Every check within Webmozart has a corresponding "all" and "nullOr". These are handled by a __callStatic magic method (nothing wrong with that!), and are properly type-hinted and declared in the class docblock via @method.

allXXX, for example allNotEmpty will assert that each element of the given array is "true" to the given test. That is, allNotEmpty will assert that each element of the given array is not empty.

nullOrXXX will, believe it or not, assert that the value may be either null or the given assertion. For example, nullOrEmail will assert that the given value is either a valid e-mail address, or === null.

Webmozart & The future

This is effectively, and always will be, a clone of Webmozart\Assert by Bernhard Schussek bschussek@gmail.com

His package is available at webmozart/assert if you are interested, and this package currently relies upon it directly.

Original Documentation

Assertions

The [Assert] class provides the following assertions:

Type Assertions

Comparison Assertions

String Assertions

You should check that a value is a string with Assert::string() before making any of the following assertions.

File Assertions

Object Assertions

Array Assertions

Function Assertions

Collection Assertions

All of the above assertions can be prefixed with all*() to test the contents of an array or a \Traversable:

Assert::allIsInstanceOf($employees, 'Acme\Employee');

Nullable Assertions

All of the above assertions can be prefixed with nullOr*() to run the assertion only if it the value is not null:

Assert::nullOrString($middleName, 'The middle name must be a string or null. Got: %s');

Std\Assert

Back to Std\Assert ...

Future

In the future, I would like to:

  • Figure out a way to cleanly extend his work, but add a little more flexibility. Particularly, add a "not...()" + "allNot...()". Perhaps there is some anti-pattern or pattern I am missing here, but to simply assert something "is not a ..." is rather difficult. This is obviously preferable. If this just can't be done cleanly, I'll have to fork instead.
  • Add a few more high-level assertions.
  • Using this same code, create a new package for more informal validation package (ex. define API input validation via schema).
  • Using a similar system, define similar assertions with a correction path.

License

Most importantly, Webmozart is Copyright (c) 2014 Bernhard Schussek and released under the MIT license.

Any further additions or changes to his work within Std\Assert is freely available without attribution, available under any one of the following:

(C) Copyright 2019; A.B. Carroll ben@hl9.net

  • Unlicense http://unlicense.org (No Attribution Required)
  • MIT License
  • Apache 2.0 License
  • BSD 2-clause License

Please be mindful and credit the author of Webmozart\Assert (Bernhard Schussek), as Std\Assert is based upon it.