Skip to content

Commit

Permalink
Merge pull request #148 from Yoast/JRF/tests-use-psr4
Browse files Browse the repository at this point in the history
Tests: use PSR-4
  • Loading branch information
jrfnl authored Nov 20, 2023
2 parents ef3fa0e + 7e8be8e commit 0529345
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 100 deletions.
27 changes: 18 additions & 9 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@
<element value="Yoast\WHIP"/>
<element value="whip"/>
</property>

<!-- Set the custom test class whitelist for all sniffs which use it in one go.
Ref: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#custom-unit-test-classes
-->
<property name="custom_test_class_whitelist" type="array">
<element value="Whip_TestCase"/>
</property>
</properties>

<!-- Historically, this library has used camelCaps not snakecase for variable and function names. -->
Expand Down Expand Up @@ -80,7 +73,7 @@
<exclude-pattern>/src/facades/wordpress\.php$</exclude-pattern>

<!-- Exclude mocks of WP Core functions which comply with the WP function name rules instead. -->
<exclude-pattern>/tests/doubles/WpCoreFunctionsMock\.php$</exclude-pattern>
<exclude-pattern>/tests/Unit/Doubles/WpCoreFunctionsMock\.php$</exclude-pattern>
</rule>


Expand All @@ -99,6 +92,14 @@
</properties>
</rule>

<rule ref="Yoast.Files.TestDoubles">
<properties>
<property name="doubles_path" type="array">
<element value="/tests/Unit/Doubles"/>
</property>
</properties>
</rule>


<!--
#############################################################################
Expand All @@ -109,7 +110,7 @@

<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<!-- Valid usage: For testing purposes, allow non-prefixed globals. -->
<exclude-pattern>/tests/doubles/WPCoreFunctionsMock\.php$</exclude-pattern>
<exclude-pattern>/tests/Unit/Doubles/WPCoreFunctionsMock\.php$</exclude-pattern>
</rule>

<rule ref="Yoast.NamingConventions.ValidHookName.WrongPrefix">
Expand Down Expand Up @@ -153,4 +154,12 @@
<exclude-pattern>/src/Whip_Host\.php$</exclude-pattern>
</rule>

<!-- The below two issues will likely be fixed in YoastCS 3.0, after which these excludes can be removed. -->
<rule ref="Yoast.Commenting.FileComment.Unnecessary">
<exclude-pattern>/tests/Unit/bootstrap\.php$</exclude-pattern>
</rule>
<rule ref="Yoast.NamingConventions.ObjectNameDepth.MaxExceeded">
<exclude-pattern>/tests/Unit/WPMessageDismissListenerTest\.php$</exclude-pattern>
</rule>

</ruleset>
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
]
},
"autoload-dev": {
"classmap": [
"tests/"
]
"psr-4": {
"Yoast\\WHIP\\Tests\\": "tests/"
}
},
"config": {
"allow-plugins": {
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
backupGlobals="true"
bootstrap="tests/bootstrap.php"
bootstrap="tests/Unit/bootstrap.php"
convertErrorsToExceptions="true"
convertWarningsToExceptions="true"
convertNoticesToExceptions="true"
Expand All @@ -12,7 +12,7 @@
>
<testsuites>
<testsuite name="whip">
<directory suffix="Test.php">./tests/</directory>
<directory suffix="Test.php">./tests/Unit/</directory>
</testsuite>
</testsuites>

Expand Down
11 changes: 5 additions & 6 deletions tests/MessageTest.php → tests/Unit/BasicMessageTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit;

use Whip_BasicMessage;

/**
* Message Unit tests.
*/
final class MessageTest extends Whip_TestCase {
final class BasicMessageTest extends TestCase {

/**
* Tests if Whip_BasicMessage correctly handles a string for its body argument.
Expand Down
11 changes: 5 additions & 6 deletions tests/ConfigurationTest.php → tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit;

use Whip_Configuration;

/**
* Configuration unit tests.
*/
final class ConfigurationTest extends Whip_TestCase {
final class ConfigurationTest extends TestCase {

/**
* Tests the creation of a Whip_Configuration with invalid input.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit\Doubles;

use Whip_DismissStorage;

/**
* Test helper.
*/
final class Whip_DismissStorageMock implements Whip_DismissStorage {
final class DismissStorageMock implements Whip_DismissStorage {

/**
* Holds the dismissed state.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit;

use Whip_MessageDismisser;
use Yoast\WHIP\Tests\Unit\Doubles\DismissStorageMock;

/**
* Message Dismisser unit tests.
*/
final class MessageDismisserTest extends Whip_TestCase {
final class MessageDismisserTest extends TestCase {

/**
* Tests if Whip_MessageDismisser correctly updates Whip_DismissStorage.
Expand All @@ -19,9 +19,9 @@ final class MessageDismisserTest extends Whip_TestCase {
* @return void
*/
public function testDismiss() {
$currentTime = time();
$storage = new Whip_DismissStorageMock();
$dismisser = new Whip_MessageDismisser( $currentTime, ( WEEK_IN_SECONDS * 4 ), $storage );
$currentTime = \time();
$storage = new DismissStorageMock();
$dismisser = new Whip_MessageDismisser( $currentTime, ( \WEEK_IN_SECONDS * 4 ), $storage );

$this->assertSame( 0, $storage->get() );

Expand All @@ -45,9 +45,9 @@ public function testDismiss() {
* @return void
*/
public function testIsDismissibleWithVersions( $savedTime, $currentTime, $expected ) {
$storage = new Whip_DismissStorageMock();
$storage = new DismissStorageMock();
$storage->set( $savedTime );
$dismisser = new Whip_MessageDismisser( $currentTime, ( WEEK_IN_SECONDS * 4 ), $storage );
$dismisser = new Whip_MessageDismisser( $currentTime, ( \WEEK_IN_SECONDS * 4 ), $storage );

$this->assertSame( $expected, $dismisser->isDismissed() );
}
Expand All @@ -59,10 +59,10 @@ public function testIsDismissibleWithVersions( $savedTime, $currentTime, $expect
*/
public static function versionNumbersProvider() {
return array(
'-2weeks' => array( strtotime( '-2weeks' ), time(), true ),
'-4weeks' => array( strtotime( '-4weeks' ), time(), true ),
'-6weeks' => array( strtotime( '-6weeks' ), time(), false ),
'time()' => array( time(), time(), true ),
'-2weeks' => array( \strtotime( '-2weeks' ), \time(), true ),
'-4weeks' => array( \strtotime( '-4weeks' ), \time(), true ),
'-6weeks' => array( \strtotime( '-6weeks' ), \time(), false ),
'time()' => array( \time(), \time(), true ),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit;

use Whip_MessagesManager;

/**
* Message Manager unit tests.
*/
final class MessagesManagerTest extends Whip_TestCase {
final class MessagesManagerTest extends TestCase {

/**
* Creates a MessagesManager, calls hasMessages and tests if it returns false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit;

use Error;
use Exception;
use stdClass;
use Whip_RequirementsChecker;
use Whip_VersionRequirement;

/**
* Requirements checker unit tests.
*/
final class RequirementsCheckerTest extends Whip_TestCase {
final class RequirementsCheckerTest extends TestCase {

/**
* Tests if Whip_RequirementsChecker is successfully created when given valid arguments.
Expand All @@ -35,7 +38,7 @@ public function testItReceivesAUsableRequirementObject() {
* @return void
*/
public function testItThrowsAnTypeErrorWhenInvalidRequirementIsPassed() {
if ( version_compare( phpversion(), '7.0', '<' ) ) {
if ( \version_compare( \phpversion(), '7.0', '<' ) ) {
$this->markTestSkipped( 'Skipped due to incompatible PHP version: this test only runs on PHP 7+.' );
}

Expand All @@ -60,7 +63,7 @@ public function testItThrowsAnTypeErrorWhenInvalidRequirementIsPassed() {
* @return void
*/
public function testItThrowsAnTypeErrorWhenInvalidRequirementIsPassedInPHP5() {
if ( version_compare( phpversion(), '7.0', '>=' ) ) {
if ( \version_compare( \phpversion(), '7.0', '>=' ) ) {
$this->markTestSkipped( 'Skipped due to incompatible PHP version: this test only runs on PHP 5.x.' );
}

Expand Down Expand Up @@ -143,7 +146,7 @@ public function testCheckIfPHPRequirementIsNotFulfilled() {

$this->assertNotEmpty( $recentMessage );

if ( method_exists( $this, 'assertIsString' ) ) {
if ( \method_exists( $this, 'assertIsString' ) ) {
// PHPUnit 8+.
$this->assertIsString( $recentMessage->body() );
}
Expand All @@ -165,7 +168,7 @@ public function testCheckIfPHPRequirementIsNotFulfilled() {
* @return void
*/
public function testCheckIfRequirementIsFulfilled() {
$checker = new Whip_RequirementsChecker( array( 'php' => phpversion() ) );
$checker = new Whip_RequirementsChecker( array( 'php' => \phpversion() ) );

$checker->addRequirement( new Whip_VersionRequirement( 'php', '5.2' ) );
$checker->check();
Expand Down
13 changes: 5 additions & 8 deletions tests/TestCase.php → tests/Unit/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

use PHPUnit\Framework\TestCase;
namespace Yoast\WHIP\Tests\Unit;

use PHPUnit\Framework\TestCase as PHPUnit_TestCase;

/**
* Base test case class from which all tests should extend.
*/
abstract class Whip_TestCase extends TestCase {
abstract class TestCase extends PHPUnit_TestCase {

/**
* Helper method to test exceptions.
Expand All @@ -32,7 +29,7 @@ abstract class Whip_TestCase extends TestCase {
* @return void
*/
public function expectExceptionHelper( $exception, $message = '', $code = null ) {
if ( method_exists( $this, 'expectException' ) ) {
if ( \method_exists( $this, 'expectException' ) ) {
// PHPUnit 5+.
$this->expectException( $exception );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit;

use Whip_VersionRequirement;

/**
* Version requirements unit tests.
*/
final class VersionRequirementTest extends Whip_TestCase {
final class VersionRequirementTest extends TestCase {

/**
* Creates a new Whip_VersionRequirement with component php and version 5.2 and
Expand Down Expand Up @@ -202,4 +201,3 @@ public function testFromCompareStringException() {
Whip_VersionRequirement::fromCompareString( 'php', '> 2.3' );
}
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit;

use Whip_WPMessageDismissListener;

/**
* Message Dismiss Listener unit tests.
*
* @coversDefaultClass Whip_WPMessageDismissListener
*/
final class WPMessageDismissListener extends Whip_TestCase {
final class WPMessageDismissListenerTest extends TestCase {

/**
* Tests the listen method.
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* WHIP libary test file.
*
* @package Yoast\WHIP
*/

namespace Yoast\WHIP\Tests\Unit;

if ( \file_exists( __DIR__ . '/../../vendor/autoload.php' ) ) {
require_once __DIR__ . '/../../vendor/autoload.php';
}
else {
echo 'ERROR: Run `composer install` to generate the autoload files before running the unit tests.' . \PHP_EOL;
exit( 1 );
}

require_once __DIR__ . '/Doubles/WPCoreFunctionsMock.php';
Loading

0 comments on commit 0529345

Please sign in to comment.