From 1e3e64674d9ac3cc028aa5e9e18ccbac852a1f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:19:15 +0100 Subject: [PATCH 1/7] Update to Cake 5 --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index fbbc832..63ba1cf 100644 --- a/composer.json +++ b/composer.json @@ -5,12 +5,12 @@ "type": "cakephp-plugin", "license": "MIT", "require": { - "cakephp/orm": "~4.0" + "cakephp/orm": "~5.0" }, "require-dev": { - "cakephp/cakephp": "~4.0", - "cakephp/cakephp-codesniffer": "^4.0", - "phpunit/phpunit": "^9.5", + "cakephp/cakephp": "~5.0", + "cakephp/cakephp-codesniffer": "^5.0", + "phpunit/phpunit": "^10.0", "phpstan/phpstan": "^2.0" }, "autoload": { From 6d6fa8be8941cb8c5842bb71933e26be7f459b7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:19:25 +0100 Subject: [PATCH 2/7] Setup test env --- .gitignore | 2 +- phpunit.xml | 17 +++++++++++++++++ tests/bootstrap.php | 7 ++++++- tests/schema.php | 22 ++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/schema.php diff --git a/.gitignore b/.gitignore index a192a98..117b2e7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,6 @@ /vendor /composer.lock /nbproject -/phpunit.xml /coverage.xml .phpunit.* +test.sqlite \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..bba8135 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,17 @@ + + + + + + ./tests/TestCase/ + + + + + + \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php index eac92ad..407a55e 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,6 +11,8 @@ use Cake\Core\BasePlugin; use Cake\Core\Plugin; +use Cake\Datasource\ConnectionManager; +use Cake\TestSuite\Fixture\SchemaLoader; error_reporting(E_ALL & ~E_USER_DEPRECATED); @@ -35,7 +37,10 @@ require $root . '/config/bootstrap.php'; } -require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; +ConnectionManager::setConfig('test', ['url' => 'sqlite://tmp/test.sqlite']); + +$loader = new SchemaLoader(); +$loader->loadInternalFile($root . '/tests/schema.php'); Plugin::getCollection()->add(new BasePlugin([ 'name' => 'Robotusers/Chunk', diff --git a/tests/schema.php b/tests/schema.php new file mode 100644 index 0000000..9f69187 --- /dev/null +++ b/tests/schema.php @@ -0,0 +1,22 @@ + [ + 'columns' => [ + 'id' => [ + 'type' => 'integer', + ], + 'name' => [ + 'type' => 'string', + 'default' => null, + ], + ], + 'constraints' => [ + 'primary' => [ + 'type' => 'primary', + 'columns' => [ + 'id', + ], + ], + ], + ], +]; \ No newline at end of file From 2a9eacc976ec4b52621320eacc74d54073f9db93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:23:12 +0100 Subject: [PATCH 3/7] Update tests --- .../Model/Behavior/ChunkBehaviorTest.php | 4 +-- tests/TestCase/ORM/ResultSetTest.php | 31 ++++++------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php index 3cde097..7d7f7cb 100644 --- a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php @@ -38,13 +38,13 @@ */ class ChunkBehaviorTest extends TestCase { - public $fixtures = [ + public array $fixtures = [ 'core.Authors', ]; public function testChunk() { - $table = TableRegistry::get('Authors'); + $table = $this->getTableLocator()->get('Authors'); $query = $table->find(); $behavior = new ChunkBehavior($table); diff --git a/tests/TestCase/ORM/ResultSetTest.php b/tests/TestCase/ORM/ResultSetTest.php index 574a7f9..dfd5d2f 100644 --- a/tests/TestCase/ORM/ResultSetTest.php +++ b/tests/TestCase/ORM/ResultSetTest.php @@ -39,13 +39,13 @@ */ class ResultSetTest extends TestCase { - public $fixtures = [ + public array $fixtures = [ 'core.Authors', ]; public function testSameResults() { - $table = TableRegistry::get('Authors'); + $table = $this->getTableLocator()->get('Authors'); $query = $table->find(); @@ -59,7 +59,7 @@ public function testSameResults() public function testMultipleQueriesFired() { - $table = TableRegistry::get('Authors'); + $table = $this->getTableLocator()->get('Authors'); $called = 0; $query = $table->find()->formatResults(function ($r) use (&$called) { @@ -79,7 +79,7 @@ public function testMultipleQueriesFired() public function testLimitAndOffset() { - $table = TableRegistry::get('Authors'); + $table = $this->getTableLocator()->get('Authors'); $query = $table->find()->limit(2)->page(2); @@ -94,9 +94,9 @@ public function testLimitAndOffset() public function testSerialize() { $this->expectException(RuntimeException::class); - $this->expectErrorMessage('You cannot serialize this result set.'); + $this->expectExceptionMessage('You cannot serialize this result set.'); - $table = TableRegistry::get('Authors'); + $table = $this->getTableLocator()->get('Authors'); $query = $table->find(); $chunkedResults = new ResultSet($query); @@ -106,9 +106,9 @@ public function testSerialize() public function testUnserialize() { $this->expectException(RuntimeException::class); - $this->expectErrorMessage('You cannot unserialize this result set.'); + $this->expectExceptionMessage('You cannot unserialize this result set.'); - $table = TableRegistry::get('Authors'); + $table = $this->getTableLocator()->get('Authors'); $query = $table->find(); $chunkedResults = new ResultSet($query); @@ -118,23 +118,12 @@ public function testUnserialize() public function testCount() { $this->expectException(RuntimeException::class); - $this->expectErrorMessage('Count is not supported yet.'); + $this->expectExceptionMessage('Count is not supported yet.'); - $table = TableRegistry::get('Authors'); + $table = $this->getTableLocator()->get('Authors'); $query = $table->find(); $chunkedResults = new ResultSet($query); $chunkedResults->count(); } - - public function testInvalidQuery() - { - $this->expectException(RuntimeException::class); - $this->expectErrorMessage('You cannot chunk a non-select query.'); - - $table = TableRegistry::get('Authors'); - $query = $table->query()->insert(['foo' => 'bar']); - - new ResultSet($query); - } } From 2e569c01f5e8f8488cd5db45791b3fd2025972ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:26:51 +0100 Subject: [PATCH 4/7] Type hints added --- src/Model/Behavior/ChunkBehavior.php | 6 ++-- src/ORM/ResultSet.php | 50 ++++++++++++++-------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Model/Behavior/ChunkBehavior.php b/src/Model/Behavior/ChunkBehavior.php index 1c046c6..8d0d5b9 100644 --- a/src/Model/Behavior/ChunkBehavior.php +++ b/src/Model/Behavior/ChunkBehavior.php @@ -27,7 +27,7 @@ namespace Robotusers\Chunk\Model\Behavior; use Cake\ORM\Behavior; -use Cake\ORM\Query; +use Cake\ORM\Query\SelectQuery; use Robotusers\Chunk\ORM\ResultSet; class ChunkBehavior extends Behavior @@ -35,11 +35,11 @@ class ChunkBehavior extends Behavior /** * Returns chunked result set. * - * @param \Cake\ORM\Query $query Query instance. + * @param \Cake\ORM\Query\SelectQuery $query Query instance. * @param array $config Config. * @return \Robotusers\Chunk\ORM\ResultSet */ - public function chunk(Query $query, array $config = []) + public function chunk(SelectQuery $query, array $config = []): ResultSet { return new ResultSet($query, $config); } diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index 9cb031a..8889c36 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -29,7 +29,7 @@ use Cake\Collection\CollectionTrait; use Cake\Core\InstanceConfigTrait; use Cake\Datasource\ResultSetInterface; -use Cake\ORM\Query; +use Cake\ORM\Query\SelectQuery; use RuntimeException; class ResultSet implements ResultSetInterface @@ -42,70 +42,70 @@ class ResultSet implements ResultSetInterface * * @var mixed */ - protected $current; + protected mixed $current; /** * Query instance. * - * @var \Cake\ORM\Query + * @var \Cake\ORM\Query\SelectQuery */ - protected $query; + protected SelectQuery $query; /** * Current chunk size. * * @var int */ - protected $chunkSize = 0; + protected int $chunkSize = 0; /** * Current chunk index. * * @var int */ - protected $chunkIndex = 0; + protected int $chunkIndex = 0; /** * Current chunk content. * - * @var array + * @var iterable */ - protected $chunk; + protected iterable $chunk; /** * Current element index. * * @var int */ - protected $index = 0; + protected int $index = 0; /** * Current page. * * @var int */ - protected $page = 0; + protected int $page = 0; /** * Original query offset. * - * @var int + * @var ?int */ - protected $offset; + protected ?int $offset; /** * Original query limit. * - * @var int + * @var ?int */ - protected $limit; + protected ?int $limit; /** * Total count. * * @var int */ - protected $count; + protected int $count; /** * Default config. @@ -114,18 +114,18 @@ class ResultSet implements ResultSetInterface * * @var array */ - protected $_defaultConfig = [ + protected array $_defaultConfig = [ 'size' => 1000, ]; /** * Constructor. * - * @param \Cake\ORM\Query $query Query object. + * @param \Cake\ORM\Query\SelectQuery $query Query object. * @param array $config Configuration. * @throws \RuntimeException When query is not supported. */ - public function __construct(Query $query, array $config = []) + public function __construct(SelectQuery $query, array $config = []) { $type = $query->type(); if ($type !== 'select') { @@ -145,7 +145,7 @@ public function __construct(Query $query, array $config = []) * @inheritDoc */ #[\ReturnTypeWillChange] - public function current() + public function current(): mixed { return $this->current; } @@ -154,7 +154,7 @@ public function current() * @inheritDoc */ #[\ReturnTypeWillChange] - public function key() + public function key(): mixed { return $this->index; } @@ -215,7 +215,7 @@ public function valid(): bool * * @return void */ - protected function fetchChunk() + protected function fetchChunk(): void { $size = $this->getConfig('size'); @@ -251,12 +251,12 @@ public function count(): int * * Serialization is not supported (yet). * */ - public function serialize() + public function serialize(): never { throw new RuntimeException('You cannot serialize this result set.'); } - public function __serialize(): array + public function __serialize(): never { throw new RuntimeException('You cannot serialize this result set.'); } @@ -266,12 +266,12 @@ public function __serialize(): array * * Serialization is not supported (yet). * */ - public function unserialize($serialized) + public function unserialize($serialized): never { throw new RuntimeException('You cannot unserialize this result set.'); } - public function __unserialize(array $data): void + public function __unserialize(array $data): never { throw new RuntimeException('You cannot unserialize this result set.'); } From 9568a178cbd37b48029df45fb5e3aa98759a2194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:27:12 +0100 Subject: [PATCH 5/7] CS --- phpunit.xml.dist | 34 ------------------- src/ORM/ResultSet.php | 5 +-- .../Model/Behavior/ChunkBehaviorTest.php | 1 - tests/TestCase/ORM/ResultSetTest.php | 1 - tests/schema.php | 4 ++- 5 files changed, 6 insertions(+), 39 deletions(-) delete mode 100644 phpunit.xml.dist diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 66d00ef..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - ./tests/TestCase/ - - - - - - - - - - - - - - ./src/ - - - \ No newline at end of file diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index 8889c36..ba819be 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -30,6 +30,7 @@ use Cake\Core\InstanceConfigTrait; use Cake\Datasource\ResultSetInterface; use Cake\ORM\Query\SelectQuery; +use ReturnTypeWillChange; use RuntimeException; class ResultSet implements ResultSetInterface @@ -144,7 +145,7 @@ public function __construct(SelectQuery $query, array $config = []) /** * @inheritDoc */ - #[\ReturnTypeWillChange] + #[ReturnTypeWillChange] public function current(): mixed { return $this->current; @@ -153,7 +154,7 @@ public function current(): mixed /** * @inheritDoc */ - #[\ReturnTypeWillChange] + #[ReturnTypeWillChange] public function key(): mixed { return $this->index; diff --git a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php index 7d7f7cb..cf4a59f 100644 --- a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php @@ -26,7 +26,6 @@ */ namespace Robotusers\Chunk\Test\TestCase\Model\Behavior; -use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use Robotusers\Chunk\Model\Behavior\ChunkBehavior; use Robotusers\Chunk\ORM\ResultSet; diff --git a/tests/TestCase/ORM/ResultSetTest.php b/tests/TestCase/ORM/ResultSetTest.php index dfd5d2f..243cb77 100644 --- a/tests/TestCase/ORM/ResultSetTest.php +++ b/tests/TestCase/ORM/ResultSetTest.php @@ -27,7 +27,6 @@ namespace Robotusers\Chunk\Test\TestCase\Model; -use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use Robotusers\Chunk\ORM\ResultSet; use RuntimeException; diff --git a/tests/schema.php b/tests/schema.php index 9f69187..33b916b 100644 --- a/tests/schema.php +++ b/tests/schema.php @@ -1,4 +1,6 @@ [ 'columns' => [ @@ -19,4 +21,4 @@ ], ], ], -]; \ No newline at end of file +]; From 1e74837b7a001b3fe314d49610b6ade42a353e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:30:44 +0100 Subject: [PATCH 6/7] Bump Stan --- phpstan.neon | 12 +++++++----- src/ORM/ResultSet.php | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 86901ed..c05edec 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,9 @@ parameters: - level: 4 -# reportUnmatchedIgnoredErrors: false -# treatPhpDocTypesAsCertain: false + level: 8 paths: - - src/ - - tests/ \ No newline at end of file + - src/ + ignoreErrors: + - + identifier: missingType.generics + - + identifier: missingType.iterableValue \ No newline at end of file diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index ba819be..6783bdf 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -69,9 +69,9 @@ class ResultSet implements ResultSetInterface /** * Current chunk content. * - * @var iterable + * @var array */ - protected iterable $chunk; + protected array $chunk; /** * Current element index. @@ -267,7 +267,7 @@ public function __serialize(): never * * Serialization is not supported (yet). * */ - public function unserialize($serialized): never + public function unserialize(string $serialized): never { throw new RuntimeException('You cannot unserialize this result set.'); } From 207df3aa376292b4c2624ec5f79526d41fbfb605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:37:26 +0100 Subject: [PATCH 7/7] Disable coverage --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index fc6cd7a..f877ab9 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -39,4 +39,4 @@ jobs: run: vendor/bin/phpstan analyse - name: Run tests - run: vendor/bin/phpunit --coverage-clover=coverage.xml + run: vendor/bin/phpunit