Skip to content

Commit

Permalink
adds abstract test case
Browse files Browse the repository at this point in the history
  • Loading branch information
datengraben committed Jul 23, 2023
1 parent 28c2a93 commit 0e37241
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
29 changes: 1 addition & 28 deletions tests/API/AvailabilityRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,19 @@

namespace CommonsBooking\Tests\API;

use CommonsBooking\Plugin;
use CommonsBooking\Repository\BookingCodes;
use CommonsBooking\Settings\Settings;
use CommonsBooking\Wordpress\CustomPostType\Item;
use CommonsBooking\Wordpress\CustomPostType\Location;
use CommonsBooking\Wordpress\CustomPostType\Timeframe;
use SlopeIt\ClockMock\ClockMock;

class AvailabilityRouteTest extends \WP_UnitTestCase {
class AvailabilityRouteTest extends CB_REST_Route_UnitTestCase {

const USER_ID = 1;
const CURRENT_DATE = '2021-05-21';
protected $ENDPOINT = '/commonsbooking/v1/availability';

protected $server;

/**
* TODO move to abstract api test router
*/
public function setUp() : void {
parent::setUp();
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$this->server = $wp_rest_server = new \WP_REST_Server;

// Enables api
Settings::updateOption( 'commonsbooking_options_api', 'api-activated' , 'on' );
Settings::updateOption( 'commonsbooking_options_api', 'apikey_not_required', 'on' );

// Registers routes (via rest_api_init hook)
( new Plugin() )->initRoutes();

// Applies hook
do_action( 'rest_api_init' );

// TODO creates initial data (should be mocked in the future)
ClockMock::freeze( new \DateTime( self::CURRENT_DATE ) );
Expand All @@ -61,12 +40,6 @@ public function setUp() : void {
ClockMock::reset();
}

// TODO move to abstract test case
public function testRoutes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( $this->ENDPOINT, $routes );
}

public function testsAvailabilitySuccess() {

ClockMock::freeze( new \DateTime( self::CURRENT_DATE ) );
Expand Down
17 changes: 17 additions & 0 deletions tests/API/CB_REST_Route_UnitTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace CommonsBooking\Tests\API;

/**
* Abstract test case which implicitly tests REST Routes
*/
class CB_REST_Route_UnitTestCase extends CB_REST_UnitTestCase {

protected $ENDPOINT;

public function testRoute() {
$this->assertNotNull( $this->ENDPOINT );
$routes = $this->server->get_routes();
$this->assertArrayHasKey( $this->ENDPOINT, $routes );
}
}
33 changes: 33 additions & 0 deletions tests/API/CB_REST_UnitTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace CommonsBooking\Tests\API;

use CommonsBooking\Plugin;
use CommonsBooking\Settings\Settings;

/**
* Abstract Unit Test case, which initializes REST functionality
*/
class CB_REST_UnitTestCase extends \WP_UnitTestCase {

protected $server;

protected $ENDPOINT;

public function setUp() : void {
parent::setUp();
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$this->server = $wp_rest_server = new \WP_REST_Server;

// Enables api
Settings::updateOption( 'commonsbooking_options_api', 'api-activated', 'on' );
Settings::updateOption( 'commonsbooking_options_api', 'apikey_not_required', 'on' );

// Registers routes (via rest_api_init hook)
( new Plugin() )->initRoutes();

// Applies hook
do_action( 'rest_api_init' );
}
}

0 comments on commit 0e37241

Please sign in to comment.