You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/extending.rst
+10-35Lines changed: 10 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,52 +10,27 @@ BeBat/Verify includes almost all the assertions built into PHPUnit, and all the
10
10
Custom Constraint
11
11
=================
12
12
13
-
Constraints are the building blocks for both PHPUnit and BeBat/Verify's assertions. It is possible to write your own constraints by extending PHPUnit's `Constraint class <https://github.com/sebastianbergmann/phpunit/blob/9.5.20/src/Framework/Constraint/Constraint.php>`__.
13
+
Constraints are the building blocks for both PHPUnit and BeBat/Verify's assertions. It is possible to write your own constraints by extending PHPUnit's `Constraint class <https://github.com/sebastianbergmann/phpunit/blob/9.5.25/src/Framework/Constraint/Constraint.php>`__.
14
14
15
-
To assert a constraint, pass it to BeBat/Verify's :php:`constraint()` method after a conjunction, just like any other assertion. For example, if you had the package `helmich/phpunit-json-assert<https://packagist.org/packages/helmich/phpunit-json-assert>`__ installed:
15
+
To assert a constraint, pass it to BeBat/Verify's :php:`constraint()` method after a conjunction, just like any other assertion. For example, if you had the package `coduo/php-matcher<https://packagist.org/packages/coduo/php-matcher>`__ installed:
16
16
17
17
.. code-block:: php
18
18
19
-
use Helmich\JsonAssert\Constraint\JsonValueMatches;
20
-
use Helmich\JsonAssert\Constraint\JsonValueMatchesSchema;
21
-
use PHPUnit\Framework\Constraint\IsEqual;
19
+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherConstraint;
If there are multiple assertions you want to create, or your assertions involve more than one step, you should create your own *verifier* class. A verifier extends :php:`BeBat\Verify\API\Base` and includes one or more assertion methods. You can inject your verifier to BeBat/Verify by passing its class to :php:`withVerifier()`. BeBat/Verify will instantiate your verifier, passing it the current subject and its name, and then allow you to call your custom assertions from it.
31
+
Using a custom constraint works well if your assertion is a one off and relatively simple. For anything more complicated though you should create your own *verifier* class. A verifier extends :php:`BeBat\Verify\API\Base` and includes one or more assertion methods.
32
+
33
+
To use your verifier in an assertion chain, pass its class name to :php:`withVerifier()`. BeBat/Verify will instantiate your verifier and pass it the subject and its name. If the constructor requires any additional arguments they can be passed to :php:`withVerifier()`.
59
34
60
35
The :php:`withVerifier()` method can also be used to switch between the value and file verifiers. For example, suppose you were testing a method that created a file and returned its path. If you wanted to write assertions about both the file contents and its name, you could do so by switching between verifiers with the :php:`withVerifier()` method:
61
36
@@ -65,7 +40,7 @@ The :php:`withVerifier()` method can also be used to switch between the value an
65
40
66
41
// ...
67
42
68
-
verify($subject->writeFile())->will()->endWith('.log') // assertion about the file path
69
-
->and()->withVerifier(File::class)->will()->contain('My Log Message'); // assertion about the file contents
43
+
verify($subject->writeFile())->will()->endWith('.log') // assertion about the file path
44
+
->withVerifier(File::class)->contain('My Log Message'); // assertion about the file contents
70
45
71
46
For more details about writing your own verifier, see its :ref:`API documentation <verifier-api>`.
Just like with :ref:`attributes <attributes>`, assertions can be made about an object's methods by adding the method call after :php:`verify()`. You can write assertions about either a method's return value or an exception that the method throws.
In :php:`returnValue()`, BeBat/Verify will call :php:`add()` on the :php:`$calculator` object, passing it :php:`2` and :php:`3`, and then cache its result internally. This means you can write multiple assertions about the return value, just like other verifiers, without the method needing to be called again.
20
+
21
+
If your method name conflicts with part of the verifier API, you can use :php:`method()` and :php:`with()` to explicitly set a method name and arguments:
Get an instance of PHPUnit's `Assert class <https://github.com/sebastianbergmann/phpunit/blob/9.5.20/src/Framework/Assert.php>`__. This class exposes much of PHPUnit's functionality for writing tests & assertions, such as causing a test to fail if an error occurs.
20
+
Get an instance of PHPUnit's `Assert class <https://github.com/sebastianbergmann/phpunit/blob/9.5.25/src/Framework/Assert.php>`__. This class exposes much of PHPUnit's functionality for writing tests & assertions, such as causing a test to fail if an error occurs.
21
21
22
22
.. php:method:: constraintFactory()
23
23
@@ -74,7 +74,7 @@ You can add functionality to BeBat/Verify by creating a custom assertion class,
74
74
75
75
:returns::php:`mixed`
76
76
77
-
Resolve the actual value of the subject. If the subject is an attribute of a class, this method will resolve the actual value under test and cache it locally.
77
+
Resolve the actual value of the subject. You may override this method in your verifier if there is some additional logic to resolving your subject's value, such as reading the result from an object property or function call.
0 commit comments