Skip to content

Commit

Permalink
Merge pull request #47 from Strategy11/fix-unit-tests
Browse files Browse the repository at this point in the history
fix test cases
  • Loading branch information
mostafa-hisham authored Jul 13, 2023
2 parents 45f49b1 + 2505adb commit 83a3fe5
Show file tree
Hide file tree
Showing 37 changed files with 250 additions and 248 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
build-test:
if: contains(github.event.pull_request.labels.*.name, 'ready to merge')
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
strategy:
matrix:
include:
Expand All @@ -26,7 +26,6 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: antecedent/patchwork, phpunit-polyfills, phake/phake, brain/monkey
- name: Installing WordPress
run: |
export WP_DEVELOP_DIR=/tmp/wordpress/
Expand All @@ -47,5 +46,6 @@ jobs:
run: |
cd "/tmp/wordpress/src/wp-content/plugins/${GITHUB_REPOSITORY#*/}"
pwd
composer install
composer dump-autoload --dev
phpunit
vendor/phpunit/phpunit/phpunit --configuration phpunit.xml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ bin/vvv-nginx.conf
!/vendor/autoload.php
!/vendor/composer
*.css.map
.phpunit.result.cache
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"squizlabs/php_codesniffer": "^3.7.0",
"wp-coding-standards/wpcs": "*",
"php-stubs/wordpress-stubs": "^6.0",
"phpstan/phpstan": "^1.8"
"phpstan/phpstan": "^1.8",
"dms/phpunit-arraysubset-asserts": "^0.4.0"
},
"config": {
"allow-plugins": {
Expand Down
49 changes: 47 additions & 2 deletions composer.lock

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

8 changes: 1 addition & 7 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
define( 'AWPCP_URL', 'https://example.org/wp-content/plugins/another-wordpress-classifieds-plugin' );

$patchwork = AWPCP_DIR . '/vendor/antecedent/patchwork/Patchwork.php';

if ( file_exists( $patchwork ) ) {
require_once $patchwork;
}

require AWPCP_DIR . '/vendor/autoload.php';

Phake::setClient( Phake::CLIENT_PHPUNIT6 );
Expand All @@ -31,12 +31,6 @@
require_once AWPCP_DIR . '/includes/functions/listings.php';
require_once AWPCP_DIR . '/includes/functions/routes.php';

if ( false !== getenv( 'WP_DEVELOP_DIR' ) ) {
require getenv( 'WP_DEVELOP_DIR' ) . 'tests/phpunit/includes/bootstrap.php';
} else {
require '../../../../tests/phpunit/includes/bootstrap.php';
}

/**
* TODO: We probably won't need this if we stop using WordPress testing framework.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/**
* Test helper for Listing Table View.
*/
use Brain\Monkey\Functions;
class AWPCP_ListingTableViewTestHelper {

/**
Expand Down
122 changes: 54 additions & 68 deletions tests/includes/testcase-awpcp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,12 @@
* Base class for all plugin tests.
*/
abstract class AWPCP_UnitTestCase extends PHPUnit\Framework\TestCase {

protected static $mockCommonWpFunctionsInSetUp = false;
/**
* @var array [Patchwork\CallRouting\Handle]
*/
private $redefined_functions = [];

// /**
// * @var string Placeholder for the save the SQL mode at the start of the test.
// */
// private $sql_mode;

// /**
// * @var int ID of the current test user.
// */
// private $current_user_id;

/**
* @var array List of filters that have been turned off.
*/
Expand All @@ -38,75 +28,19 @@ abstract class AWPCP_UnitTestCase extends PHPUnit\Framework\TestCase {
*/
public function setUp(): void {
parent::setUp();

Monkey\setup();

// $this->save_current_user();
$this->mockCommonWpFunctions();
}

// /**
// * TODO: We probably won't need this if we stop using WordPress testing framework.
// */
// private function save_current_user() {
// $current_user = wp_get_current_user();

// if ( ! is_null( $current_user ) ) {
// $this->current_user_id = $current_user->ID;
// } else {
// $this->current_user_id = 0;
// }
// }

// /**
// * TODO: We probably won't need this if we stop using WordPress testing framework.
// */
// private function restore_current_user() {
// if ( $this->current_user_id > 0 ) {
// wp_set_current_user( $this->current_user_id );
// } else {
// global $current_user;
// $current_user = null;
// }
// }

/**
* TODO: We probably won't need this if we stop using WordPress testing framework.
*/
// public function start_transaction() {
// $this->activate_mysql_strict_mode();
// parent::start_transaction();
// }

// /**
// * TODO: We probably won't need this if we stop using WordPress testing framework.
// */
// private function activate_mysql_strict_mode() {
// global $wpdb;
// $this->sql_mode = $wpdb->get_var( 'SELECT @@SESSION.sql_mode' );
// $wpdb->query( "SET @@SESSION.sql_mode = 'TRADITIONAL';" );
// }

/**
* Code executed at the end of every test.
*/
public function tearDown(): void {
array_map( 'Patchwork\restore', $this->redefined_functions );
// $this->restore_mysql_mode();
// $this->restore_current_user();
// $this->resume_all_filters();
Monkey\teardown();

parent::tearDown();
}

// /**
// * TODO: We probably won't need this if we stop using WordPress testing framework.
// */
// private function restore_mysql_mode() {
// global $wpdb;
// $wpdb->query( $wpdb->prepare( 'SET @@SESSION.sql_mode = %s', $this->sql_mode ) );
// }

/**
* @since 4.0.0
*/
Expand Down Expand Up @@ -218,4 +152,56 @@ function( $arg ) use ( &$param ) {
}
);
}
protected function mockCommonWpFunctions() {
Functions\stubs(
[
'__',
'esc_attr__',
'esc_html__',
'_x',
'esc_attr_x',
'esc_html_x',
'_n',
'_nx',
'esc_attr',
'esc_html',
'esc_textarea',
'esc_url',
'sanitize_text_field',
'wp_parse_args' => static function ( $settings, $defaults ) {
return \array_merge( $defaults, $settings );
},
'wp_slash' => null,
'wp_unslash' => static function( $value ) {
return \is_string( $value ) ? \stripslashes( $value ) : $value;
},
'wp_rand' => static function() {
return rand();
},
'esc_url_raw',
]
);

$functions = [
'_e',
'esc_attr_e',
'esc_html_e',
'_ex',
];

foreach ( $functions as $function ) {
Functions\when( $function )->echoArg();
}
}

protected function expectAddQueryArg( $key = null, $val = null, $url = null ) {
Functions\expect( 'add_query_arg' )->andReturnUsing(
function () use ( $key, $val, $url ) {
if ( is_array( $key ) ) {
return 'https://example.org' . '?' . key( $key ) . '=' . $key[ key( $key ) ];
} else {
return $url . '?' . $key . '=' . $val;
}
} );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
/**
* Unit tests for Awaiting Approval listings table view.
*/
use Brain\Monkey\Functions;

class AWPCP_AwaitingApprovalListingTableViewTest extends AWPCP_UnitTestCase {

/**
* @since 4.0.0
*/
public function setUp(): void {
parent::setUp();
$this->test_helper = new AWPCP_ListingTableViewTestHelper( $this );

$this->listings_collection = Mockery::mock( 'AWPCP_ListingsCollection' );
Expand All @@ -21,6 +24,7 @@ public function setUp(): void {
* @since 4.0.0
*/
public function test_common_features() {
$this->expectAddQueryArg();
$this->test_helper->check_common_table_view_methods( $this->get_test_subject() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/**
* Unit tests for Complete Listing Table View.
*/
use Brain\Monkey\Functions;

class AWPCP_CompleteListingTableViewTest extends AWPCP_UnitTestCase {

/**
Expand All @@ -23,6 +25,7 @@ public function setUp(): void {
* @since 4.0.0
*/
public function test_common_features() {
$this->expectAddQueryArg();
$this->test_helper->check_common_table_view_methods( $this->get_test_subject() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
/**
* Unit tests for Expired Listings table view.
*/
use Brain\Monkey\Functions;

class AWPCP_ExpiredListingTableViewTest extends AWPCP_UnitTestCase {

/**
* @since 4.0.0
*/
public function setUp(): void {
parent::setUp();
$this->table_view_test_helper = new AWPCP_ListingTableViewTestHelper( $this );

$this->listings_collection = Mockery::mock( 'AWPCP_ListingsCollection' );
Expand All @@ -21,6 +24,7 @@ public function setUp(): void {
* @since 4.0.0
*/
public function test_common_features() {
$this->expectAddQueryArg();
$this->table_view_test_helper->check_common_table_view_methods( $this->get_test_subject() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AWPCP_FlaggedListingTableViewTest extends AWPCP_UnitTestCase {
* @since 4.0.0
*/
public function setUp(): void {
parent::setUp();
$this->test_helper = new AWPCP_ListingTableViewTestHelper( $this );

$this->listings_collection = Mockery::mock( 'AWPCP_ListingsCollection' );
Expand All @@ -21,6 +22,7 @@ public function setUp(): void {
* @since 4.0.0
*/
public function test_common_features() {
$this->expectAddQueryArg();
$this->test_helper->check_common_table_view_methods( $this->get_test_subject() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AWPCP_ImagesAwaitingApprovalListingTableViewTest extends AWPCP_UnitTestCas
* @since 4.0.0
*/
public function setUp(): void {
parent::setUp();
$this->test_helper = new AWPCP_ListingTableViewTestHelper( $this );

$this->listings_collection = Mockery::mock( 'AWPCP_ListingsCollection' );
Expand All @@ -21,6 +22,7 @@ public function setUp(): void {
* @since 4.0.0
*/
public function test_common_features() {
$this->expectAddQueryArg();
$this->test_helper->check_common_table_view_methods( $this->get_test_subject() );
}

Expand Down
Loading

0 comments on commit 83a3fe5

Please sign in to comment.