Migrate tests/unit folder to WordPress Coding Standard

[MAILPOET-6240]
This commit is contained in:
Jan Lysý
2024-11-04 12:21:28 +01:00
committed by Jan Lysý
parent d0f8d6f83e
commit d0c8cde59c
4 changed files with 87 additions and 20 deletions

View File

@@ -11,6 +11,11 @@
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<!-- Exclude bootstrap from the SeparateFunctionsFromOO.Mixed rule -->
<rule ref="Universal.Files.SeparateFunctionsFromOO.Mixed">
<exclude-pattern>tests/unit/_bootstrap.php</exclude-pattern>
</rule>
<!-- Skip the vendor directory -->
<exclude-pattern>vendor/*</exclude-pattern>
</ruleset>

View File

@@ -1,12 +1,24 @@
<?php declare(strict_types = 1);
<?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor;
use Exception;
use PHPUnit\Framework\TestCase;
use stdClass;
/**
* Unit test for Container class.
*/
class Container_Test extends TestCase {
/**
* Test if sets and gets service.
*/
public function testSetAndGetService(): void {
$container = new Container();
@@ -22,6 +34,9 @@ class Container_Test extends TestCase {
$this->assertInstanceOf( stdClass::class, $service );
}
/**
* Test if sets and gets service with dependencies.
*/
public function testGetReturnsSameInstance(): void {
$container = new Container();
@@ -32,19 +47,22 @@ class Container_Test extends TestCase {
}
);
// Retrieve the service twice
// Retrieve the service twice.
$service1 = $container->get( 'singleton_service' );
$service2 = $container->get( 'singleton_service' );
// Check that both instances are the same
// Check that both instances are the same.
$this->assertSame( $service1, $service2 );
}
/**
* Test if it throws exception for non-existing service.
*/
public function testExceptionForNonExistingService(): void {
// Create the container instance
// Create the container instance.
$container = new Container();
// Attempt to get a non-existing service should throw an exception
// Attempt to get a non-existing service should throw an exception.
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Service not found: non_existing_service' );

View File

@@ -1,16 +1,41 @@
<?php declare(strict_types = 1);
<?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
$console = new \Codeception\Lib\Console\Output( array() );
if ( ! function_exists( 'esc_attr' ) ) {
/**
* Mock esc_attr function.
*
* @param string $attr Attribute to escape.
*/
function esc_attr( $attr ) {
return $attr;
}
}
/**
* Base class for unit tests.
*/
abstract class MailPoetUnitTest extends \Codeception\TestCase\Test {
protected $runTestInSeparateProcess = false;
protected $preserveGlobalState = false;
/**
* Disable running tests in separate processes.
*
* @var bool
*/
protected $runTestInSeparateProcess = false; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
/**
* Disable preserving global state.
*
* @var bool
*/
protected $preserveGlobalState = false; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
}
require '_stubs.php';

View File

@@ -1,14 +1,33 @@
<?php declare(strict_types = 1);
// phpcs:ignoreFile - We want to allow multiple classes etc.
<?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
// Dummy WP classes
if (!class_exists(\WP_Theme_JSON::class)) {
class WP_Theme_JSON {
public function get_data() {
return [];
}
public function get_settings() {
return [];
}
}
declare(strict_types = 1);
// Dummy WP classes.
if ( ! class_exists( \WP_Theme_JSON::class ) ) {
/**
* Class WP_Theme_JSON
*/
class WP_Theme_JSON {
/**
* Get data.
*
* @return array
*/
public function get_data() {
return array();
}
/**
* Get settings.
*
* @return array
*/
public function get_settings() {
return array();
}
}
}