From bda5ad0f83b5e5709f3d24b938ab5a0686b549a2 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 31 Oct 2023 13:20:46 -0500 Subject: [PATCH 1/8] Implement Check_Collection to separate concerns for check filtering from Check_Repository. --- includes/Checker/Abstract_Check_Runner.php | 4 +- includes/Checker/Check_Collection.php | 49 +++++ includes/Checker/Check_Repository.php | 7 +- includes/Checker/Default_Check_Collection.php | 187 ++++++++++++++++++ includes/Checker/Default_Check_Repository.php | 43 ++-- .../Checker/Check_Categories_Tests.php | 3 +- .../Default_Check_Repository_Tests.php | 29 ++- 7 files changed, 268 insertions(+), 54 deletions(-) create mode 100644 includes/Checker/Check_Collection.php create mode 100644 includes/Checker/Default_Check_Collection.php diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index ce4b89af4..5651ad67e 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -368,7 +368,9 @@ final public function get_checks_to_run() { $check_flags = $check_flags | Check_Repository::INCLUDE_EXPERIMENTAL; } - $checks = $this->check_repository->get_checks( $check_flags, $check_slugs ); + $checks = $this->check_repository->get_checks( $check_flags ) + ->include( $check_slugs ) + ->to_map(); // Filters the checks by specific categories. $categories = $this->get_categories(); diff --git a/includes/Checker/Check_Collection.php b/includes/Checker/Check_Collection.php new file mode 100644 index 000000000..f1874f4ba --- /dev/null +++ b/includes/Checker/Check_Collection.php @@ -0,0 +1,49 @@ + $check_obj` pairs. + */ + public function to_map(): array; + + /** + * Returns a new check collection containing the subset of checks based on the given check slugs. + * + * If the given list is empty, the same collection will be returned without any change. + * + * @since n.e.x.t + * + * @return Check_Collection New check collection, effectively a subset of this one. + */ + public function include( array $check_slugs ): Check_Collection; +} diff --git a/includes/Checker/Check_Repository.php b/includes/Checker/Check_Repository.php index 1c1141093..7c757d04b 100644 --- a/includes/Checker/Check_Repository.php +++ b/includes/Checker/Check_Repository.php @@ -63,9 +63,8 @@ public function register_check( $slug, Check $check ); * * @since n.e.x.t * - * @param int $flags The check type flag. - * @param array $check_slugs An array of check slugs to return. - * @return array An indexed array of check instances. + * @param int $flags The check type flag. + * @return Check_Collection Check collection providing an indexed array of check instances. */ - public function get_checks( $flags = self::TYPE_ALL, array $check_slugs = array() ); + public function get_checks( $flags = self::TYPE_ALL ); } diff --git a/includes/Checker/Default_Check_Collection.php b/includes/Checker/Default_Check_Collection.php new file mode 100644 index 000000000..f5765fbd3 --- /dev/null +++ b/includes/Checker/Default_Check_Collection.php @@ -0,0 +1,187 @@ + $check_obj` pairs. + * + * @since n.e.x.t + * @var array + */ + private $checks; + + /** + * List of check slugs, in the same order as `$checks` - effectively the keys of that array. + * + * @since n.e.x.t + * @var array + */ + private $slugs; + + /** + * Constructor. + * + * @since n.e.x.t + * + * @param array $checks Map of `$check_slug => $check_obj` pairs for the collection. + */ + public function __construct( array $checks ) { + $this->checks = $checks; + $this->slugs = array_keys( $this->checks ); + } + + /** + * Returns the raw indexed array representation of this collection. + * + * @since n.e.x.t + * + * @return array The indexed array of check objects. + */ + public function to_array(): array { + return array_values( $this->checks ); + } + + /** + * Returns the raw map of check slugs and their check objects as a representation of this collection. + * + * @since n.e.x.t + * + * @return array Map of `$check_slug => $check_obj` pairs. + */ + public function to_map(): array { + return $this->checks; + } + + /** + * Returns a new check collection containing the subset of checks based on the given check slugs. + * + * If the given list is empty, the same collection will be returned without any change. + * + * @since n.e.x.t + * + * @return Check_Collection New check collection, effectively a subset of this one. + */ + public function include( array $check_slugs ): Check_Collection { + // Return unmodified collection if no check slugs to limit to are given. + if ( ! $check_slugs ) { + return $this; + } + + $check_slugs = array_flip( $check_slugs ); + + $checks = array(); + foreach ( $this->checks as $slug => $check ) { + if ( ! isset( $check_slugs[ $slug ] ) ) { + continue; + } + + $checks[ $slug ] = $check; + } + + return new static( $checks ); + } + + /** + * Counts the checks in the collection. + * + * @since n.e.x.t + * + * @return int Number of checks in the collection. + */ + public function count(): int { + return count( $this->checks ); + } + + /** + * Returns an iterator for the checks in the collection. + * + * @since n.e.x.t + * + * @return Traversable Checks iterator. + */ + public function getIterator(): Traversable { + return new ArrayIterator( $this->checks ); + } + + /** + * Checks whether a check exists with the given slug or index. + * + * @since n.e.x.t + * + * @param string|int $offset Either a check slug (string) or index (integer). + * @return bool True if a check exists at the given slug or index, false otherwise. + */ + public function offsetExists( $offset ) { + if ( is_string( $offset ) ) { + return isset( $this->checks[ $offset ] ); + } + + return isset( $this->slugs[ $offset ] ); + } + + /** + * Retrieves the check with the given slug or index. + * + * @since n.e.x.t + * + * @param string|int $offset Either a check slug (string) or index (integer). + * @return Check|null Check with the given slug or index, or null if it does not exist. + */ + public function offsetGet( $offset ) { + if ( is_string( $offset ) ) { + if ( isset( $this->checks[ $offset ] ) ) { + return $this->checks[ $offset ]; + } + return null; + } + + if ( isset( $this->slugs[ $offset ] ) ) { + return $this->checks[ $this->slugs[ $offset ] ]; + } + + return null; + } + + /** + * Sets a check in the collection. + * + * This method does nothing as the collection is read-only. + * + * @since n.e.x.t + * + * @param string|int $offset Either a check slug (string) or index (integer). + * @param mixed $value Value to set. + */ + public function offsetSet( $offset, $value ) { + // Not implemented as this is a read-only collection. + } + + /** + * Removes a check from the collection. + * + * This method does nothing as the collection is read-only. + * + * @since n.e.x.t + * + * @param string|int $offset Either a check slug (string) or index (integer). + */ + public function offsetUnset( $offset ) { + // Not implemented as this is a read-only collection. + } +} diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 761b0fee3..761d6f1d6 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -82,13 +82,10 @@ public function register_check( $slug, Check $check ) { * * @since n.e.x.t * - * @param int $flags The check type flag. - * @param array $check_slugs An array of check slugs to return. - * @return array An array of check instances. - * - * @throws Exception Thrown when invalid flag is passed, or Check slug does not exist. + * @param int $flags The check type flag. + * @return Check_Collection Check collection providing an indexed array of check instances. */ - public function get_checks( $flags = self::TYPE_ALL, array $check_slugs = array() ) { + public function get_checks( $flags = self::TYPE_ALL ) { $checks = array(); if ( $flags & self::TYPE_STATIC ) { @@ -99,37 +96,19 @@ public function get_checks( $flags = self::TYPE_ALL, array $check_slugs = array( $checks += $this->runtime_checks; } - // Filter out the specific check slugs requested. - if ( ! empty( $check_slugs ) ) { - $checks = array_map( - function ( $slug ) use ( $checks ) { - if ( ! isset( $checks[ $slug ] ) ) { - throw new Exception( - sprintf( - /* translators: %s: The Check slug. */ - __( 'Check with the slug "%s" does not exist.', 'plugin-check' ), - $slug - ) - ); - } - - return $checks[ $slug ]; - }, - $check_slugs - ); - } - // Return all checks, including experimental if requested. if ( $flags & self::INCLUDE_EXPERIMENTAL ) { - return $checks; + return new Default_Check_Collection( $checks ); } // Remove experimental checks before returning. - return array_filter( - $checks, - static function ( $check ) { - return $check->get_stability() !== Check::STABILITY_EXPERIMENTAL; - } + return new Default_Check_Collection( + array_filter( + $checks, + static function ( $check ) { + return $check->get_stability() !== Check::STABILITY_EXPERIMENTAL; + } + ) ); } } diff --git a/tests/phpunit/Checker/Check_Categories_Tests.php b/tests/phpunit/Checker/Check_Categories_Tests.php index a6d26035b..7399a80e6 100644 --- a/tests/phpunit/Checker/Check_Categories_Tests.php +++ b/tests/phpunit/Checker/Check_Categories_Tests.php @@ -44,7 +44,8 @@ public function test_filter_checks_by_categories( array $categories, array $all_ $this->repository->register_check( $check[0], $check[1] ); } - $checks = $this->repository->get_checks(); + $checks = $this->repository->get_checks() + ->to_map(); $check_categories = new Check_Categories(); $filtered_checks = $check_categories->filter_checks_by_categories( $checks, $categories ); diff --git a/tests/phpunit/Checker/Default_Check_Repository_Tests.php b/tests/phpunit/Checker/Default_Check_Repository_Tests.php index 8822ce622..710f940ab 100644 --- a/tests/phpunit/Checker/Default_Check_Repository_Tests.php +++ b/tests/phpunit/Checker/Default_Check_Repository_Tests.php @@ -26,14 +26,14 @@ public function test_register_static_check() { $check = new Static_Check(); $this->repository->register_check( 'static_check', $check ); - $this->assertSame( array( 'static_check' => $check ), $this->repository->get_checks() ); + $this->assertSame( array( 'static_check' => $check ), $this->repository->get_checks()->to_map() ); } public function test_register_runtime_check() { $check = new Runtime_Check(); $this->repository->register_check( 'runtime_check', $check ); - $this->assertSame( array( 'runtime_check' => $check ), $this->repository->get_checks() ); + $this->assertSame( array( 'runtime_check' => $check ), $this->repository->get_checks()->to_map() ); } public function test_register_exception_thrown_for_invalid_check() { @@ -86,7 +86,7 @@ public function test_get_checks_returns_all_checks() { 'runtime_check' => $runtime_check, ); - $this->assertSame( $expected, $this->repository->get_checks() ); + $this->assertSame( $expected, $this->repository->get_checks()->to_map() ); } public function test_get_checks_returns_static_checks_via_flag() { @@ -96,7 +96,7 @@ public function test_get_checks_returns_static_checks_via_flag() { $this->repository->register_check( 'static_check', $static_check ); $this->repository->register_check( 'runtime_check', $runtime_check ); - $this->assertSame( array( 'static_check' => $static_check ), $this->repository->get_checks( Check_Repository::TYPE_STATIC ) ); + $this->assertSame( array( 'static_check' => $static_check ), $this->repository->get_checks( Check_Repository::TYPE_STATIC )->to_map() ); } public function test_get_checks_returns_runtime_checks_via_flag() { @@ -106,7 +106,7 @@ public function test_get_checks_returns_runtime_checks_via_flag() { $this->repository->register_check( 'static_check', $static_check ); $this->repository->register_check( 'runtime_check', $runtime_check ); - $this->assertSame( array( 'runtime_check' => $runtime_check ), $this->repository->get_checks( Check_Repository::TYPE_RUNTIME ) ); + $this->assertSame( array( 'runtime_check' => $runtime_check ), $this->repository->get_checks( Check_Repository::TYPE_RUNTIME )->to_map() ); } public function test_get_checks_returns_checks_via_slug() { @@ -116,14 +116,11 @@ public function test_get_checks_returns_checks_via_slug() { $this->repository->register_check( 'static_check', $static_check ); $this->repository->register_check( 'runtime_check', $runtime_check ); - $this->assertSame( array( $static_check ), $this->repository->get_checks( Check_Repository::TYPE_ALL, array( 'static_check' ) ) ); - } - - public function test_get_checks_throws_exception_for_invalid_check_slug() { - $this->expectException( 'Exception' ); - $this->expectExceptionMessage( 'Check with the slug "invalid_check" does not exist.' ); + $checks = $this->repository->get_checks( Check_Repository::TYPE_ALL ) + ->include( array( 'static_check' ) ) + ->to_array(); - $this->repository->get_checks( Check_Repository::TYPE_ALL, array( 'invalid_check' ) ); + $this->assertSame( array( $static_check ), $checks ); } public function test_get_checks_returns_no_experimental_checks_by_default() { @@ -142,7 +139,7 @@ public function test_get_checks_returns_no_experimental_checks_by_default() { 'runtime_check' => $runtime_check, ); - $this->assertSame( $expected, $this->repository->get_checks() ); + $this->assertSame( $expected, $this->repository->get_checks()->to_map() ); } public function test_get_checks_returns_experimental_checks_with_flag() { @@ -163,7 +160,7 @@ public function test_get_checks_returns_experimental_checks_with_flag() { 'experimental_runtime_check' => $experimental_runtime_check, ); - $this->assertSame( $expected, $this->repository->get_checks( Check_Repository::TYPE_ALL | Check_Repository::INCLUDE_EXPERIMENTAL ) ); + $this->assertSame( $expected, $this->repository->get_checks( Check_Repository::TYPE_ALL | Check_Repository::INCLUDE_EXPERIMENTAL )->to_map() ); } public function test_get_checks_returns_experimental_static_checks_with_flag() { @@ -182,7 +179,7 @@ public function test_get_checks_returns_experimental_static_checks_with_flag() { 'experimental_static_check' => $experimental_static_check, ); - $this->assertSame( $expected, $this->repository->get_checks( Check_Repository::TYPE_STATIC | Check_Repository::INCLUDE_EXPERIMENTAL ) ); + $this->assertSame( $expected, $this->repository->get_checks( Check_Repository::TYPE_STATIC | Check_Repository::INCLUDE_EXPERIMENTAL )->to_map() ); } public function test_get_checks_returns_experimental_runtime_checks_with_flag() { @@ -201,6 +198,6 @@ public function test_get_checks_returns_experimental_runtime_checks_with_flag() 'experimental_runtime_check' => $experimental_runtime_check, ); - $this->assertSame( $expected, $this->repository->get_checks( Check_Repository::TYPE_RUNTIME | Check_Repository::INCLUDE_EXPERIMENTAL ) ); + $this->assertSame( $expected, $this->repository->get_checks( Check_Repository::TYPE_RUNTIME | Check_Repository::INCLUDE_EXPERIMENTAL )->to_map() ); } } From 3d193b68ed5c7416d94530c5181578c3fa05cb20 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 31 Oct 2023 13:25:41 -0500 Subject: [PATCH 2/8] Fix PHPCS violations. --- includes/Checker/Check_Collection.php | 1 + includes/Checker/Default_Check_Collection.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/Checker/Check_Collection.php b/includes/Checker/Check_Collection.php index f1874f4ba..78398e598 100644 --- a/includes/Checker/Check_Collection.php +++ b/includes/Checker/Check_Collection.php @@ -43,6 +43,7 @@ public function to_map(): array; * * @since n.e.x.t * + * @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned. * @return Check_Collection New check collection, effectively a subset of this one. */ public function include( array $check_slugs ): Check_Collection; diff --git a/includes/Checker/Default_Check_Collection.php b/includes/Checker/Default_Check_Collection.php index f5765fbd3..b72ad0c6f 100644 --- a/includes/Checker/Default_Check_Collection.php +++ b/includes/Checker/Default_Check_Collection.php @@ -8,7 +8,6 @@ namespace WordPress\Plugin_Check\Checker; use ArrayIterator; -use Exception; use Traversable; /** @@ -75,6 +74,7 @@ public function to_map(): array { * * @since n.e.x.t * + * @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned. * @return Check_Collection New check collection, effectively a subset of this one. */ public function include( array $check_slugs ): Check_Collection { From 6353199554735af2e8aff9b86154c5e03254d254 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 31 Oct 2023 13:28:46 -0500 Subject: [PATCH 3/8] Fix PHPStan violation. --- includes/Checker/Default_Check_Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Checker/Default_Check_Collection.php b/includes/Checker/Default_Check_Collection.php index b72ad0c6f..8ff2ebef9 100644 --- a/includes/Checker/Default_Check_Collection.php +++ b/includes/Checker/Default_Check_Collection.php @@ -94,7 +94,7 @@ public function include( array $check_slugs ): Check_Collection { $checks[ $slug ] = $check; } - return new static( $checks ); + return new self( $checks ); } /** From 6b9df7fbe80fca772eb13737a29e9a43a750e206 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 2 Nov 2023 15:21:44 -0700 Subject: [PATCH 4/8] Implement filter method on check collection. --- includes/Checker/Default_Check_Collection.php | 18 ++++++++++++++++++ includes/Checker/Default_Check_Repository.php | 11 ++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/includes/Checker/Default_Check_Collection.php b/includes/Checker/Default_Check_Collection.php index 8ff2ebef9..1c29fddd7 100644 --- a/includes/Checker/Default_Check_Collection.php +++ b/includes/Checker/Default_Check_Collection.php @@ -67,6 +67,24 @@ public function to_map(): array { return $this->checks; } + /** + * Returns a new check collection containing the subset of checks based on the given check filter function. + * + * @since n.e.x.t + * + * @param callable $filter_fn Filter function that accepts a single check object and should return a boolean for + * whether to include the check in the new collection. + * @return Check_Collection New check collection, effectively a subset of this one. + */ + public function filter( callable $filter_fn ): Check_Collection { + return new self( + array_filter( + $this->checks, + $filter_fn + ) + ); + } + /** * Returns a new check collection containing the subset of checks based on the given check slugs. * diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index 761d6f1d6..aa1670c14 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -102,13 +102,10 @@ public function get_checks( $flags = self::TYPE_ALL ) { } // Remove experimental checks before returning. - return new Default_Check_Collection( - array_filter( - $checks, - static function ( $check ) { - return $check->get_stability() !== Check::STABILITY_EXPERIMENTAL; - } - ) + return ( new Default_Check_Collection( $checks ) )->filter( + static function ( $check ) { + return $check->get_stability() !== Check::STABILITY_EXPERIMENTAL; + } ); } } From 62d98c6f7305fdd2dd229d2a2e5a1ce1c776e464 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 3 Nov 2023 11:07:55 -0700 Subject: [PATCH 5/8] Introduce Check_Collection::require() method to bring back the functionality to fail if an invalid check slug is given. --- includes/Checker/Abstract_Check_Runner.php | 3 ++- includes/Checker/Check_Collection.php | 24 +++++++++++++++++ includes/Checker/Default_Check_Collection.php | 27 +++++++++++++++++++ .../Default_Check_Repository_Tests.php | 8 ++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 5651ad67e..59138e1bd 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -369,7 +369,8 @@ final public function get_checks_to_run() { } $checks = $this->check_repository->get_checks( $check_flags ) - ->include( $check_slugs ) + ->require( $check_slugs ) // Ensures all of the given slugs are valid. + ->include( $check_slugs ) // Ensures only the checks with the given slugs are included. ->to_map(); // Filters the checks by specific categories. diff --git a/includes/Checker/Check_Collection.php b/includes/Checker/Check_Collection.php index 78398e598..c5698cbe3 100644 --- a/includes/Checker/Check_Collection.php +++ b/includes/Checker/Check_Collection.php @@ -9,6 +9,7 @@ use ArrayAccess; use Countable; +use Exception; use IteratorAggregate; /** @@ -36,6 +37,17 @@ public function to_array(): array; */ public function to_map(): array; + /** + * Returns a new check collection containing the subset of checks based on the given check filter function. + * + * @since n.e.x.t + * + * @param callable $filter_fn Filter function that accepts a single check object and should return a boolean for + * whether to include the check in the new collection. + * @return Check_Collection New check collection, effectively a subset of this one. + */ + public function filter( callable $filter_fn ): Check_Collection; + /** * Returns a new check collection containing the subset of checks based on the given check slugs. * @@ -47,4 +59,16 @@ public function to_map(): array; * @return Check_Collection New check collection, effectively a subset of this one. */ public function include( array $check_slugs ): Check_Collection; + + /** + * Throws an exception if any of the given check slugs are not present, or returns the same collection otherwise. + * + * @since n.e.x.t + * + * @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned. + * @return Check_Collection The unchanged check collection. + * + * @throws Exception Thrown when any of the given check slugs is not present in the collection. + */ + public function require( array $check_slugs ): Check_Collection; } diff --git a/includes/Checker/Default_Check_Collection.php b/includes/Checker/Default_Check_Collection.php index 1c29fddd7..de81e85b9 100644 --- a/includes/Checker/Default_Check_Collection.php +++ b/includes/Checker/Default_Check_Collection.php @@ -8,6 +8,7 @@ namespace WordPress\Plugin_Check\Checker; use ArrayIterator; +use Exception; use Traversable; /** @@ -115,6 +116,32 @@ public function include( array $check_slugs ): Check_Collection { return new self( $checks ); } + /** + * Throws an exception if any of the given check slugs are not present, or returns the same collection otherwise. + * + * @since n.e.x.t + * + * @param array $check_slugs List of slugs to limit to only those. If empty, the same collection is returned. + * @return Check_Collection The unchanged check collection. + * + * @throws Exception Thrown when any of the given check slugs is not present in the collection. + */ + public function require( array $check_slugs ): Check_Collection { + foreach ( $check_slugs as $slug ) { + if ( ! isset( $this->checks[ $slug ] ) ) { + throw new Exception( + sprintf( + /* translators: %s: The Check slug. */ + __( 'Check with the slug "%s" does not exist.', 'plugin-check' ), + $slug + ) + ); + } + } + + return $this; + } + /** * Counts the checks in the collection. * diff --git a/tests/phpunit/Checker/Default_Check_Repository_Tests.php b/tests/phpunit/Checker/Default_Check_Repository_Tests.php index 710f940ab..b2b1ddfb6 100644 --- a/tests/phpunit/Checker/Default_Check_Repository_Tests.php +++ b/tests/phpunit/Checker/Default_Check_Repository_Tests.php @@ -123,6 +123,14 @@ public function test_get_checks_returns_checks_via_slug() { $this->assertSame( array( $static_check ), $checks ); } + public function test_get_checks_throws_exception_for_invalid_check_slug() { + $this->expectException( 'Exception' ); + $this->expectExceptionMessage( 'Check with the slug "invalid_check" does not exist.' ); + + $this->repository->get_checks( Check_Repository::TYPE_ALL ) + ->require( array( 'invalid_check' ) ); + } + public function test_get_checks_returns_no_experimental_checks_by_default() { $static_check = new Static_Check(); $runtime_check = new Runtime_Check(); From 541a16f220e2463e1f772ca6ab8297287fbdff3b Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 3 Nov 2023 11:24:40 -0700 Subject: [PATCH 6/8] Implement proper test coverage for Check_Collection. --- .../Default_Check_Collection_Tests.php | 93 +++++++++++++++++++ .../Default_Check_Repository_Tests.php | 24 +---- 2 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 tests/phpunit/Checker/Default_Check_Collection_Tests.php diff --git a/tests/phpunit/Checker/Default_Check_Collection_Tests.php b/tests/phpunit/Checker/Default_Check_Collection_Tests.php new file mode 100644 index 000000000..152e27370 --- /dev/null +++ b/tests/phpunit/Checker/Default_Check_Collection_Tests.php @@ -0,0 +1,93 @@ +checks = array( + 'static_check' => new Static_Check(), + 'runtime_check' => new Runtime_Check(), + ); + + $repository = new Default_Check_Repository(); + foreach ( $this->checks as $slug => $check ) { + $repository->register_check( $slug, $check ); + } + + $this->collection = $repository->get_checks(); + } + + public function test_to_array() { + $this->assertSame( + array_values( $this->checks ), + $this->collection->to_array() + ); + } + + public function test_to_map() { + $this->assertSame( + $this->checks, + $this->collection->to_map() + ); + } + + public function test_filter() { + $this->assertSame( + array( $this->checks['runtime_check'] ), + $this->collection->filter( + function ( $check ) { + return $check instanceof Runtime_Check_Interface; + } + )->to_array() + ); + } + + public function test_include() { + $this->assertSame( + array( $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->collection->include( array() )->to_array() + ); + } + + public function test_include_with_invalid() { + $this->assertSame( + array( $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->collection->require( array( 'static_check' ) )->to_array() + ); + } + + public function test_require_with_invalid() { + $this->expectException( 'Exception' ); + $this->expectExceptionMessage( 'Check with the slug "invalid_check" does not exist.' ); + + $this->collection->require( array( 'static_check', 'invalid_check' ) ); + } +} diff --git a/tests/phpunit/Checker/Default_Check_Repository_Tests.php b/tests/phpunit/Checker/Default_Check_Repository_Tests.php index b2b1ddfb6..b1a8898d9 100644 --- a/tests/phpunit/Checker/Default_Check_Repository_Tests.php +++ b/tests/phpunit/Checker/Default_Check_Repository_Tests.php @@ -16,6 +16,8 @@ class Default_Check_Repository_Tests extends WP_UnitTestCase { + private $repository; + public function set_up() { parent::set_up(); @@ -109,28 +111,6 @@ public function test_get_checks_returns_runtime_checks_via_flag() { $this->assertSame( array( 'runtime_check' => $runtime_check ), $this->repository->get_checks( Check_Repository::TYPE_RUNTIME )->to_map() ); } - public function test_get_checks_returns_checks_via_slug() { - $static_check = new Static_Check(); - $runtime_check = new Runtime_Check(); - - $this->repository->register_check( 'static_check', $static_check ); - $this->repository->register_check( 'runtime_check', $runtime_check ); - - $checks = $this->repository->get_checks( Check_Repository::TYPE_ALL ) - ->include( array( 'static_check' ) ) - ->to_array(); - - $this->assertSame( array( $static_check ), $checks ); - } - - public function test_get_checks_throws_exception_for_invalid_check_slug() { - $this->expectException( 'Exception' ); - $this->expectExceptionMessage( 'Check with the slug "invalid_check" does not exist.' ); - - $this->repository->get_checks( Check_Repository::TYPE_ALL ) - ->require( array( 'invalid_check' ) ); - } - public function test_get_checks_returns_no_experimental_checks_by_default() { $static_check = new Static_Check(); $runtime_check = new Runtime_Check(); From 99bf7a5c0db12950323ad01cdf50c5d1e5fc7cde Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 3 Nov 2023 11:34:47 -0700 Subject: [PATCH 7/8] Update phpmd.xml to ignore public methods that are for special interfaces. --- phpmd.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpmd.xml b/phpmd.xml index 0dc175f0d..1f07359e3 100644 --- a/phpmd.xml +++ b/phpmd.xml @@ -19,6 +19,12 @@ + + + + + + From edbf4d27f2c03b3b879e36693467a909dc9b94c3 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 3 Nov 2023 11:44:57 -0700 Subject: [PATCH 8/8] Update phpmd.xml to ignore public methods that are for special interfaces. --- phpmd.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpmd.xml b/phpmd.xml index 1f07359e3..6e6f9cc61 100644 --- a/phpmd.xml +++ b/phpmd.xml @@ -11,6 +11,7 @@ + @@ -21,7 +22,7 @@ - +