Set
s¶
So far you've seen hardcoded Set
s via Set\Element
and integers one via Set\Integers
. But there are much more.
- Primitives
Innmind\BlackBox\Set\Chars
Innmind\BlackBox\Set\Integers
Innmind\BlackBox\Set\IntegersExceptZero
Innmind\BlackBox\Set\NaturalNumbers
Innmind\BlackBox\Set\NaturalNumbersExceptZero
Innmind\BlackBox\Set\Nullable
Innmind\BlackBox\Set\RealNumbers
Innmind\BlackBox\Set\Strings
Innmind\BlackBox\Set\Type
Innmind\BlackBox\Set\Unicode
Innmind\BlackBox\Set\UnsafeStrings
- User defined values
- Higher order
Set
s - Specific types
Tip
You can find more on Packagist.
Common methods¶
Map¶
For all Set
objects you can transform the generated values with a function of your own.
Let's say the code you want to prove needs a Password
object. You can wrap strings in your object like this:
use Innmind\BlackBox\Set;
$set = Set\Strings::any()
->map(static fn(string $string) => new Password($string));
Now if you use $set
in a proof it will generate instances of Password
with a random string inside it.
Filter¶
To reuse the password example from above. Say that your password needs to contain the character $
. You can do:
use Innmind\BlackBox\Set;
$set = Set\Strings::any()
->filter(static fn(string $string) => \str_contains($string, '$'))
->map(static fn(string $string) => new Password($string));
Warning
This is an example. You should not enforce your passwords to have a specific value in it. The strength is based on length. (US and French recommendations)
Primitives¶
Chars¶
This Set
can generate strings containing a single character.
Chars::any()
describes any chars that can be returned by the\chr()
functionChars::lowercaseLetter()
describes the rangea..z
Chars::uppercaseLetter()
describes the rangeA..Z
Chars::number()
describes the range0..9
Chars::ascii()
describes any character that you can typically find on your keyboardChars::alphanumerical()
describes any character from::lowercaseLetter()
,::uppercaseLetter()
or::number()
Integers¶
Integers::any()
describes any integer between\PHP_INT_MIN
and\PHP_INT_MAX
Integers::between(min, max)
describes any integer between the bounds you specifyIntegers::above(min)
Integers::below(max)
The bounds are included in the values that can be generated
IntegersExceptZero¶
IntegersExceptZero::any()
describes any integer except 0
NaturalNumbers¶
NaturalNumbers::any()
is the same as Integers::above(0)
NaturalNumbersExceptZero¶
NaturalNumbersExceptZero::any()
is the same as Integers::above(1)
Nullable¶
Nullable::of(Set)
describes all the values that can be generated by the Set
passed as argument and null
RealNumbers¶
RealNumbers::any()
describes any float between\PHP_INT_MIN
and\PHP_INT_MAX
RealNumbers::between(min, max)
describes any float between the bounds you specifyRealNumbers::above(min)
RealNumbers::below(max)
The bounds are included in the values that can be generated
Strings¶
Strings::any()
describes any string of a length between0
and128
containing any character fromChars::any()
Strings::between(min, max)
same as::any()
but you specify the length rangeStrings::atMost(max)
Strings::atLeast(min)
Strings::madeOf(Set)
describes any string made of the characters you specify (ieStrings::madeOf(Chars::alphanumerical())
)- you can specify the length range via
Strings::madeOf(Set)->between(min, max)
- you can specify the length range via
Type¶
Type::any()
describes any type that is supported by PHP. This is useful to prove a code doesn't depend on the type of its arguments.
Unicode¶
Unicode::strings()
is the same asStrings::madeOf(Unicode::any())
Unicode::any()
describes any single unicode characterUnicode
provides all the unicode blocks
UnsafeStrings¶
UnsafeStrings::any()
describes any string that could break your code. You can use this to test the robustness of your code.
User defined values¶
Elements¶
Elements::of(...values)
describes all the values that you put in (ie Elements::of(true, false)
to describe booleans)
FromGenerator¶
FromGenerator::of(callable)
describes values that you will provide via a Generator
Higher order Set
s¶
Decorate¶
Decorate::immutable(callable, Set)
is a way to transform the values that can be generated by the given Set
(ie Decorate::immutable(\chr(...), Integers::between(0, 255))
describes all the strings that can be generated by \chr()
)
This is the same as Integers::between(0, 255)->map(\chr(...))
.
Composite¶
This Set
allows to aggregate multiple values to a new one. Let's say you have a User
class, you could desribe it via:
Set\Composite::immutable(
static fn(string $firstname, string $lastname) => new User(
$firstname,
$lastname,
),
Strings::atLeast(1),
Strings::atLeast(1),
);
Any additionnal Set
provided will give access to a new argument to the callable.
Either¶
You can think of this Set
as an OR. Either::any(Integers::any(), Strings::any())
describes any integer or string.
Sequence¶
Sequence::of(Set)
describes a list (an array of consecutive values) of values of the given Set
type. Sequence::of(Integers::any())
describes any list of integers.
By default the list contains between 0
and 100
elements, you can change this via Sequence::of(Set)->between(min, max)
.
The bounds are included.
Tuple¶
This is a special case of Composite
. Both examples does the same thing.
Call¶
This set is useful when building the Model to tests via properties. If BlackBox shrinks properties it will call the provided callable at each shrinking step. This allows to get rid of any state inside your Model between each run.
Specific types¶
Email¶
Email::any()
describes any valid email string
Uuid¶
Uuid::any()
describes any valid UUID