Skip to content

Commit

Permalink
WIP, still trying to find a good way of running tests locally and on …
Browse files Browse the repository at this point in the history
…actions

on a satisfactory way
  • Loading branch information
freibergergarcia committed Feb 10, 2024
1 parent 911fc50 commit ac4c3fb
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 117 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/run-phpcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
pull_request:
branches: [ "develop" ]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

permissions:
contents: read

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
},
"require-dev": {
"phpunit/phpunit": "^9.6.13",
"10up/wp_mock": "^1.0.0",
"wp-coding-standards/wpcs": "^3.0",
"wp-phpunit/wp-phpunit": "^6.4",
"automattic/vipwpcs": "^3.0"
"automattic/vipwpcs": "^3.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"phpcompatibility/phpcompatibility-wp": "*",
"yoast/phpunit-polyfills": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "freibergergarcia/custom-post-types-taxonomies",
"name": "custom-post-types-taxonomies",
"description": "Custom Post Types and Taxonomies",
"license": "GPL-2.0-only",
"author": {
Expand All @@ -21,7 +21,7 @@
"build": "NODE_ENV=production npx postcss assets/css/general.css -o assets/css/public/general.css",
"start-env": "wp-env start",
"stop-env": "wp-env stop",
"test-php": "npm run start-env && wp-env run tests-cli --env-cwd=wp-content/plugins/$(basename $(pwd)) vendor/bin/phpunit -c phpunit.xml.dist --verbose && npm run stop-env",
"test-php": "wp-env run tests-cli --env-cwd=wp-content/plugins/$(basename $(pwd)) vendor/bin/phpunit -c phpunit.xml.dist --verbose",
"wp-env": "wp-env"
}
}
2 changes: 1 addition & 1 deletion tests/unit/Admin/Admin_Menu_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Custom_PTT\Tests\Unit\Admin;
namespace Custom_PTT\Tests\Unit\Unit\Admin;

use PHPUnit\Framework\TestCase;
use Custom_PTT\Admin\Admin_Menu;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Plugin_Factory_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Custom_PTT\Tests\Unit;
namespace Custom_PTT\Tests\Unit\Unit;

use PHPUnit\Framework\TestCase;
use Custom_PTT\Plugin_Factory;
Expand Down
117 changes: 11 additions & 106 deletions tests/unit/Plugin_Test.php
Original file line number Diff line number Diff line change
@@ -1,121 +1,26 @@
<?php

declare(strict_types=1);
declare( strict_types=1 );

namespace Custom_PTT\Tests\Unit;

use PHPUnit\Framework\TestCase;
use Custom_PTT\Plugin;
use DI\Container;
use Custom_PTT\Infrastructure\Registerable;
use WP_Mock;
use WP_UnitTestCase;

/**
* Class Plugin_Test
*
* @package Custom_PTT\Tests\Unit
* @since 0.1.0-alpha
*/
class Plugin_Test extends TestCase {
class Plugin_Test extends WP_UnitTestCase {

/**
* Set up before the test class.
*
* @since 0.1.0-alpha
*/
public static function setUpBeforeClass(): void {
if ( ! defined( 'CUSTOM_PTT_FILE' ) ) {
define( 'CUSTOM_PTT_FILE', 'custom-post-types-taxonomies/custom-post-types-taxonomies.php' );
}
}

/**
* Set up the test.
*
* @since 0.1.0-alpha
*/
protected function setUp(): void {
WP_Mock::setUp();
}

/**
* Tear down the test.
*
* @since 0.1.0-alpha
*/
protected function tearDown(): void {
WP_Mock::tearDown();
}

/**
* Test register_hooks method.
*
* @since 0.1.0-alpha
*/
public function test_register_hooks() {
WP_Mock::userFunction( 'register_activation_hook' );
WP_Mock::userFunction( 'register_deactivation_hook' );

$container = $this->createMock( Container::class );
$plugin = new Plugin( $container );

$this->assertNull( $plugin->register_hooks() );
}

/**
* Test on_deactivation method.
*
* @since 0.1.0-alpha
*/
public function test_on_deactivation() {
WP_Mock::userFunction( 'wp_cache_flush' );
WP_Mock::userFunction( 'flush_rewrite_rules' );
private Plugin $plugin;
private Container $container;

$container = $this->createMock( Container::class );
$plugin = new Plugin( $container );
public function setUp(): void {
parent::setUp();

$this->assertNull( $plugin->on_deactivation() );
$this->container = $this->createMock( Container::class );
$this->plugin = new Plugin( $this->container );
}

/**
* Test on_activation method.
*
* @since 0.1.0-alpha
*/
public function test_on_activation() {
$container = $this->createMock( Container::class );
$plugin = new Plugin( $container );

$this->assertNull( $plugin->on_activation() );
}

/**
* Test on_uninstall method.
*
* @since 0.1.0-alpha
*/
public function test_on_uninstall() {
$this->assertNull( Plugin::on_uninstall() );
}

/**
* Test register_services method.
*
* @since 0.1.0-alpha
*/
public function test_register_services() {
$container = $this->createMock( Container::class );
$container->method( 'getKnownEntryNames' )->willReturn( array( 'service1', 'service2' ) );

$service1 = $this->createMock( Registerable::class );
$service1->expects( $this->once() )->method( 'register' );

$service2 = $this->createMock( Registerable::class );
$service2->expects( $this->once() )->method( 'register' );

$container->method( 'get' )->will( $this->onConsecutiveCalls( $service1, $service2 ) );

$plugin = new Plugin( $container );
$plugin->register_services();
public function tearDown(): void {
parent::tearDown();
}
}
2 changes: 1 addition & 1 deletion tests/unit/Taxonomy/Taxonomy_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare( strict_types=1 );

namespace Custom_PTT\Tests\Unit\Taxonomy;
namespace Custom_PTT\Tests\Unit\Unit\Taxonomy;

use PHPUnit\Framework\TestCase;
use Custom_PTT\Taxonomy\Taxonomy;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Utilities_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @since 0.1.0-alpha
*/

namespace Custom_PTT\Tests\Unit;
namespace Custom_PTT\Tests\Unit\Unit;

use PHPUnit\Framework\TestCase;
use Custom_PTT\Utilities;
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@

// First we need to load the composer autoloader, so we can use WP Mock
require_once dirname( __DIR__ ) . '../../vendor/autoload.php';

// Bootstrap WP_Mock to initialize built-in features
WP_Mock::bootstrap();

0 comments on commit ac4c3fb

Please sign in to comment.