Skip to content

Adding integration tests #109

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

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"phpcompatibility/php-compatibility": "*",
"wp-coding-standards/wpcs": "*",
"phpunit/phpunit": "^8.5",
"yoast/phpunit-polyfills": "^2.0"
"yoast/phpunit-polyfills": "^2.0",
"wp-phpunit/wp-phpunit": "^6.7"
},
"require": {
"php": ">=7.4"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"format": "wp-scripts format",
"wp-env": "wp-env",
"release": "composer run build && npm run build && npm run plugin-zip",
"test:unit": "./vendor/bin/phpunit"
"test:unit": "./vendor/bin/phpunit --configuration ./tests/unit/phpunit.xml",
"test:integration": "wp-env run tests-cli --env-cwd=wp-content/plugins/advanced-query-loop ./vendor/bin/phpunit --configuration ./tests/integration/phpunit.xml"
},
"files": [
"build",
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

<!-- Dont need to capitalize first letter -->
<exclude name="Generic.Commenting.DocComment.ShortNotCapital"/>


</rule>

<rule ref="WordPress-Extra">
Expand All @@ -64,6 +66,7 @@

<!-- Allow shorthand array syntax -->
<exclude name="Generic.Arrays.DisallowShortArraySyntax.Found"/>
<exclude name="Universal.Arrays.DisallowShortArraySyntax.Found"/>

<!-- Allow WP global modification -->
<exclude name="WordPress.WP.GlobalVariablesOverride.Prohibited"/>
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* PHPUnit bootstrap file
*
* @package features-plugin-v2
*/

define( 'TESTS_PLUGIN_DIR', dirname( __DIR__ ) );
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', dirname( TESTS_PLUGIN_DIR ) . '/vendor/yoast/phpunit-polyfills' );

// Determine correct location for plugins directory to use.
define( 'WP_PLUGIN_DIR', dirname( dirname( TESTS_PLUGIN_DIR ) ) );

define( 'WP_PHPUNIT__DIR', dirname( TESTS_PLUGIN_DIR ) . '/vendor/wp-phpunit/wp-phpunit/' );

// Load Composer dependencies if applicable.
if ( file_exists( dirname( TESTS_PLUGIN_DIR ) . '/vendor/autoload.php' ) ) {
require_once dirname( TESTS_PLUGIN_DIR ) . '/vendor/autoload.php';
}

// Detect where to load the WordPress tests environment from.

$_test_root = WP_PHPUNIT__DIR;

require_once $_test_root . '/includes/functions.php';

/**
* Load plugin in test env.
*
* @return void
*/
function features_plugin_unit_test_load_plugin_file() {
require_once dirname( TESTS_PLUGIN_DIR ) . '/index.php';
}

tests_add_filter( 'muplugins_loaded', 'features_plugin_unit_test_load_plugin_file' );

require $_test_root . '/includes/bootstrap.php';
17 changes: 17 additions & 0 deletions tests/integration/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<phpunit
bootstrap="./bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="unittest">
<directory suffix="_Tests.php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="WP_PHPUNIT__TESTS_CONFIG" value="/wordpress-phpunit/wp-tests-config.php"/>
</php>
</phpunit>
82 changes: 82 additions & 0 deletions tests/integration/tests/Filter_Tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Test the filter
*/
namespace AdvancedQueryLoop\Tests\Integration;

use AdvancedQueryLoop\Query_Params_Generator;

/**
* Test the filter
*/
class AQL_Filter_Tests extends \WP_UnitTestCase {

public function test_tax_query_add_items_to_custom_tax_query() {

$expected_query_args = [
'tax_query' => [
[
'taxonomy' => 'category',
'terms' => [ 1 ],
'include_children' => true,
'operator' => 'IN',
],
[
'taxonomy' => 'recipe_category',
'field' => 'slug',
'terms' => 'pasta',
'operator' => 'NOT IN',
'include_children' => false,
],
],
];

$default_params_from_block = [];

$custom_params_from_aql = [
'tax_query' => [
'queries' => [
[
'id' => '9aa9887e-e61b-42e0-bc5d-3b8c60337a3b',
'taxonomy' => 'category',
'terms' => [ 'Uncategorized' ],
'include_children' => true,
'operator' => 'IN',
],
],
],
];

$qpg = new Query_Params_Generator( $default_params_from_block, $custom_params_from_aql );

add_filter(
'aql_query_vars',
function ( $query_args, $default_params, $context ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'recipe_category',
'field' => 'slug',
'terms' => 'pasta',
'operator' => 'NOT IN',
'include_children' => false,
);
return $query_args;
},
10,
3
);

$qpg->process_all();
$query_args = $qpg->get_query_args();
$filtered_query_args = \apply_filters(
'aql_query_vars',
$query_args,
$default_params_from_block,
false
);
// Assertions
$this->assertSame( $expected_query_args, array_merge( $default_params_from_block, $filtered_query_args ) );
}

public function test_tax_query_add_items_to_default_tax_query() {}

}
4 changes: 2 additions & 2 deletions phpunit.xml → tests/unit/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
bootstrap="../../vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
Expand All @@ -12,7 +12,7 @@
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix=".php">tests/unit</directory>
<directory suffix=".php">tests</directory>
</testsuite>
</testsuites>

Expand Down
File renamed without changes.
Loading