Skip to content

Commit

Permalink
Refactor Unit tests namespace (#190)
Browse files Browse the repository at this point in the history
Co-authored-by: Fulvio Notarstefano <[email protected]>
  • Loading branch information
nmolham-godaddy and unfulvio-godaddy authored Dec 26, 2022
1 parent 8b1abb6 commit cdac012
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 338 deletions.
19 changes: 12 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A mocking library to take the pain out of unit testing for WordPress",
"license": "GPL-2.0-or-later",
"version": "0.5.0",
"prefer-stable" : true,
"prefer-stable": true,
"require": {
"php": ">=7.3 < 9.0",
"phpunit/phpunit": "^9.5.24",
Expand All @@ -30,17 +30,22 @@
"php/WP_Mock.php"
]
},
"autoload-dev": {
"psr-4": {
"WP_Mock\\Tests\\": "tests/"
},
"classmap": [
"tests"
]
},
"config": {
"platform": {
"php" : "7.3"
"php": "7.3"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"autoload-dev" : {
"classmap": ["tests"]
},
"scripts": {
"test:behat": "behat",
"test:phpunit": "phpunit",
Expand All @@ -54,6 +59,6 @@
"@test:phpunitcov"
],
"post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility",
"post-update-cmd" : "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility"
"post-update-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility"
}
}
}
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

387 changes: 136 additions & 251 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
verbose="true">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/</directory>
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Integration">
<directory suffix="Test.php">./tests/Integration</directory>
</testsuite>
</testsuites>
<coverage cacheDirectory=".phpunit.cache/code-coverage"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

class FunctionMocksTest extends \PHPUnit\Framework\TestCase
namespace WP_Mock\Tests\Integration;

use WP_Mock;
use WP_Mock\Tests\WP_MockTestCase;

class FunctionMocksTest extends WP_MockTestCase
{
private $common_functions = array(
private $common_functions = [
'esc_attr',
'esc_html',
'esc_js',
Expand All @@ -19,9 +24,9 @@ class FunctionMocksTest extends \PHPUnit\Framework\TestCase
'esc_html_e',
'esc_html_x',
'_n',
);
];

protected function setUp(): void
protected function setUp() : void
{
if (! $this->isInIsolation()) {
WP_Mock::setUp();
Expand All @@ -46,12 +51,12 @@ public function testCommonFunctionsAreDefined()
}

/**
* @covers \WP_Mock::userFunction()
* @covers \WP_Mock::userFunction()
* @dataProvider dataCommonFunctionsDefaultFunctionality
*/
public function testCommonFunctionsDefaultFunctionality($function, $action)
{
$input = $expected = 'Something Random ' . rand(0, 99);
$input = $expected = 'Something Random '.rand(0, 99);
if ('echo' === $action) {
$this->expectOutputString($input);
$expected = null;
Expand Down Expand Up @@ -83,7 +88,7 @@ public function testDefaultFailsInStrictMode()
public function dataCommonFunctionsDefaultFunctionality()
{
return array_map(function ($function) {
return array( $function, '_e' === substr($function, - 2) ? 'echo' : 'return' );
return [$function, '_e' === substr($function, -2) ? 'echo' : 'return'];
}, $this->common_functions);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Mocks/SampleClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace WP_Mock\Tests\Mocks;

class SampleClass
{
public function action() : void
{
}
}
10 changes: 10 additions & 0 deletions tests/Mocks/SampleSubClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace WP_Mock\Tests\Mocks;

class SampleSubClass extends SampleClass
{
public function action() : void
{
}
}
7 changes: 7 additions & 0 deletions tests/Mocks/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace WP_Mock\Tests\Mocks;

function testCallback() : void
{
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

namespace WP_Mock;
namespace WP_Mock\Tests\Unit\WP_Mock;

use Exception;
use Mockery;
use PHPUnit\Framework\RiskyTest;
use PHPUnit\Framework\RiskyTestError;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use WP_Mock\DeprecatedListener;
use WP_Mock\Tests\WP_MockTestCase;

/**
* @covers \WP_Mock\DeprecatedListener
*/
class DeprecatedListenerTest extends \PHPUnit\Framework\TestCase
class DeprecatedListenerTest extends WP_MockTestCase
{
/** @var DeprecatedListener */
protected $object;
Expand Down Expand Up @@ -80,7 +83,7 @@ public function testCheckCalls_scalar_only()
$this->object->logDeprecatedCall('FooBar::bazBat', array( 'string', true, 42 ));
$this->object->setTestName('TestName');
$testCase = Mockery::mock('\PHPUnit\Framework\TestCase');
/** @var \PHPUnit\Framework\TestCase $testCase */
/** @var TestCase $testCase */
$this->object->setTestCase($testCase);
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock($testResult);
Expand Down Expand Up @@ -122,7 +125,7 @@ public function testCheckCalls_non_scalars()
$this->object->logDeprecatedCall('BazBat::fooBar', array( range(1, $range), $resource ));
$this->object->setTestName('OtherTest');
$testCase = Mockery::mock('\PHPUnit\Framework\TestCase');
/** @var \PHPUnit\Framework\TestCase $testCase */
/** @var TestCase $testCase */
$this->object->setTestCase($testCase);
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock($testResult);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
<?php

namespace WP_Mock\Tests\Unit\WP_Mock;

use Mockery;
use WP_Mock;
use WP_Mock\Tests\WP_MockTestCase;

/**
* @covers \WP_Mock\DeprecatedListener
*/
class DeprecatedMethodsTest extends \PHPUnit\Framework\TestCase
class DeprecatedMethodsTest extends WP_MockTestCase
{
public function setUp(): void
public function setUp() : void
{
WP_Mock::setUp();
WP_Mock::getDeprecatedListener()->reset();
}

protected function tearDown(): void
protected function tearDown() : void
{
WP_Mock::getDeprecatedListener()->reset();
WP_Mock::tearDown();
Expand All @@ -24,8 +30,8 @@ public function testWpFunctionLogsDeprecationNotice()
{
$listener = WP_Mock::getDeprecatedListener();
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock($testResult);
$case = Mockery::mock('\PHPUnit\Framework\TestCase');
$result = Mockery::mock($testResult);
$case = Mockery::mock('\PHPUnit\Framework\TestCase');
$listener->setTestCase($case);
$listener->setTestResult($result);
$result->shouldReceive('addFailure')
Expand All @@ -45,7 +51,7 @@ public function testWpPassthruFunctionLogsDeprecationNotice()
$listener = WP_Mock::getDeprecatedListener();
$testResult = new \PHPUnit\Framework\TestResult();
$result = Mockery::mock($testResult);
$case = Mockery::mock('\PHPUnit\Framework\TestCase');
$case = Mockery::mock('\PHPUnit\Framework\TestCase');
$listener->setTestCase($case);
$listener->setTestResult($result);
$result->shouldReceive('addFailure')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

namespace WP_Mock\Matcher;
namespace WP_Mock\Tests\Unit\WP_Mock\Matcher;

class AnyInstanceTest extends \PHPUnit\Framework\TestCase
use WP_Mock\Matcher\AnyInstance;
use WP_Mock\Tests\Mocks\SampleClass;
use WP_Mock\Tests\Mocks\SampleSubClass;
use WP_Mock\Tests\WP_MockTestCase;

class AnyInstanceTest extends WP_MockTestCase
{
/**
* @covers \WP_Mock\Matcher\AnyInstance::match
Expand Down Expand Up @@ -98,7 +103,7 @@ public function testToString()

$result = "$sut";

$this->assertEquals("<AnyInstance[WP_Mock\Matcher\SampleClass]>", $result);
$this->assertEquals("<AnyInstance[WP_Mock\Tests\Mocks\SampleClass]>", $result);
}

/**
Expand Down
Loading

0 comments on commit cdac012

Please sign in to comment.