diff --git a/composer.json b/composer.json index 7574872..3ec2293 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5060c5e..b9b79a9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -17,8 +17,8 @@ testdox="true"> - - ./tests/ + + ./tests/Integration/ diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php new file mode 100644 index 0000000..1daefc0 --- /dev/null +++ b/tests/Integration/TestCase.php @@ -0,0 +1,16 @@ +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 ) { @@ -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 ); } @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ) { @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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', @@ -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', @@ -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( @@ -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; } @@ -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(), diff --git a/tests/README.md b/tests/README.md index 439a9f9..5c38e82 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,4 +1,4 @@ -### Running Unit Tests +### Running Integration Tests ```bash composer install diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8804db1..f663eec 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,23 +1,53 @@