Skip to content

Commit

Permalink
Merge 0.4.1. Fixes #133
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Mann authored and ericmann committed Feb 26, 2019
2 parents 9f3525a + 99804c3 commit d3e90d6
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ composer.lock
vendor/
docs/
.editorconfig
.phpunit.result.cache
build/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ WP_Mock is an API mocking framework, built and maintained by [10up](http://10up.
First, add WP Mock as a dev-dependency with [Composer](http://getcomposer.org):

```bash
composer require --dev 10up/wp_mock:0.3.0
composer require --dev 10up/wp_mock:0.4.1
```

Then, make sure your bootstrap file is loading the composer autoloader:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name" : "10up/wp_mock",
"description": "A mocking library to take the pain out of unit testing for WordPress",
"license" : "GPL-2.0-or-later",
"version" : "0.4.0",
"version" : "0.4.1",
"require" : {
"php" : ">=7.1",
"phpunit/phpunit" : ">=7.0",
Expand Down
4 changes: 2 additions & 2 deletions php/WP_Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static function bootstrap() {
/**
* Make sure Mockery doesn't have anything set up already.
*/
public static function setUp() {
public static function setUp() : void {
if ( self::$__bootstrapped ) {
\Mockery::close();

Expand All @@ -128,7 +128,7 @@ public static function setUp() {
/**
* Tear down anything built up inside Mockery when we're ready to do so.
*/
public static function tearDown() {
public static function tearDown() : void {
self::$event_manager->flush();
self::$function_manager->flush();

Expand Down
42 changes: 39 additions & 3 deletions php/WP_Mock/Tools/Constraints/IsEqualHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,43 @@

use PHPUnit\Framework\Constraint\IsEqual;

class IsEqualHtml extends \PHPUnit\Framework\Constraint\IsEqual {
class IsEqualHtml {
protected $IsEqual;
protected $value;

/**
* @var float
*/
private $delta;

/**
* @var int
*/
private $maxDepth;

/**
* @var bool
*/
private $canonicalize;

/**
* @var bool
*/
private $ignoreCase;

public function __construct(
$value,
float $delta = 0.0,
int $maxDepth = 10,
bool $canonicalize = false,
bool $ignoreCase = false
) {
$this->value = $value;
$this->delta = $delta;
$this->maxDepth = $maxDepth;
$this->canonicalize = $canonicalize;
$this->ignoreCase = $ignoreCase;
}

private function clean( $thing ) {
$thing = preg_replace( '/\n\s+/', '', $thing );
Expand All @@ -15,7 +51,7 @@ private function clean( $thing ) {
public function evaluate( $other, $description = '', $returnResult = FALSE ) {
$other = $this->clean( $other );
$this->value = $this->clean( $this->value );
return parent::evaluate( $other, $description, $returnResult );
$isEqual = new IsEqual( $this->value, $this->delta, $this->maxDepth, $this->canonicalize, $this->ignoreCase );
return $isEqual->evaluate( $other, $description, $returnResult );
}

}
4 changes: 2 additions & 2 deletions php/WP_Mock/Tools/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
*/
protected $testFiles = array();

public function setUp() {
public function setUp() : void {
$this->requireFileDependencies();

WP_Mock::setUp();
Expand All @@ -54,7 +54,7 @@ public function setUp() {
$this->cleanGlobals();
}

public function tearDown() {
public function tearDown() : void {
WP_Mock::tearDown();

$this->cleanGlobals();
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
colors="true"
verbose="true">
<testsuites>
<testsuite>
<testsuite name="All Tests">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
Expand Down
10 changes: 6 additions & 4 deletions tests/DeprecatedMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

class DeprecatedMethodsTest extends \PHPUnit\Framework\TestCase {

public function setUp() {
public function setUp() : void {
WP_Mock::setUp();
WP_Mock::getDeprecatedListener()->reset();
}

protected function tearDown() {
protected function tearDown() : void {
WP_Mock::getDeprecatedListener()->reset();
WP_Mock::tearDown();
}

public function testWpFunctionLogsDeprecationNotice() {
$listener = WP_Mock::getDeprecatedListener();
$result = Mockery::mock( '\PHPUnit\Framework\TestResult' );
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock( $testResult );
$case = Mockery::mock( '\PHPUnit\Framework\TestCase' );
$listener->setTestCase( $case );
$listener->setTestResult( $result );
Expand All @@ -27,7 +28,8 @@ public function testWpFunctionLogsDeprecationNotice() {

public function testWpPassthruFunctionLogsDeprecationNotice() {
$listener = WP_Mock::getDeprecatedListener();
$result = Mockery::mock( '\PHPUnit\Framework\TestResult' );
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock( $testResult );
$case = Mockery::mock( '\PHPUnit\Framework\TestCase' );
$listener->setTestCase( $case );
$listener->setTestResult( $result );
Expand Down
6 changes: 3 additions & 3 deletions tests/FunctionMocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FunctionMocksTest extends \PHPUnit\Framework\TestCase {
'_n',
);

protected function setUp() {
protected function setUp() : void {
if ( ! $this->isInIsolation() ) {
WP_Mock::setUp();
}
Expand Down Expand Up @@ -56,10 +56,10 @@ public function testCommonFunctionsDefaultFunctionality( $function, $action ) {
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
* @expectedException \PHPUnit\Framework\ExpectationFailedException
* @expectedExceptionMessageRegExp /No handler found for \w+/
*/
public function testDefaultFailsInStrictMode() {
$this->expectExceptionMessageRegExp('/No handler found for \w+/');
$this->expectException('\PHPUnit\Framework\ExpectationFailedException');
WP_Mock::activateStrictMode();
WP_Mock::bootstrap();
_e('Test');
Expand Down
13 changes: 8 additions & 5 deletions tests/WP_Mock/DeprecatedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class DeprecatedListenerTest extends \PHPUnit\Framework\TestCase {
/** @var DeprecatedListener */
protected $object;

protected function setUp() {
protected function setUp() : void {
$this->object = new DeprecatedListener();
}

public function tearDown() {
public function tearDown() : void {
$this->object->reset();
}

Expand All @@ -37,7 +37,8 @@ public function testReset() {
}

public function testCheckCallsNoCalls() {
$result = Mockery::mock( '\PHPUnit\Framework\TestResult' );
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock( $testResult );
$result->shouldReceive( 'addFailure' )->never();
/** @var \\PHPUnit\Framework\TestResult $result */
$this->object->setTestResult( $result );
Expand All @@ -51,7 +52,8 @@ public function testCheckCalls_scalar_only() {
$testCase = Mockery::mock( '\PHPUnit\Framework\TestCase' );
/** @var \PHPUnit\Framework\TestCase $testCase */
$this->object->setTestCase( $testCase );
$result = Mockery::mock( '\PHPUnit\Framework\TestResult' );
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock( $testResult );
$result->shouldReceive( 'addFailure' )
->once()
->andReturnUsing( function ( $case, $exception, $int ) use ( $testCase ) {
Expand Down Expand Up @@ -85,7 +87,8 @@ public function testCheckCalls_non_scalars() {
$testCase = Mockery::mock( '\PHPUnit\Framework\TestCase' );
/** @var \PHPUnit\Framework\TestCase $testCase */
$this->object->setTestCase( $testCase );
$result = Mockery::mock( '\PHPUnit\Framework\TestResult' );
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock( $testResult );;
$testClosure = function ( $case, $exception, $int ) use ( $testCase, $callback1, $object1, $range ) {
$int = (int) $int; // It's coming as 0.0
$callback1 = get_class( $callback1 ) . ':' . spl_object_hash( $callback1 );
Expand Down

0 comments on commit d3e90d6

Please sign in to comment.