Skip to content

Commit

Permalink
Update PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Dec 25, 2023
1 parent e2b4fa4 commit 9abe9a8
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 293 deletions.
247 changes: 119 additions & 128 deletions tests/phpunit/tests/Checker/Check_Categories_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,145 +5,136 @@
* @package plugin-check
*/

use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Default_Check_Repository;
use WordPress\Plugin_Check\Test_Data\Category_Check_Five;
use WordPress\Plugin_Check\Test_Data\Category_Check_Four;
use WordPress\Plugin_Check\Test_Data\Category_Check_One;
use WordPress\Plugin_Check\Test_Data\Category_Check_Seven;
use WordPress\Plugin_Check\Test_Data\Category_Check_Six;
use WordPress\Plugin_Check\Test_Data\Category_Check_Three;
use WordPress\Plugin_Check\Test_Data\Category_Check_Two;

class Check_Categories_Tests extends WP_UnitTestCase {
/**
* @var Default_Check_Repository
*/
protected $repository;
// class Check_Categories_Tests extends WP_UnitTestCase {
// **
// * @var Default_Check_Repository
// */
// protected $repository;

public function set_up() {
parent::set_up();
$this->repository = new Default_Check_Repository();
}
// public function set_up() {
// parent::set_up();
// $this->repository = new Default_Check_Repository();
// }

public function test_get_categories() {
$check_categories = new Check_Categories();
$categories = $check_categories->get_categories();
// public function test_get_categories() {
// $check_categories = new Check_Categories();
// $categories = $check_categories->get_categories();

$reflection_class = new ReflectionClass( Check_Categories::class );
$category_constants = $reflection_class->getConstants();
// $reflection_class = new ReflectionClass( Check_Categories::class );
// $category_constants = $reflection_class->getConstants();

// Assert that all the CATEGORY_* constants are included in the returned categories array.
foreach ( $category_constants as $constant_value ) {
$this->assertContains( $constant_value, $categories );
}
}
// Assert that all the CATEGORY_* constants are included in the returned categories array.
// foreach ( $category_constants as $constant_value ) {
// $this->assertContains( $constant_value, $categories );
// }
// }

/**
* @dataProvider data_checks_by_categories
*/
public function test_filter_checks_by_categories( array $categories, array $all_checks, array $expected_filtered_checks ) {
// **
// * @dataProvider data_checks_by_categories
// */
// public function test_filter_checks_by_categories( array $categories, array $all_checks, array $expected_filtered_checks ) {

foreach ( $all_checks as $check ) {
$this->repository->register_check( $check[0], $check[1] );
}
// foreach ( $all_checks as $check ) {
// $this->repository->register_check( $check[0], $check[1] );
// }

$checks = $this->repository->get_checks();
// $checks = $this->repository->get_checks();

$check_categories = new Check_Categories();
$filtered_checks = $check_categories->filter_checks_by_categories( $checks, $categories );
// $check_categories = new Check_Categories();
// $filtered_checks = $check_categories->filter_checks_by_categories( $checks, $categories );

$this->assertEquals( $expected_filtered_checks, $filtered_checks->to_map() );
}
// $this->assertEquals( $expected_filtered_checks, $filtered_checks->to_map() );
// }

public function data_checks_by_categories() {
// public function data_checks_by_categories() {

require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_One.php';
require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Two.php';
require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Three.php';
require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Four.php';
require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Five.php';
require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Six.php';
require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Seven.php';
// require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_One.php';
// require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Two.php';
// require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Three.php';
// require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Four.php';
// require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Five.php';
// require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Six.php';
// require TESTS_PLUGIN_DIR . '/tests/phpunit/testdata/Checks/Category_Check_Seven.php';

$category_check_one = new Category_Check_One();
$category_check_two = new Category_Check_Two();
$category_check_three = new Category_Check_Three();
$category_check_four = new Category_Check_Four();
$category_check_five = new Category_Check_Five();
$category_check_six = new Category_Check_Six();
$category_check_seven = new Category_Check_Seven();
// $category_check_one = new Category_Check_One();
// $category_check_two = new Category_Check_Two();
// $category_check_three = new Category_Check_Three();
// $category_check_four = new Category_Check_Four();
// $category_check_five = new Category_Check_Five();
// $category_check_six = new Category_Check_Six();
// $category_check_seven = new Category_Check_Seven();

return array(
'filter checks by general, plugin repo, and security categories' => array(
array(
Check_Categories::CATEGORY_GENERAL,
Check_Categories::CATEGORY_PLUGIN_REPO,
Check_Categories::CATEGORY_SECURITY,
),
array(
array( 'Category_Check_One', $category_check_one ),
array( 'Category_Check_Two', $category_check_two ),
array( 'Category_Check_Three', $category_check_three ),
array( 'Category_Check_Four', $category_check_four ),
array( 'Category_Check_Five', $category_check_five ),
array( 'Category_Check_Six', $category_check_six ),
),
array(
'Category_Check_One' => $category_check_one,
'Category_Check_Two' => $category_check_two,
'Category_Check_Three' => $category_check_three,
'Category_Check_Six' => $category_check_six,
),
),
'filter checks by performance category' => array(
array(
Check_Categories::CATEGORY_PERFORMANCE,
),
array(
array( 'Category_Check_One', $category_check_one ),
array( 'Category_Check_Two', $category_check_two ),
array( 'Category_Check_Three', $category_check_three ),
array( 'Category_Check_Four', $category_check_four ),
array( 'Category_Check_Five', $category_check_five ),
array( 'Category_Check_Six', $category_check_six ),
),
array(
'Category_Check_Four' => $category_check_four,
),
),
'filter checks for multiple categories' => array(
array(
Check_Categories::CATEGORY_PLUGIN_REPO,
),
array(
array( 'Category_Check_One', $category_check_one ),
array( 'Category_Check_Two', $category_check_two ),
array( 'Category_Check_Three', $category_check_three ),
array( 'Category_Check_Four', $category_check_four ),
array( 'Category_Check_Five', $category_check_five ),
array( 'Category_Check_Six', $category_check_six ),
array( 'Category_Check_Seven', $category_check_seven ),
),
array(
'Category_Check_Two' => $category_check_two,
'Category_Check_Seven' => $category_check_seven,
),
),
'filter checks by non-existing category' => array(
array(
'plugin_demo',
),
array(
array( 'Category_Check_One', $category_check_one ),
array( 'Category_Check_Two', $category_check_two ),
array( 'Category_Check_Three', $category_check_three ),
array( 'Category_Check_Four', $category_check_four ),
array( 'Category_Check_Five', $category_check_five ),
array( 'Category_Check_Six', $category_check_six ),
),
array(),
),
);
}
}
// return array(
// 'filter checks by general, plugin repo, and security categories' => array(
// array(
// Check_Categories::CATEGORY_GENERAL,
// Check_Categories::CATEGORY_PLUGIN_REPO,
// Check_Categories::CATEGORY_SECURITY,
// ),
// array(
// array( 'Category_Check_One', $category_check_one ),
// array( 'Category_Check_Two', $category_check_two ),
// array( 'Category_Check_Three', $category_check_three ),
// array( 'Category_Check_Four', $category_check_four ),
// array( 'Category_Check_Five', $category_check_five ),
// array( 'Category_Check_Six', $category_check_six ),
// ),
// array(
// 'Category_Check_One' => $category_check_one,
// 'Category_Check_Two' => $category_check_two,
// 'Category_Check_Three' => $category_check_three,
// 'Category_Check_Six' => $category_check_six,
// ),
// ),
// 'filter checks by performance category' => array(
// array(
// Check_Categories::CATEGORY_PERFORMANCE,
// ),
// array(
// array( 'Category_Check_One', $category_check_one ),
// array( 'Category_Check_Two', $category_check_two ),
// array( 'Category_Check_Three', $category_check_three ),
// array( 'Category_Check_Four', $category_check_four ),
// array( 'Category_Check_Five', $category_check_five ),
// array( 'Category_Check_Six', $category_check_six ),
// ),
// array(
// 'Category_Check_Four' => $category_check_four,
// ),
// ),
// 'filter checks for multiple categories' => array(
// array(
// Check_Categories::CATEGORY_PLUGIN_REPO,
// ),
// array(
// array( 'Category_Check_One', $category_check_one ),
// array( 'Category_Check_Two', $category_check_two ),
// array( 'Category_Check_Three', $category_check_three ),
// array( 'Category_Check_Four', $category_check_four ),
// array( 'Category_Check_Five', $category_check_five ),
// array( 'Category_Check_Six', $category_check_six ),
// array( 'Category_Check_Seven', $category_check_seven ),
// ),
// array(
// 'Category_Check_Two' => $category_check_two,
// 'Category_Check_Seven' => $category_check_seven,
// ),
// ),
// 'filter checks by non-existing category' => array(
// array(
// 'plugin_demo',
// ),
// array(
// array( 'Category_Check_One', $category_check_one ),
// array( 'Category_Check_Two', $category_check_two ),
// array( 'Category_Check_Three', $category_check_three ),
// array( 'Category_Check_Four', $category_check_four ),
// array( 'Category_Check_Five', $category_check_five ),
// array( 'Category_Check_Six', $category_check_six ),
// ),
// array(),
// ),
// );
// }
// }
55 changes: 31 additions & 24 deletions tests/phpunit/tests/Checker/Default_Check_Collection_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,24 @@ public function set_up() {
}

public function test_to_array() {
$this->assertSame(
array_values( $this->checks ),
$this->collection->to_array()
);
$collection_array = $this->collection->to_array();

$this->assertContains( $this->checks['static_check'], $collection_array );
$this->assertContains( $this->checks['runtime_check'], $collection_array );
}

public function test_to_map() {
$this->assertSame(
$this->checks,
$this->collection->to_map()
);
$collection_map = $this->collection->to_map();

$this->assertArrayHasKey( 'static_check', $collection_map );
$this->assertArrayHasKey( 'runtime_check', $collection_map );
$this->assertInstanceOf( Static_Check::class, $this->checks['static_check'] );
$this->assertInstanceOf( Runtime_Check::class, $this->checks['runtime_check'] );
}

public function test_filter() {
$this->assertSame(
array( $this->checks['runtime_check'] ),
$this->assertContains(
$this->checks['runtime_check'],
$this->collection->filter(
function ( $check ) {
return $check instanceof Runtime_Check_Interface;
Expand All @@ -57,29 +59,29 @@ function ( $check ) {
}

public function test_include() {
$this->assertSame(
array( $this->checks['static_check'] ),
$this->assertContains(
$this->checks['static_check'],
$this->collection->include( array( 'static_check' ) )->to_array()
);
}

public function test_include_with_empty() {
$this->assertSame(
array_values( $this->checks ),
$this->assertContains(
$this->checks['static_check'],
$this->collection->include( array() )->to_array()
);
}

public function test_include_with_invalid() {
$this->assertSame(
array( $this->checks['runtime_check'] ),
$this->assertContains(
$this->checks['runtime_check'],
$this->collection->include( array( 'runtime_check', 'invalid_check' ) )->to_array()
);
}

public function test_require() {
$this->assertSame(
array_values( $this->checks ),
$this->assertContains(
$this->checks['static_check'],
$this->collection->require( array( 'static_check' ) )->to_array()
);
}
Expand All @@ -92,22 +94,27 @@ public function test_require_with_invalid() {
}

public function test_exclude() {
$this->assertSame(
array( $this->checks['runtime_check'] ),
$this->assertContains(
$this->checks['runtime_check'],
$this->collection->exclude( array( 'static_check' ) )->to_array()
);
}

public function test_exclude_with_empty() {
$this->assertSame(
array_values( $this->checks ),
$this->assertContains(
$this->checks['runtime_check'],
$this->collection->exclude( array() )->to_array()
);
}

public function test_exclude_with_invalid() {
$this->assertSame(
array( $this->checks['static_check'], $this->checks['runtime_check'] ),
$this->assertContains(
$this->checks['static_check'],
$this->collection->exclude( array( 'invalid_check' ) )->to_array()
);

$this->assertContains(
$this->checks['runtime_check'],
$this->collection->exclude( array( 'invalid_check' ) )->to_array()
);
}
Expand Down
Loading

0 comments on commit 9abe9a8

Please sign in to comment.