Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUGFIX: Collection Filter Iterators #34

Merged
merged 10 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
files/
repo/
vendor/
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Tables/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
20 changes: 14 additions & 6 deletions src/Schema/Tables/Filters/Group_FilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,34 @@

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.
*
* @since 1.0.0
*
* @var array<string>
*/
private $groups = [];
private $groups;

/**
* Constructor.
*
* @since 1.0.0
*
* @param array<string> $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 = (array) $groups;
$this->groups = $groups;
}

/**
Expand All @@ -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();
} ) );
}
}
10 changes: 8 additions & 2 deletions src/Schema/Tables/Filters/Needs_Update_FilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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();
} ) );
}
}
7 changes: 3 additions & 4 deletions tests/_support/Helper/SchemaTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
31 changes: 31 additions & 0 deletions tests/_support/Traits/Table_Fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*/
Expand Down
3 changes: 1 addition & 2 deletions tests/wpunit/BuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace StellarWP\Schema\Tests\Builder;
namespace StellarWP\Schema\Tests;

use StellarWP\Schema\Tables\Contracts\Schema_Interface as Table_Schema_Interface;
use StellarWP\Schema\Register;
use StellarWP\Schema\Schema;
use StellarWP\Schema\Tests\SchemaTestCase;
use StellarWP\Schema\Tests\Traits\Table_Fixtures;

class BuilderTest extends SchemaTestCase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php

namespace StellarWP\Schema\Tests;

use lucatume\DI52\App;
use StellarWP\Schema\Builder;
use StellarWP\Schema\Config;
use StellarWP\Schema\Register;
use StellarWP\Schema\Tests\SchemaTestCase;
use StellarWP\Schema\Tests\Traits;

class Full_ActivationTest extends SchemaTestCase {
class FullActivationTest extends SchemaTestCase {
use Traits\Table_Fixtures;

/**
Expand Down
61 changes: 60 additions & 1 deletion tests/wpunit/RegisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

namespace StellarWP\Schema\Tests;

use lucatume\DI52\App;
use StellarWP\Schema\Config;
use StellarWP\Schema\Fields;
use StellarWP\Schema\Register;
use StellarWP\Schema\Schema;
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
*
Expand Down Expand Up @@ -74,6 +83,56 @@ 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() );
}

/**
* @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.
*
Expand Down
Loading