Skip to content

[PhpUnitBridge] Add docs for ClassExistsMock #11903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions components/phpunit_bridge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,78 @@ conditions::
],
]);

Class Existence Based Tests
---------------------------

Tests that behave differently depending on existing classes, for example Composer's
development dependencies, are often hard to test for the alternate case. For that
reason, this component also provides mocks for these PHP functions:

* :phpfunction:`class_exists`
* :phpfunction:`interface_exists`
* :phpfunction:`trait_exists`

Use Case
~~~~~~~~

Consider the following example that relies on the ``Vendor\DependencyClass`` to
toggle a behavior::

use Vendor\DependencyClass;

class MyClass
{
public function hello(): string
{
if (class_exists(DependencyClass::class)) {
return 'The dependency bahavior.';
}

return 'The default behavior.';
}
}

A regular test case for ``MyClass`` (assuming the development dependencies
are installed during tests) would look like::

use MyClass;
use PHPUnit\Framework\TestCase;

class MyClassTest extends TestCase
{
public function testHello()
{
$class = new MyClass();
$result = $class->hello(); // "The dependency bahavior."

// ...
}
}

In order to test the default behavior instead use the
``ClassExistsMock::withMockedClasses()`` to configure the expected
classes, interfaces and/or traits for the code to run::

use MyClass;
use PHPUnit\Framework\TestCase;
use Vendor\DependencyClass;

class MyClassTest extends TestCase
{
// ...

public function testHelloDefault()
{
ClassExistsMock::register(MyClass::class);
ClassExistsMock::withMockedClasses([DependencyClass::class => false]);

$class = new MyClass();
$result = $class->hello(); // "The default bahavior."

// ...
}
}

Troubleshooting
---------------

Expand Down