Skip to content

Commit cb5673a

Browse files
authored
Merge pull request #88 from bbatsche/method-docs
Method Docs
2 parents 9263934 + 3c4d6a8 commit cb5673a

File tree

7 files changed

+72
-39
lines changed

7 files changed

+72
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased](https://github.com/bbatsche/Verify/compare/3.0.0...master)
88

9-
## [3.1.0](https://github.com/bbatsche/Verify/compare/3.0.0...3.1.0) - 2022-10-DD
9+
## [3.1.0](https://github.com/bbatsche/Verify/compare/3.0.0...3.1.0) - 2022-10-07
1010

1111
### Added
1212

docs/attributes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.. role:: php(code)
22
:language: php
33

4+
.. _attributes:
5+
46
====================
57
Attribute Assertions
68
====================

docs/extending.rst

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,27 @@ BeBat/Verify includes almost all the assertions built into PHPUnit, and all the
1010
Custom Constraint
1111
=================
1212

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>`__.
1414

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:
1616

1717
.. code-block:: php
1818
19-
use Helmich\JsonAssert\Constraint\JsonValueMatches;
20-
use Helmich\JsonAssert\Constraint\JsonValueMatchesSchema;
21-
use PHPUnit\Framework\Constraint\IsEqual;
19+
use Coduo\PHPMatcher\PHPUnit\PHPMatcherConstraint;
2220
2321
use function BeBat\Verify\verify;
2422
25-
// ...
26-
27-
$jsonDocument = [
28-
'id' => 1000,
29-
'username' => 'mhelmich',
30-
'given_name' => 'Martin',
31-
'family_name' => 'Helmich',
32-
'age' => 27,
33-
'hobbies' => [
34-
"Heavy Metal",
35-
"Science Fiction",
36-
"Open Source Software"
37-
]
38-
];
39-
40-
$schema = [
41-
'type' => 'object',
42-
'required' => ['username', 'age'],
43-
'properties' => [
44-
'username' => ['type' => 'string', 'minLength' => 3],
45-
'age' => ['type' => 'number']
46-
]
47-
];
48-
49-
verify($jsonDocument)->has()->constraint(new JsonValueMatchesSchema($schema))
50-
->and()->constraint(new JsonValueMatches('$.username', new IsEqual('mhelmich')));
51-
23+
verify('{"name": "Norbert"}')->has()
24+
->constraint(new PHPMatcherConstraint('{"name": "@string@"}'));
5225
5326
.. phpref:: extending.withVerifier
5427

5528
Custom Verifier
5629
===============
5730

58-
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()`.
5934

6035
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:
6136

@@ -65,7 +40,7 @@ The :php:`withVerifier()` method can also be used to switch between the value an
6540
6641
// ...
6742
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
7045
7146
For more details about writing your own verifier, see its :ref:`API documentation <verifier-api>`.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ BeBat/Verify is a small wrapper for PHPUnit's assertions, intended to make your
1414
modifiers
1515
chaining
1616
attributes
17+
methods
1718

1819
.. toctree::
1920
:maxdepth: 2

docs/methods.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
.. role:: php(code)
2+
:language: php
3+
4+
=================
5+
Method Assertions
6+
=================
7+
8+
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.
9+
10+
Return Values
11+
=============
12+
13+
A simple example might look like:
14+
15+
.. code-block:: php
16+
17+
verify($calculator)->add(2, 3)->will()->returnValue()->identicalTo(5);
18+
19+
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:
22+
23+
.. code-block:: php
24+
25+
verify(new ArrayObject([]))
26+
->method('empty')->will()->returnValue()->true()
27+
->method('count')->will()->returnValue()->identicalTo(0);
28+
29+
The :php:`with()` method can also be used to set up multiple example arguments for a single method:
30+
31+
.. code-block:: php
32+
33+
verify($calculator)->add()
34+
->with(1, $someValue)->will()->returnValue()->greaterThan($someValue)
35+
->with(0, $someValue)->will()->returnValue()->identicalTo($someValue)
36+
->with(-1, $someValue)->will()->returnValue()->lessThan($someValue);
37+
38+
Exceptions
39+
==========
40+
41+
If you need to test an exception thrown by your method, you may do so with :php:`throwException()` like so:
42+
43+
.. code-block:: php
44+
45+
verify($calculator)->divide($someValue, 0)
46+
->will()->throwException()->instanceOf(DivideByZeroException::class);
47+
48+
You can drill into more detail of your exception by using the :php:`withMessage()` and :php:`withCode()` methods:
49+
50+
.. code-block:: php
51+
52+
verify($calculator)->add(1, 'two')
53+
->will()->throwException()->instanceOf(InvalidArgumentException::class)
54+
->withMessage()->startWith('Invalid argument passed')
55+
->withCode()->identicalTo(2);

docs/verifier-api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You can add functionality to BeBat/Verify by creating a custom assertion class,
1717
1818
:returns: :php:class:`BeBat\\Verify\\API\\Assert` (extends :php:class:`PHPUnit\\Framework\\Assert`)
1919

20-
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.
2121

2222
.. php:method:: constraintFactory()
2323
@@ -74,7 +74,7 @@ You can add functionality to BeBat/Verify by creating a custom assertion class,
7474
7575
:returns: :php:`mixed`
7676

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.
7878

7979
.. php:method:: resetParams()
8080

src/API/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected function callMethod()
128128
// @phpstan-ignore-next-line
129129
return $this->actual->{$this->methodName}(...$this->methodArgs);
130130
}
131-
if ($reflector->hasMethod('__callStatic')) {
131+
if (\is_string($this->actual) && $reflector->hasMethod('__callStatic')) {
132132
$this->methodCalled = true;
133133

134134
// @phpstan-ignore-next-line

0 commit comments

Comments
 (0)