Skip to content

Add support for types in expectFilter mocks #254

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
Show file tree
Hide file tree
Changes from 3 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
47 changes: 47 additions & 0 deletions docs/usage/mocking-wp-action-and-filter-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,51 @@ final class MyClassTest extends TestCase
$this->assertEquals('Default', (new MyClass())->filterContent());
}
}
```

## Asserting that an object has been passed

To assert that an object has been added as an argument, you can perform assertions referencing the object's class type.

Take the code below, for example:

```php
namespace MyPlugin;

class MyClass
{
public function filterContent() : NewClass
{
return apply_filters('custom_content_filter', new NewClass());
}
}

class NewClass
{
public function __construct()
{
echo 'New Class';
}
}
```

We can do this:

```php
use WP_Mock;
use WP_Mock\Tools\TestCase as TestCase;

use MyPlugin\MyClass;
use MyPlugin\NewClass;

final class MyClassTest extends TestCase
{
public function testAnonymousObject() : void
{
WP_Mock::expectFilter('custom_content_filter', WP_Mock\Functions::type(NewClass::class));

$this->assertInstanceOf(NewClass::class, (new MyClass())->filterContent());
$this->assertConditionsMet();
}
}
```
5 changes: 4 additions & 1 deletion php/WP_Mock/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public static function anyOf(): AnyOf
*/
public static function type(string $expected): Type
{
return Mockery::type($expected);
$type = Mockery::type($expected);
Filter::$objects[ $expected ] = spl_object_hash($type);

return $type;
}
}
11 changes: 11 additions & 0 deletions php/WP_Mock/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
/** @var array<mixed> collection of processors */
protected $processors = [];

/** @var array<mixed> collection of objects mapped to their Type hashes */
public static array $objects = [];

/**
* Hook constructor.
*
Expand Down Expand Up @@ -62,6 +65,14 @@
}

if (is_object($value)){
if (! $value instanceof Type) {
$class = get_class($value);

if (isset(static::$objects[ $class ])) {
return static::$objects[ $class ];

Check failure on line 72 in php/WP_Mock/Hook.php

View workflow job for this annotation

GitHub Actions / php-static-analysis

Method WP_Mock\Hook::safe_offset() should return string but returns mixed.
}
}

return spl_object_hash($value);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/WP_Mock/TestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace WP_Mock\Tests\Unit\WP_Mock;

class TestClass {
public function __construct() {
//Do nothing...
}
}
36 changes: 29 additions & 7 deletions tests/Unit/WP_MockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace WP_Mock\Tests\Unit;

use Generator;
use Mockery;
use Mockery\Exception\InvalidCountException;
use Mockery\ExpectationInterface;
use WP_Mock;
use stdClass;
use Generator;
use PHPUnit\Framework\Exception;
use Mockery\ExpectationInterface;
use WP_Mock\Tests\WP_MockTestCase;
use WP_Mock\DeprecatedMethodListener;
use WP_Mock\Tests\Unit\WP_Mock\TestClass;
use Mockery\Exception\InvalidCountException;
use PHPUnit\Framework\ExpectationFailedException;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use stdClass;
use WP_Mock;
use WP_Mock\DeprecatedMethodListener;
use WP_Mock\Tests\WP_MockTestCase;

/**
* @covers \WP_Mock
Expand Down Expand Up @@ -222,6 +223,27 @@ public function testAssertFiltersCalledFails(): void
Mockery::close();
}

/**
* @covers \WP_Mock::assertFiltersCalled()
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @return void
*/
public function testAssertFiltersPassesWithTypes(): void
{
WP_Mock::bootstrap();

WP_Mock::expectFilter('testFilter', WP_Mock\Functions::type(TestClass::class));

apply_filters('testFilter', new TestClass());

WP_Mock::assertFiltersCalled();

Mockery::close();
}

/**
* @covers \WP_Mock::alias()
*
Expand Down
Loading