From eefb8e07ae7ef9a8a63d9275c564fc00ade147be Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 12:12:23 -0600 Subject: [PATCH 01/10] ignore phpstorm files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 17a68b2..3890cfe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea/ files/ repo/ vendor/ From cc7472c1c3f26dd65c9fa825265bda32194b4b6b Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 12:13:48 -0600 Subject: [PATCH 02/10] Fix tests psr paths --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3298e67..e6d2f47 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,10 @@ }, "autoload-dev": { "psr-4": { - "StellarWP\\Schema\\Tests\\": "tests/_support/Helper/", + "StellarWP\\Schema\\Tests\\": [ + "tests/_support/Helper/", + "tests/wpunit/" + ], "StellarWP\\Schema\\Tests\\Traits\\": "tests/_support/Traits/" } }, From 571531b4ef536132a6e0c03d347f8859f3d19fcd Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 12:24:51 -0600 Subject: [PATCH 03/10] Standardize tests, clean up code --- tests/_support/Helper/SchemaTestCase.php | 7 +++---- tests/wpunit/BuilderTest.php | 3 +-- .../{Full_ActivationTest.php => FullActivationTest.php} | 6 ++---- tests/wpunit/RegisterTest.php | 1 - 4 files changed, 6 insertions(+), 11 deletions(-) rename tests/wpunit/{Full_ActivationTest.php => FullActivationTest.php} (83%) diff --git a/tests/_support/Helper/SchemaTestCase.php b/tests/_support/Helper/SchemaTestCase.php index 7b87321..fa1c6ed 100644 --- a/tests/_support/Helper/SchemaTestCase.php +++ b/tests/_support/Helper/SchemaTestCase.php @@ -2,16 +2,15 @@ namespace StellarWP\Schema\Tests; -use lucatume\DI52\App; -use StellarWP\Schema\Tests\Container; +use Codeception\Test\Unit; use StellarWP\Schema\Config; use StellarWP\DB\DB; use StellarWP\Schema\Schema; -class SchemaTestCase extends \Codeception\Test\Unit { +class SchemaTestCase extends Unit { protected $backupGlobals = false; - public function setUp() { + protected function setUp() { // before parent::setUp(); diff --git a/tests/wpunit/BuilderTest.php b/tests/wpunit/BuilderTest.php index 302d32f..d38ed6d 100644 --- a/tests/wpunit/BuilderTest.php +++ b/tests/wpunit/BuilderTest.php @@ -1,11 +1,10 @@ Date: Wed, 5 Jun 2024 12:31:40 -0600 Subject: [PATCH 04/10] Add failing get tables by group test --- tests/wpunit/Tables/CollectionTest.php | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/wpunit/Tables/CollectionTest.php diff --git a/tests/wpunit/Tables/CollectionTest.php b/tests/wpunit/Tables/CollectionTest.php new file mode 100644 index 0000000..10099ee --- /dev/null +++ b/tests/wpunit/Tables/CollectionTest.php @@ -0,0 +1,52 @@ +get_simple_table()->drop(); + $this->get_indexless_table()->drop(); + + Register::tables([ + $this->get_simple_table(), + $this->get_indexless_table(), + ]); + + $this->collection = Config::get_container()->get( Collection::class ); + } + + public function test_it_has_tables_in_collection() { + $this->assertSame( 2, $this->collection->count() ); + } + + public function test_it_fetches_table_by_base_table_name() { + $name = $this->get_simple_table()::base_table_name(); + $table = $this->collection->get( $name ); + + $this->assertSame( $name, $table::base_table_name() ); + } + + public function test_it_gets_tables_by_group() { + $tables = $this->collection->get_by_group( 'bork' ); + + $this->assertSame( 2, $tables->count() ); + } + + +} From 112c9be4caf94d0289797213596bee3453b5ff98 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 12:32:20 -0600 Subject: [PATCH 05/10] Make tests pass --- src/Schema/Tables/Collection.php | 2 +- src/Schema/Tables/Filters/Group_FilterIterator.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/Tables/Collection.php b/src/Schema/Tables/Collection.php index 3d85712..1f04fea 100644 --- a/src/Schema/Tables/Collection.php +++ b/src/Schema/Tables/Collection.php @@ -75,7 +75,7 @@ public function get( string $key ): Schema_Interface { * @return Filters\Group_FilterIterator */ public function get_by_group( $groups, $iterator = null ): Filters\Group_FilterIterator { - return new Filters\Group_FilterIterator( $groups, $iterator ?: $this ); + return new Filters\Group_FilterIterator( (array) $groups, $iterator ?: $this ); } /** diff --git a/src/Schema/Tables/Filters/Group_FilterIterator.php b/src/Schema/Tables/Filters/Group_FilterIterator.php index f6bfbf2..56d1c58 100644 --- a/src/Schema/Tables/Filters/Group_FilterIterator.php +++ b/src/Schema/Tables/Filters/Group_FilterIterator.php @@ -23,7 +23,7 @@ class Group_FilterIterator extends \FilterIterator implements \Countable { public function __construct( array $groups, \Iterator $iterator ) { parent::__construct( $iterator ); - $this->groups = (array) $groups; + $this->groups = $groups; } /** From cf1e7533f63632cc5c6be9e64941d4f67984bbbe Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 12:51:00 -0600 Subject: [PATCH 06/10] Fix missing use statement, add a new table with a different group --- tests/_support/Traits/Table_Fixtures.php | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/_support/Traits/Table_Fixtures.php b/tests/_support/Traits/Table_Fixtures.php index 06faf18..7108c96 100644 --- a/tests/_support/Traits/Table_Fixtures.php +++ b/tests/_support/Traits/Table_Fixtures.php @@ -4,6 +4,7 @@ use StellarWP\Schema\Activation; use StellarWP\Schema\Builder; +use StellarWP\Schema\Config; use StellarWP\Schema\Fields\Contracts\Field; use StellarWP\Schema\Tables\Contracts\Table; @@ -109,6 +110,36 @@ protected function get_definition() { return $table; } + /** + * Get a fake table to verify its creation. + */ + public function get_simple_table_alt_group(): Table { + return new class extends Table { + const SCHEMA_VERSION = '1.0.0'; + + protected static $base_table_name = 'simple-alt'; + protected static $group = 'test'; + protected static $schema_slug = 'bork-simple-alt'; + protected static $uid_column = 'id'; + + protected function get_definition(): string { + global $wpdb; + $table_name = self::table_name(); + $charset_collate = $wpdb->get_charset_collate(); + + return " + CREATE TABLE `$table_name` ( + `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, + `name` varchar(25) NOT NULL, + `slug` varchar(25) NOT NULL, + PRIMARY KEY (`id`), + KEY `slug` (`slug`) + ) $charset_collate; + "; + } + }; + } + /** * Get a fake table to verify its creation. */ From 2c34843d65aded06cf8ee00d3974ebf099fc482e Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 12:52:12 -0600 Subject: [PATCH 07/10] Remove CollectionTest.php, move to RegisterTest.php and fix count bug --- .../Tables/Filters/Group_FilterIterator.php | 18 +++++-- tests/wpunit/RegisterTest.php | 40 ++++++++++++++ tests/wpunit/Tables/CollectionTest.php | 52 ------------------- 3 files changed, 53 insertions(+), 57 deletions(-) delete mode 100644 tests/wpunit/Tables/CollectionTest.php diff --git a/src/Schema/Tables/Filters/Group_FilterIterator.php b/src/Schema/Tables/Filters/Group_FilterIterator.php index 56d1c58..b987fa3 100644 --- a/src/Schema/Tables/Filters/Group_FilterIterator.php +++ b/src/Schema/Tables/Filters/Group_FilterIterator.php @@ -2,7 +2,13 @@ namespace StellarWP\Schema\Tables\Filters; -class Group_FilterIterator extends \FilterIterator implements \Countable { +use CallbackFilterIterator; +use Countable; +use FilterIterator; +use Iterator; + +class Group_FilterIterator extends FilterIterator implements Countable { + /** * Groups to filter. * @@ -10,7 +16,7 @@ class Group_FilterIterator extends \FilterIterator implements \Countable { * * @var array */ - private $groups = []; + private $groups; /** * Constructor. @@ -18,9 +24,9 @@ class Group_FilterIterator extends \FilterIterator implements \Countable { * @since 1.0.0 * * @param array $groups Paths to filter. - * @param \Iterator $iterator Iterator to filter. + * @param Iterator $iterator Iterator to filter. */ - public function __construct( array $groups, \Iterator $iterator ) { + public function __construct( array $groups, Iterator $iterator ) { parent::__construct( $iterator ); $this->groups = $groups; @@ -39,6 +45,8 @@ public function accept(): bool { * @inheritDoc */ public function count(): int { - return iterator_count( $this->getInnerIterator() ); + return iterator_count( new CallbackFilterIterator( $this->getInnerIterator(), function (): bool { + return $this->accept(); + } ) ); } } diff --git a/tests/wpunit/RegisterTest.php b/tests/wpunit/RegisterTest.php index c0d9dc2..c92fb58 100644 --- a/tests/wpunit/RegisterTest.php +++ b/tests/wpunit/RegisterTest.php @@ -9,8 +9,18 @@ use StellarWP\Schema\Tests\Traits\Table_Fixtures; class RegisterTest extends SchemaTestCase { + use Table_Fixtures; + /** + * @before + */ + public function drop_tables() { + $this->get_simple_table()->drop(); + $this->get_simple_table_alt_group()->drop(); + $this->get_foreign_key_table()->drop(); + } + /** * Registered fields should exist in the collection * @@ -73,6 +83,36 @@ public function it_should_have_tables_in_collection_when_batch_added() { $this->assertArrayHasKey( $table_2::base_table_name(), Schema::tables() ); } + /** + * @test + */ + public function it_should_allow_fetching_tables_by_single_group() { + Register::tables( [ + $this->get_simple_table(), + $this->get_simple_table_alt_group(), + $this->get_indexless_table(), + ] ); + + $tables = Schema::tables()->get_by_group( 'bork' ); + + $this->assertSame( 2, $tables->count() ); + } + + /** + * @test + */ + public function it_should_allow_fetching_tables_by_multiple_groups() { + Register::tables( [ + $this->get_simple_table(), + $this->get_simple_table_alt_group(), + $this->get_indexless_table(), + ] ); + + $tables = Schema::tables()->get_by_group( [ 'bork', 'test' ] ); + + $this->assertSame( 3, $tables->count() ); + } + /** * Registered tables should be removed from the collection. * diff --git a/tests/wpunit/Tables/CollectionTest.php b/tests/wpunit/Tables/CollectionTest.php deleted file mode 100644 index 10099ee..0000000 --- a/tests/wpunit/Tables/CollectionTest.php +++ /dev/null @@ -1,52 +0,0 @@ -get_simple_table()->drop(); - $this->get_indexless_table()->drop(); - - Register::tables([ - $this->get_simple_table(), - $this->get_indexless_table(), - ]); - - $this->collection = Config::get_container()->get( Collection::class ); - } - - public function test_it_has_tables_in_collection() { - $this->assertSame( 2, $this->collection->count() ); - } - - public function test_it_fetches_table_by_base_table_name() { - $name = $this->get_simple_table()::base_table_name(); - $table = $this->collection->get( $name ); - - $this->assertSame( $name, $table::base_table_name() ); - } - - public function test_it_gets_tables_by_group() { - $tables = $this->collection->get_by_group( 'bork' ); - - $this->assertSame( 2, $tables->count() ); - } - - -} From 4004e88ef65618fd4a87953de58e0d3f01ec4ddd Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 12:59:59 -0600 Subject: [PATCH 08/10] Add failing test for bad count on needs update --- tests/wpunit/RegisterTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/wpunit/RegisterTest.php b/tests/wpunit/RegisterTest.php index c92fb58..b2655a4 100644 --- a/tests/wpunit/RegisterTest.php +++ b/tests/wpunit/RegisterTest.php @@ -113,6 +113,26 @@ public function it_should_allow_fetching_tables_by_multiple_groups() { $this->assertSame( 3, $tables->count() ); } + /** + * @test + */ + public function it_should_allow_fetching_of_tables_that_need_updates() { + Register::tables([ + $this->get_simple_table(), + $this->get_indexless_table(), + ]); + + $schema_tables = Schema::tables(); + + $tables = $schema_tables->get_tables_needing_updates(); + + $this->assertSame( 0, $tables->count() ); + + $schema_tables->add( $this->get_modified_simple_table() ); + + $this->assertSame( 1, $tables->count() ); + } + /** * Registered tables should be removed from the collection. * From 46483b370cda5c47a040b912752114d27f527210 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 13:00:13 -0600 Subject: [PATCH 09/10] Fix test --- .../Tables/Filters/Needs_Update_FilterIterator.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Schema/Tables/Filters/Needs_Update_FilterIterator.php b/src/Schema/Tables/Filters/Needs_Update_FilterIterator.php index 942209f..0373bc3 100644 --- a/src/Schema/Tables/Filters/Needs_Update_FilterIterator.php +++ b/src/Schema/Tables/Filters/Needs_Update_FilterIterator.php @@ -2,7 +2,11 @@ namespace StellarWP\Schema\Tables\Filters; -class Needs_Update_FilterIterator extends \FilterIterator implements \Countable { +use CallbackFilterIterator; +use Countable; +use FilterIterator; + +class Needs_Update_FilterIterator extends FilterIterator implements Countable { /** * @inheritDoc */ @@ -16,6 +20,8 @@ public function accept(): bool { * @inheritDoc */ public function count(): int { - return iterator_count( $this->getInnerIterator() ); + return iterator_count( new CallbackFilterIterator( $this->getInnerIterator(), function (): bool { + return $this->accept(); + } ) ); } } From 0b8b9b418fc6f7862fa060cc7060f26463238443 Mon Sep 17 00:00:00 2001 From: Justin Frydman Date: Wed, 5 Jun 2024 13:10:53 -0600 Subject: [PATCH 10/10] Update CHANGELOG.md --- CHANGELOG.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ebbf19..82901fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file. This projec ## [unreleased] Unreleased + +## [1.1.7] 2024-06-05 + +* Fix - `Collection::get_by_group()` now properly works with a single string group name. +* Fix - `Group_FilterIterator::count()` now properly returns the filtered count and not the base iterator count. +* Fix - `Needs_Update_FilterIterator::count()` now properly returns the filtered count and not the base iterator count. +* Fix - Use proper PSR namespacing for tests. +* Tests - code clean up and file name standardization. + +## [1.1.3] 2023-04-04 + * Feature - Added `Table::has_foreign_key()` method. ## [1.1.2] 2022-11-2