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 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/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": { 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/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/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/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..6783bdf 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -29,7 +29,8 @@ use Cake\Collection\CollectionTrait; use Cake\Core\InstanceConfigTrait; use Cake\Datasource\ResultSetInterface; -use Cake\ORM\Query; +use Cake\ORM\Query\SelectQuery; +use ReturnTypeWillChange; use RuntimeException; class ResultSet implements ResultSetInterface @@ -42,70 +43,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 array */ - protected $chunk; + protected array $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 +115,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') { @@ -144,8 +145,8 @@ public function __construct(Query $query, array $config = []) /** * @inheritDoc */ - #[\ReturnTypeWillChange] - public function current() + #[ReturnTypeWillChange] + public function current(): mixed { return $this->current; } @@ -153,8 +154,8 @@ public function current() /** * @inheritDoc */ - #[\ReturnTypeWillChange] - public function key() + #[ReturnTypeWillChange] + public function key(): mixed { return $this->index; } @@ -215,7 +216,7 @@ public function valid(): bool * * @return void */ - protected function fetchChunk() + protected function fetchChunk(): void { $size = $this->getConfig('size'); @@ -251,12 +252,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 +267,12 @@ public function __serialize(): array * * Serialization is not supported (yet). * */ - public function unserialize($serialized) + public function unserialize(string $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.'); } diff --git a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php index 3cde097..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; @@ -38,13 +37,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..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; @@ -39,13 +38,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 +58,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 +78,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 +93,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 +105,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 +117,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); - } } 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..33b916b --- /dev/null +++ b/tests/schema.php @@ -0,0 +1,24 @@ + [ + 'columns' => [ + 'id' => [ + 'type' => 'integer', + ], + 'name' => [ + 'type' => 'string', + 'default' => null, + ], + ], + 'constraints' => [ + 'primary' => [ + 'type' => 'primary', + 'columns' => [ + 'id', + ], + ], + ], + ], +];