Skip to content

Commit 4dc4939

Browse files
authored
Merge pull request #34 from stellarwp/bugfix/table-collection-type-errors
BUGFIX: Collection Filter Iterators
2 parents afef730 + 0b8b9b4 commit 4dc4939

File tree

11 files changed

+136
-21
lines changed

11 files changed

+136
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea/
12
files/
23
repo/
34
vendor/

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file. This projec
44

55
## [unreleased] Unreleased
66

7+
8+
## [1.1.7] 2024-06-05
9+
10+
* Fix - `Collection::get_by_group()` now properly works with a single string group name.
11+
* Fix - `Group_FilterIterator::count()` now properly returns the filtered count and not the base iterator count.
12+
* Fix - `Needs_Update_FilterIterator::count()` now properly returns the filtered count and not the base iterator count.
13+
* Fix - Use proper PSR namespacing for tests.
14+
* Tests - code clean up and file name standardization.
15+
16+
## [1.1.3] 2023-04-04
17+
718
* Feature - Added `Table::has_foreign_key()` method.
819

920
## [1.1.2] 2022-11-2

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
},
4040
"autoload-dev": {
4141
"psr-4": {
42-
"StellarWP\\Schema\\Tests\\": "tests/_support/Helper/",
42+
"StellarWP\\Schema\\Tests\\": [
43+
"tests/_support/Helper/",
44+
"tests/wpunit/"
45+
],
4346
"StellarWP\\Schema\\Tests\\Traits\\": "tests/_support/Traits/"
4447
}
4548
},

src/Schema/Tables/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function get( string $key ): Schema_Interface {
7575
* @return Filters\Group_FilterIterator
7676
*/
7777
public function get_by_group( $groups, $iterator = null ): Filters\Group_FilterIterator {
78-
return new Filters\Group_FilterIterator( $groups, $iterator ?: $this );
78+
return new Filters\Group_FilterIterator( (array) $groups, $iterator ?: $this );
7979
}
8080

8181
/**

src/Schema/Tables/Filters/Group_FilterIterator.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,34 @@
22

33
namespace StellarWP\Schema\Tables\Filters;
44

5-
class Group_FilterIterator extends \FilterIterator implements \Countable {
5+
use CallbackFilterIterator;
6+
use Countable;
7+
use FilterIterator;
8+
use Iterator;
9+
10+
class Group_FilterIterator extends FilterIterator implements Countable {
11+
612
/**
713
* Groups to filter.
814
*
915
* @since 1.0.0
1016
*
1117
* @var array<string>
1218
*/
13-
private $groups = [];
19+
private $groups;
1420

1521
/**
1622
* Constructor.
1723
*
1824
* @since 1.0.0
1925
*
2026
* @param array<string> $groups Paths to filter.
21-
* @param \Iterator $iterator Iterator to filter.
27+
* @param Iterator $iterator Iterator to filter.
2228
*/
23-
public function __construct( array $groups, \Iterator $iterator ) {
29+
public function __construct( array $groups, Iterator $iterator ) {
2430
parent::__construct( $iterator );
2531

26-
$this->groups = (array) $groups;
32+
$this->groups = $groups;
2733
}
2834

2935
/**
@@ -39,6 +45,8 @@ public function accept(): bool {
3945
* @inheritDoc
4046
*/
4147
public function count(): int {
42-
return iterator_count( $this->getInnerIterator() );
48+
return iterator_count( new CallbackFilterIterator( $this->getInnerIterator(), function (): bool {
49+
return $this->accept();
50+
} ) );
4351
}
4452
}

src/Schema/Tables/Filters/Needs_Update_FilterIterator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace StellarWP\Schema\Tables\Filters;
44

5-
class Needs_Update_FilterIterator extends \FilterIterator implements \Countable {
5+
use CallbackFilterIterator;
6+
use Countable;
7+
use FilterIterator;
8+
9+
class Needs_Update_FilterIterator extends FilterIterator implements Countable {
610
/**
711
* @inheritDoc
812
*/
@@ -16,6 +20,8 @@ public function accept(): bool {
1620
* @inheritDoc
1721
*/
1822
public function count(): int {
19-
return iterator_count( $this->getInnerIterator() );
23+
return iterator_count( new CallbackFilterIterator( $this->getInnerIterator(), function (): bool {
24+
return $this->accept();
25+
} ) );
2026
}
2127
}

tests/_support/Helper/SchemaTestCase.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
namespace StellarWP\Schema\Tests;
44

5-
use lucatume\DI52\App;
6-
use StellarWP\Schema\Tests\Container;
5+
use Codeception\Test\Unit;
76
use StellarWP\Schema\Config;
87
use StellarWP\DB\DB;
98
use StellarWP\Schema\Schema;
109

11-
class SchemaTestCase extends \Codeception\Test\Unit {
10+
class SchemaTestCase extends Unit {
1211
protected $backupGlobals = false;
1312

14-
public function setUp() {
13+
protected function setUp() {
1514
// before
1615
parent::setUp();
1716

tests/_support/Traits/Table_Fixtures.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use StellarWP\Schema\Activation;
66
use StellarWP\Schema\Builder;
7+
use StellarWP\Schema\Config;
78
use StellarWP\Schema\Fields\Contracts\Field;
89
use StellarWP\Schema\Tables\Contracts\Table;
910

@@ -109,6 +110,36 @@ protected function get_definition() {
109110
return $table;
110111
}
111112

113+
/**
114+
* Get a fake table to verify its creation.
115+
*/
116+
public function get_simple_table_alt_group(): Table {
117+
return new class extends Table {
118+
const SCHEMA_VERSION = '1.0.0';
119+
120+
protected static $base_table_name = 'simple-alt';
121+
protected static $group = 'test';
122+
protected static $schema_slug = 'bork-simple-alt';
123+
protected static $uid_column = 'id';
124+
125+
protected function get_definition(): string {
126+
global $wpdb;
127+
$table_name = self::table_name();
128+
$charset_collate = $wpdb->get_charset_collate();
129+
130+
return "
131+
CREATE TABLE `$table_name` (
132+
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
133+
`name` varchar(25) NOT NULL,
134+
`slug` varchar(25) NOT NULL,
135+
PRIMARY KEY (`id`),
136+
KEY `slug` (`slug`)
137+
) $charset_collate;
138+
";
139+
}
140+
};
141+
}
142+
112143
/**
113144
* Get a fake table to verify its creation.
114145
*/

tests/wpunit/BuilderTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22

3-
namespace StellarWP\Schema\Tests\Builder;
3+
namespace StellarWP\Schema\Tests;
44

55
use StellarWP\Schema\Tables\Contracts\Schema_Interface as Table_Schema_Interface;
66
use StellarWP\Schema\Register;
77
use StellarWP\Schema\Schema;
8-
use StellarWP\Schema\Tests\SchemaTestCase;
98
use StellarWP\Schema\Tests\Traits\Table_Fixtures;
109

1110
class BuilderTest extends SchemaTestCase {
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
2+
23
namespace StellarWP\Schema\Tests;
34

4-
use lucatume\DI52\App;
55
use StellarWP\Schema\Builder;
66
use StellarWP\Schema\Config;
77
use StellarWP\Schema\Register;
8-
use StellarWP\Schema\Tests\SchemaTestCase;
9-
use StellarWP\Schema\Tests\Traits;
108

11-
class Full_ActivationTest extends SchemaTestCase {
9+
class FullActivationTest extends SchemaTestCase {
1210
use Traits\Table_Fixtures;
1311

1412
/**

0 commit comments

Comments
 (0)