Skip to content

Commit

Permalink
Add PHPUnit abstract facade.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed Jan 24, 2021
1 parent 78d0942 commit 2fce615
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
34 changes: 34 additions & 0 deletions app/Hametuha/Sharee/Tests/UnitTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Hametuha\Sharee\Tests;

use Hametuha\Sharee\Models\RevenueMetaModel;
use Hametuha\Sharee\Models\RevenueModel;

/**
* Utility class for UnitTest
*
* @package sharee
* @property-read RevenueMetaModel $revenue_meta Revenue meta model.
* @property-read RevenueModel $revenue Revenue model.
*/
abstract class UnitTestCase extends \WP_UnitTestCase {

/**
* Getter
*
* @param string $name
*
* @return mixed
*/
public function __get( $name ) {
switch ( $name ) {
case 'revenue':
return RevenueModel::get_instance();
case 'revenue_meta':
return RevenueMetaModel::get_instance();
default:
return parent::__get( $name );
}
}
}
5 changes: 3 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/vendor/autoload.php';
\Hametuha\Sharee::get_instance();
require dirname( __DIR__ ) . '/sharee.php';
\Hametuha\Sharee\Models\RevenueModel::get_instance()->create_table();
\Hametuha\Sharee\Models\RevenueMetaModel::get_instance()->create_table();
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

Expand Down
32 changes: 11 additions & 21 deletions tests/test-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,22 @@
/**
* Sample test case.
*/
class FunctionsTest extends WP_UnitTestCase {

/**
* Create table
*/
public static function setUpBeforeClass() {
\Hametuha\Sharee\Models\RevenueModel::get_instance()->create_table();
\Hametuha\Sharee\Models\RevenueMetaModel::get_instance()->create_table();
}
class FunctionsTest extends \Hametuha\Sharee\Tests\UnitTestCase {

/**
* Test for range function.
*/
function test_range() {
// Check range
$model = \Hametuha\Sharee\Models\RevenueModel::get_instance();
list( $start, $end ) = $model->get_month_range( 2018, 1 );
list( $start, $end ) = $this->revenue->get_month_range( 2018, 1 );
$this->assertEquals( '2018-01-01 00:00:00', $start );
$this->assertEquals( '2018-01-31 23:59:59', $end );
// Check leap year
list( $start, $end ) = $model->get_month_range( 2004, 2 );
list( $start, $end ) = $this->revenue->get_month_range( 2004, 2 );
$this->assertEquals( '2004-02-01 00:00:00', $start );
$this->assertEquals( '2004-02-29 23:59:59', $end );
// Check exceptional leap year.
list( $start, $end ) = $model->get_month_range( 1900, 2 );
list( $start, $end ) = $this->revenue->get_month_range( 1900, 2 );
$this->assertEquals( '1900-02-01 00:00:00', $start );
$this->assertEquals( '1900-02-28 23:59:59', $end );
}
Expand All @@ -41,28 +32,27 @@ function test_range() {
* Test for basic insertion
*/
function test_crud() {
$model = \Hametuha\Sharee\Models\RevenueModel::get_instance();
// Save revenue.
$revenue_id = $model->add_revenue( 'task', 1, 1000 );
$revenue_id = $this->revenue->add_revenue( 'task', 1, 1000 );
if ( is_wp_error( $revenue_id ) ) {
throw new \Exception( $revenue_id->get_error_message() );
}
$this->assertTrue( is_numeric( $revenue_id ) );
// Get revenue
$revenue = $model->get( $revenue_id );
$revenue = $this->revenue->get( $revenue_id );
$this->assertObjectHasAttribute( 'revenue_id', $revenue );
// Update revenue;
$model->update_status( $revenue_id, 1 );
$revenue = $model->get( $revenue_id );
$this->revenue->update_status( $revenue_id, 1 );
$revenue = $this->revenue->get( $revenue_id );
$this->assertEquals( '1', $revenue->status );
// Check log.
$log = $model->revenue_meta->get_logs( $revenue_id );
$log = $this->revenue->revenue_meta->get_logs( $revenue_id );
$this->assertEquals( 1, count( $log ) );
// Delete revenue.
$model->delete( [
$this->revenue->delete( [
'revenue_id' => $revenue_id,
] );
$revenue = $model->get( $revenue_id );
$revenue = $this->revenue->get( $revenue_id );
$this->assertNull( $revenue );
}
}
Expand Down

0 comments on commit 2fce615

Please sign in to comment.