Skip to content
Merged
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"lint-ci": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git --checkstyle"
],
"test:integration": "wp-env run tests-cli --env-cwd=wp-content/plugins/zoninator ./vendor/bin/phpunit --testsuite WP_Tests",
"test:integration-ms": "wp-env run tests-cli --env-cwd=wp-content/plugins/zoninator /bin/bash -c 'WP_MULTISITE=1 ./vendor/bin/phpunit --testsuite WP_Tests'"
"test:integration": "wp-env run tests-cli --env-cwd=wp-content/plugins/zoninator ./vendor/bin/phpunit --testsuite integration",
"test:integration-ms": "wp-env run tests-cli --env-cwd=wp-content/plugins/zoninator /bin/bash -c 'WP_MULTISITE=1 ./vendor/bin/phpunit --testsuite integration'"
},
"scripts-descriptions": {
"coverage": "Run tests with code coverage reporting",
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
testdox="true">

<testsuites>
<testsuite name="WP_Tests">
<directory suffix="-test.php">./tests/</directory>
<testsuite name="integration">
<directory>./tests/Integration/</directory>
</testsuite>
</testsuites>
</phpunit>
16 changes: 16 additions & 0 deletions tests/Integration/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Base test case for integration tests.
*
* @package Automattic\Zoninator
*/

namespace Automattic\Zoninator\Tests\Integration;

use Yoast\WPTestUtils\WPIntegration\TestCase as WPIntegrationTestCase;

/**
* Base test case for integration tests.
*/
abstract class TestCase extends WPIntegrationTestCase {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?php
/**
* Integration tests for the Zoninator API controller.
*
* @package Automattic\Zoninator
*/

class Zoninator_Api_Controller_Test extends WP_UnitTestCase {
namespace Automattic\Zoninator\Tests\Integration;

class Zoninator_Api_Controller_Test extends TestCase {

/**
* REST Server
*
* @var WP_REST_Server
* @var \WP_REST_Server
*/
protected $rest_server;

Expand Down Expand Up @@ -33,7 +40,7 @@ class Zoninator_Api_Controller_Test extends WP_UnitTestCase {
/**
* Assert Status
*
* @param WP_REST_Response $response Response.
* @param \WP_REST_Response $response Response.
* @param int $status_code Code.
*/
public function assert_response_status( $response, $status_code ) {
Expand Down Expand Up @@ -63,34 +70,34 @@ public function login_as( $user_id ) {
/**
* Assert Status 200
*
* @param WP_REST_Response $response Response.
* @param \WP_REST_Response $response Response.
*/
public function assert_http_response_status_success( $response ) {
$this->assert_response_status( $response, MT_Controller::HTTP_OK );
$this->assert_response_status( $response, \MT_Controller::HTTP_OK );
}

/**
* Assert Status 201
*
* @param WP_REST_Response $response Response.
* @param \WP_REST_Response $response Response.
*/
public function assert_http_response_status_created( $response ) {
$this->assert_response_status( $response, MT_Controller::HTTP_CREATED );
$this->assert_response_status( $response, \MT_Controller::HTTP_CREATED );
}

/**
* Assert Status 404
*
* @param WP_REST_Response $response Response.
* @param \WP_REST_Response $response Response.
*/
public function assert_http_response_status_not_found( $response ) {
$this->assert_response_status( $response, MT_Controller::HTTP_NOT_FOUND );
$this->assert_response_status( $response, \MT_Controller::HTTP_NOT_FOUND );
}

/**
* Ensure we got a certain response code
*
* @param WP_REST_Response $response The Response.
* @param \WP_REST_Response $response The Response.
* @param int $status_code Expected status code.
*/
public function assertResponseStatus( $response, $status_code ) {
Expand All @@ -104,10 +111,10 @@ public function assertResponseStatus( $response, $status_code ) {
* @param string $endpoint The Endpoint.
* @param string $method Http method.
* @param array $args Any Data/Args.
* @return WP_REST_Response
* @return \WP_REST_Response
*/
public function request( $endpoint, $method, $args = array() ) {
$request = new WP_REST_Request( $method, $endpoint );
$request = new \WP_REST_Request( $method, $endpoint );
foreach ( $args as $key => $value ) {
$request->set_param( $key, $value );
}
Expand All @@ -120,7 +127,7 @@ public function request( $endpoint, $method, $args = array() ) {
*
* @param string $endpoint The Endpoint.
* @param array $args Any Data/Args.
* @return WP_REST_Response
* @return \WP_REST_Response
*/
public function get( $endpoint, $args = array() ) {
return $this->request( $endpoint, 'GET', $args );
Expand All @@ -131,7 +138,7 @@ public function get( $endpoint, $args = array() ) {
*
* @param string $endpoint The Endpoint.
* @param array $args Any Data/Args.
* @return WP_REST_Response
* @return \WP_REST_Response
*/
public function post( $endpoint, $args = array() ) {
return $this->request( $endpoint, 'POST', $args );
Expand All @@ -142,7 +149,7 @@ public function post( $endpoint, $args = array() ) {
*
* @param string $endpoint The Endpoint.
* @param array $args Any Data/Args.
* @return WP_REST_Response
* @return \WP_REST_Response
*/
public function put( $endpoint, $args = array() ) {
return $this->request( $endpoint, 'PUT', $args );
Expand All @@ -153,7 +160,7 @@ public function put( $endpoint, $args = array() ) {
*
* @param string $endpoint The Endpoint.
* @param array $args Any Data/Args.
* @return WP_REST_Response
* @return \WP_REST_Response
*/
public function delete( $endpoint, $args = array() ) {
return $this->request( $endpoint, 'DELETE', $args );
Expand All @@ -167,10 +174,10 @@ public function setUp(): void {
/**
*The global
*
* @var WP_REST_Server $wp_rest_server
* @var \WP_REST_Server $wp_rest_server
*/
global $wp_rest_server;
$this->rest_server = new Spy_REST_Server;
$this->rest_server = new \Spy_REST_Server();
$wp_rest_server = $this->rest_server;
$admin = get_user_by( 'email', 'rest_api_admin_user@test.com' );
if ( false === $admin ) {
Expand All @@ -187,13 +194,13 @@ public function setUp(): void {
$this->login_as_admin();
$this->rest_server = $wp_rest_server;
do_action( 'rest_api_init' );
$this->environment = Zoninator()->rest_api->bootstrap->environment();
$this->environment = \Zoninator()->rest_api->bootstrap->environment();
}

/**
* T test_create_zone_responds_with_created_when_method_post
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_create_zone_responds_with_created_when_method_post() {
$this->login_as_admin();
Expand All @@ -206,7 +213,7 @@ public function test_create_zone_responds_with_created_when_method_post() {
/**
* T test_create_zone_fail_if_invalid_data
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_create_zone_fail_if_invalid_data() {
$this->login_as_admin();
Expand All @@ -219,7 +226,7 @@ public function test_create_zone_fail_if_invalid_data() {
/**
* T test_create_zone_with_special_chars
*
* @throws Exception E
* @throws \Exception E
*/
public function test_create_zone_with_special_chars() {
$this->login_as_admin();
Expand All @@ -237,7 +244,7 @@ public function test_create_zone_with_special_chars() {
/**
* T test_update_zone_responds_with_success_when_method_put
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_update_zone_responds_with_success_when_method_put() {
$this->login_as_admin();
Expand All @@ -251,7 +258,7 @@ public function test_update_zone_responds_with_success_when_method_put() {
/**
* T test_update_zone_responds_with_not_found_if_zone_not_exist
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_update_zone_responds_with_not_found_if_zone_not_exist() {
$this->login_as_admin();
Expand All @@ -264,7 +271,7 @@ public function test_update_zone_responds_with_not_found_if_zone_not_exist() {
/**
* T test_delete_zone_responds_with_success_when_method_delete
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_delete_zone_responds_with_success_when_method_delete() {
$this->login_as_admin();
Expand All @@ -276,7 +283,7 @@ public function test_delete_zone_responds_with_success_when_method_delete() {
/**
* T test_delete_zone_responds_with_not_found_if_zone_not_exist
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_delete_zone_responds_with_not_found_if_zone_not_exist() {
$this->login_as_admin();
Expand All @@ -287,7 +294,7 @@ public function test_delete_zone_responds_with_not_found_if_zone_not_exist() {
/**
* T test_update_zone_posts_responds_with_ok_when_method_put
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_update_zone_posts_responds_with_success_when_method_put() {
$this->login_as_admin();
Expand All @@ -302,7 +309,7 @@ public function test_update_zone_posts_responds_with_success_when_method_put() {
/**
* T test_update_zone_posts_responds_with_not_found_if_zone_not_exist
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_update_zone_posts_responds_with_not_found_if_zone_not_exist() {
$this->login_as_admin();
Expand All @@ -316,7 +323,7 @@ public function test_update_zone_posts_responds_with_not_found_if_zone_not_exist
/**
* T test_update_zone_posts_fails_if_invalid_data_format
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_update_zone_posts_fails_if_invalid_data() {
$this->login_as_admin();
Expand All @@ -329,7 +336,7 @@ public function test_update_zone_posts_fails_if_invalid_data() {
/**
* T test_update_zone_posts_fails_if_invalid_post_id
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_update_zone_posts_fails_if_invalid_post_id() {
$this->login_as_admin();
Expand All @@ -344,12 +351,12 @@ public function test_update_zone_posts_fails_if_invalid_post_id() {
/**
* T test_get_zone_posts_success_when_valid_zone_and_posts
*
* @throws Exception E.
* @throws \Exception E.
*/
public function test_get_zone_posts_success_when_valid_zone_and_posts() {
$this->login_as_admin();
self::factory()->post->create_many( 5 );
$query = new WP_Query();
$query = new \WP_Query();
$posts = $query->query( array() );
$post = $posts[0];
$zone_id = $this->add_a_zone();
Expand All @@ -365,7 +372,7 @@ public function test_get_zone_posts_success_when_valid_zone_and_posts() {
* Test test_get_zone_posts_success_when_no_posts_in_zone
*/
public function test_get_zone_posts_success_when_no_posts_in_zone() {
$term_factory = new WP_UnitTest_Factory_For_Term( null, Zoninator()->zone_taxonomy );
$term_factory = new \WP_UnitTest_Factory_For_Term( null, \Zoninator()->zone_taxonomy );
$zone_id = $term_factory->create_object( array(
'name' => 'The Zone Add Post one',
'description' => 'Zone 2',
Expand All @@ -380,7 +387,7 @@ public function test_get_zone_posts_success_when_no_posts_in_zone() {
* Test test_get_zone_posts_not_found_when_invalid_zone
*/
public function test_get_zone_posts_not_found_when_invalid_zone() {
$term_factory = new WP_UnitTest_Factory_For_Term( null, Zoninator()->zone_taxonomy );
$term_factory = new \WP_UnitTest_Factory_For_Term( null, \Zoninator()->zone_taxonomy );
$zone_id = $term_factory->create_object( array(
'name' => 'The Zone Add Post one',
'description' => 'Zone 2',
Expand All @@ -392,7 +399,7 @@ public function test_get_zone_posts_not_found_when_invalid_zone() {
}

/**
* @return int|WP_Error
* @return int|\WP_Error
*/
private function _insert_a_post() {
$insert = wp_insert_post( array(
Expand All @@ -403,14 +410,14 @@ private function _insert_a_post() {
'post_type' => 'post'
) );
if ( is_wp_error( $insert ) ) {
throw new Exception( 'Error' );
throw new \Exception( 'Error' );
}

return $insert;
}

private function create_a_zone( $slug, $title ) {
$result = Zoninator()->insert_zone( $slug, $title, array( 'description' => rand_str() ) );
$result = \Zoninator()->insert_zone( $slug, $title, array( 'description' => rand_str() ) );
if ( is_wp_error( $result ) ) {
return $result;
}
Expand All @@ -423,10 +430,10 @@ private function create_a_zone( $slug, $title ) {
*
* @param string $slug Slug.
*
* @return array|mixed|WP_Error
* @return array|mixed|\WP_Error
*/
private function add_a_zone( $slug = 'zone-1' ) {
$term_factory = new WP_UnitTest_Factory_For_Term(null, Zoninator()->zone_taxonomy);
$term_factory = new \WP_UnitTest_Factory_For_Term(null, \Zoninator()->zone_taxonomy);
return $term_factory->create_object(array(
'name' => 'The Zone Add Post one ' . rand_str(),
'description' => 'Zone ' . rand_str(),
Expand Down
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Running Unit Tests
### Running Integration Tests

```bash
composer install
Expand Down
Loading