Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix test cases #47

Merged
merged 27 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d7aeb58
fix test cases
mostafa-hisham Mar 29, 2023
1a65242
fix more tests
mostafa-hisham May 5, 2023
191734c
add phpunit-arraysubset-asserts
mostafa-hisham May 5, 2023
57cc6dd
ignore phpunit cache file
mostafa-hisham May 5, 2023
ede6a43
vendor and composer.lock
mostafa-hisham May 5, 2023
5f6f602
fix the last tests
mostafa-hisham May 8, 2023
938144f
fix form fields test
mostafa-hisham May 8, 2023
da8936c
clean up
mostafa-hisham May 8, 2023
4bc0022
update workflow ubuntu to 20.04
mostafa-hisham May 8, 2023
41966d6
update workflow ubuntu add phpunit arraysubset-asserts
mostafa-hisham May 8, 2023
951d7fd
echo dir
mostafa-hisham May 8, 2023
8a1f4b9
echo dir
mostafa-hisham May 8, 2023
b041c49
echo dir
mostafa-hisham May 8, 2023
040cdd7
find tools
mostafa-hisham May 8, 2023
1d5e4d5
find tools
mostafa-hisham May 8, 2023
7650950
find patchwork
mostafa-hisham May 8, 2023
6092fdd
find patchwork
mostafa-hisham May 8, 2023
1b8a87c
find patchwork
mostafa-hisham May 8, 2023
4eefed6
find patchwork
mostafa-hisham May 8, 2023
e8c22be
find patchwork
mostafa-hisham May 8, 2023
2b15677
cleanup
mostafa-hisham May 8, 2023
95f78e0
run tests from inside the plugin
mostafa-hisham May 9, 2023
701f733
run tests from inside the plugin fix path
mostafa-hisham May 9, 2023
d2b1601
cleanup and remove unused env
mostafa-hisham May 9, 2023
d091316
remove env from workflow
mostafa-hisham May 10, 2023
8ff9dec
create expectAddQueryArg function to avoid duplication
mostafa-hisham May 12, 2023
2505adb
run composer with no dev flag
mostafa-hisham May 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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