From c66fa94ede8dbeb39af38382ea640783294aaf9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Mon, 21 Nov 2022 15:55:36 +0100 Subject: [PATCH 1/8] Update to Cake 4 --- composer.json | 8 ++--- src/ORM/ResultSet.php | 10 +++--- .../Model/Behavior/ChunkBehaviorTest.php | 2 +- tests/TestCase/ORM/ResultSetTest.php | 31 +++++++++---------- tests/bootstrap.php | 7 +++-- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index 457a990..6c027b9 100644 --- a/composer.json +++ b/composer.json @@ -5,13 +5,13 @@ "type": "cakephp-plugin", "license": "MIT", "require": { - "php": ">=5.6", - "cakephp/orm": "^3.4" + "cakephp/orm": "~4.0" }, "require-dev": { - "cakephp/cakephp": "~3.4.0", + "cakephp/cakephp": "~4.0.0", "cakephp/cakephp-codesniffer": "^3.0", - "phpunit/phpunit": "^5.6|^6" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^1.9" }, "autoload": { "psr-4": { diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index 94cab36..46c10ef 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -142,6 +142,7 @@ public function __construct(Query $query, array $config = []) /** * {@inheritDoc} */ + #[\ReturnTypeWillChange] public function current() { return $this->current; @@ -150,6 +151,7 @@ public function current() /** * {@inheritDoc} */ + #[\ReturnTypeWillChange] public function key() { return $this->index; @@ -158,7 +160,7 @@ public function key() /** * {@inheritDoc} */ - public function next() + public function next(): void { $this->index++; $this->chunkIndex++; @@ -167,7 +169,7 @@ public function next() /** * {@inheritDoc} */ - public function rewind() + public function rewind(): void { $this->index = 0; $this->page = 1; @@ -178,7 +180,7 @@ public function rewind() /** * {@inheritDoc} */ - public function valid() + public function valid(): bool { if ($this->limit && $this->index >= $this->limit) { return false; @@ -237,7 +239,7 @@ protected function fetchChunk() /** * {@inheritDoc} */ - public function count() + public function count(): int { throw new RuntimeException('Count is not supported yet.'); } diff --git a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php index 88f5bb9..1c11f9a 100644 --- a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php @@ -37,7 +37,7 @@ class ChunkBehaviorTest extends TestCase { public $fixtures = [ - 'core.authors' + 'core.Authors' ]; public function testChunk() diff --git a/tests/TestCase/ORM/ResultSetTest.php b/tests/TestCase/ORM/ResultSetTest.php index b71d920..b62e74c 100644 --- a/tests/TestCase/ORM/ResultSetTest.php +++ b/tests/TestCase/ORM/ResultSetTest.php @@ -28,6 +28,7 @@ use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use Robotusers\Chunk\ORM\ResultSet; +use RuntimeException; /** * Description of ResultsSetTest @@ -37,7 +38,7 @@ class ResultsSetTest extends TestCase { public $fixtures = [ - 'core.authors' + 'core.Authors' ]; public function testSameResults() @@ -88,12 +89,11 @@ public function testLimitAndOffset() $this->assertEquals($standardResults->toArray(), $chunkedResults->toArray()); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage You cannot serialize this result set. - */ public function testSerialize() { + $this->expectException(RuntimeException::class); + $this->expectErrorMessage('You cannot serialize this result set.'); + $table = TableRegistry::get('Authors'); $query = $table->find(); @@ -101,12 +101,11 @@ public function testSerialize() $chunkedResults->serialize(); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage You cannot unserialize this result set. - */ public function testUnserialize() { + $this->expectException(RuntimeException::class); + $this->expectErrorMessage('You cannot unserialize this result set.'); + $table = TableRegistry::get('Authors'); $query = $table->find(); @@ -114,12 +113,11 @@ public function testUnserialize() $chunkedResults->unserialize(''); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage Count is not supported yet. - */ public function testCount() { + $this->expectException(RuntimeException::class); + $this->expectErrorMessage('Count is not supported yet.'); + $table = TableRegistry::get('Authors'); $query = $table->find(); @@ -127,12 +125,11 @@ public function testCount() $chunkedResults->count(); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage You cannot chunk a non-select query. - */ 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']); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1c2790c..3b18c48 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -6,6 +6,8 @@ * has been installed as a dependency of the plugin, or the plugin is itself * installed as a dependency of an application. */ + +use Cake\Core\BasePlugin; use Cake\Core\Plugin; $findRoot = function ($root) { @@ -31,6 +33,7 @@ require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php'; -Plugin::load('Robotusers/Chunk', [ +Plugin::getCollection()->add(new BasePlugin([ + 'name' => 'Robotusers/Chunk', 'path' => PLUGIN_ROOT . DS -]); +])); From 02a9ab8aab82d7521800fa9680ee390f9d6f835f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 08:45:19 +0100 Subject: [PATCH 2/8] Create php.yml --- .github/workflows/php.yml | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 .github/workflows/php.yml diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..a668078 --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,42 @@ +name: PHP Composer + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v3 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Run CS + run: vendor/bin/phpcs -p --extensions=php ./src ./tests + + - name: Run stan + run: vendor/bin/phpstan analyse ./src --level 4 + + - name: Run tests + run: vendor/bin/phpunit --coverage-clover=coverage.xml From 3f359e44d8b35817088ab534a97449fa9c267a21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 08:47:41 +0100 Subject: [PATCH 3/8] Del travis --- .travis.yml | 44 -------------------------------------------- 1 file changed, 44 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0b92138..0000000 --- a/.travis.yml +++ /dev/null @@ -1,44 +0,0 @@ -language: php - -dist: trusty - -sudo: false - -php: - - 5.6 - - 7.0 - - 7.1 - -env: - global: - - DEFAULT=1 - -matrix: - fast_finish: true - - include: - - php: 5.6 - env: PHPCS=1 DEFAULT=0 - - php: 7.1 - env: PHPSTAN=4 DEFAULT=0 - - php: 7.1 - env: PHPSTAN=7 DEFAULT=0 - - allow_failures: - - env: PHPCS=1 DEFAULT=0 - - env: PHPSTAN=7 DEFAULT=0 - -before_script: - - composer install; - - if [[ $PHPSTAN > 0 ]]; then composer require phpstan/phpstan:^0.7; fi - -script: - - if [[ $DEFAULT = 1 ]]; then vendor/bin/phpunit --coverage-clover=coverage.xml; fi - - if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php ./src ./tests; fi - - if [[ $PHPSTAN > 0 ]]; then vendor/bin/phpstan analyse ./src --level $PHPSTAN; fi - -notifications: - email: false - -after_success: - - bash <(curl -s https://codecov.io/bash) From ca582ec2babbff0f452fd867750be0e394b4da2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 08:51:47 +0100 Subject: [PATCH 4/8] Loosen dev dep --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6c027b9..66f75d7 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "cakephp/orm": "~4.0" }, "require-dev": { - "cakephp/cakephp": "~4.0.0", + "cakephp/cakephp": "~4.0", "cakephp/cakephp-codesniffer": "^3.0", "phpunit/phpunit": "^9.5", "phpstan/phpstan": "^1.9" From e35379f540db75ccdc15c1e862654c7ad56b8abd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 08:55:20 +0100 Subject: [PATCH 5/8] CS fixes --- composer.json | 7 ++++- phpcs.xml.dist | 16 ++++++++-- src/Model/Behavior/ChunkBehavior.php | 7 +++-- src/ORM/ResultSet.php | 30 ++++++++++--------- .../Model/Behavior/ChunkBehaviorTest.php | 6 ++-- tests/TestCase/ORM/ResultSetTest.php | 12 ++++---- tests/bootstrap.php | 6 ++-- 7 files changed, 55 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index 66f75d7..c067cf6 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ }, "require-dev": { "cakephp/cakephp": "~4.0", - "cakephp/cakephp-codesniffer": "^3.0", + "cakephp/cakephp-codesniffer": "^4.0", "phpunit/phpunit": "^9.5", "phpstan/phpstan": "^1.9" }, @@ -23,5 +23,10 @@ "Cake\\Test\\": "vendor/cakephp/cakephp/tests", "Robotusers\\Chunk\\Test\\": "tests" } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 36805de..d09769c 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,4 +1,16 @@ - - + + + + + + + + + + + + + src/ + tests/ diff --git a/src/Model/Behavior/ChunkBehavior.php b/src/Model/Behavior/ChunkBehavior.php index 2ad8d19..1c046c6 100644 --- a/src/Model/Behavior/ChunkBehavior.php +++ b/src/Model/Behavior/ChunkBehavior.php @@ -1,4 +1,6 @@ 1000 + 'size' => 1000, ]; /** * Constructor. * - * @param Query $query Query object. + * @param \Cake\ORM\Query $query Query object. * @param array $config Configuration. - * @throws RuntimeException When query is not supported. + * @throws \RuntimeException When query is not supported. */ public function __construct(Query $query, array $config = []) { @@ -140,7 +142,7 @@ public function __construct(Query $query, array $config = []) } /** - * {@inheritDoc} + * @inheritDoc */ #[\ReturnTypeWillChange] public function current() @@ -149,7 +151,7 @@ public function current() } /** - * {@inheritDoc} + * @inheritDoc */ #[\ReturnTypeWillChange] public function key() @@ -158,7 +160,7 @@ public function key() } /** - * {@inheritDoc} + * @inheritDoc */ public function next(): void { @@ -167,7 +169,7 @@ public function next(): void } /** - * {@inheritDoc} + * @inheritDoc */ public function rewind(): void { @@ -178,7 +180,7 @@ public function rewind(): void } /** - * {@inheritDoc} + * @inheritDoc */ public function valid(): bool { @@ -237,7 +239,7 @@ protected function fetchChunk() } /** - * {@inheritDoc} + * @inheritDoc */ public function count(): int { @@ -245,9 +247,9 @@ public function count(): int } /** - * Serialization is not supported (yet). - * * {@inheritDoc} + * + * Serialization is not supported (yet). * */ public function serialize() { @@ -255,9 +257,9 @@ public function serialize() } /** - * Serialization is not supported (yet). - * * {@inheritDoc} + * + * Serialization is not supported (yet). * */ public function unserialize($serialized) { diff --git a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php index 1c11f9a..3cde097 100644 --- a/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/ChunkBehaviorTest.php @@ -1,4 +1,6 @@ chunk($query, [ - 'size' => 100 + 'size' => 100, ]); $this->assertInstanceOf(ResultSet::class, $chunk); diff --git a/tests/TestCase/ORM/ResultSetTest.php b/tests/TestCase/ORM/ResultSetTest.php index b62e74c..574a7f9 100644 --- a/tests/TestCase/ORM/ResultSetTest.php +++ b/tests/TestCase/ORM/ResultSetTest.php @@ -1,4 +1,6 @@ */ -class ResultsSetTest extends TestCase +class ResultSetTest extends TestCase { public $fixtures = [ - 'core.Authors' + 'core.Authors', ]; public function testSameResults() @@ -49,7 +51,7 @@ public function testSameResults() $standardResults = $query->all(); $chunkedResults = new ResultSet($query, [ - 'size' => 1 + 'size' => 1, ]); $this->assertEquals($standardResults->toArray(), $chunkedResults->toArray()); @@ -67,7 +69,7 @@ public function testMultipleQueriesFired() }); $results = new ResultSet($query, [ - 'size' => 1 + 'size' => 1, ]); $results->toList(); @@ -83,7 +85,7 @@ public function testLimitAndOffset() $standardResults = $query->all(); $chunkedResults = new ResultSet($query, [ - 'size' => 1 + 'size' => 1, ]); $this->assertEquals($standardResults->toArray(), $chunkedResults->toArray()); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3b18c48..fadd248 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,4 +1,6 @@ add(new BasePlugin([ 'name' => 'Robotusers/Chunk', - 'path' => PLUGIN_ROOT . DS + 'path' => PLUGIN_ROOT . DS, ])); From 04d9a8ed24e3479ed349657d197cbb24a69ae85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 08:59:44 +0100 Subject: [PATCH 6/8] PHP Stan fixes --- composer.json | 2 +- phpstan.neon | 7 +++++++ src/ORM/ResultSet.php | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index c067cf6..fbbc832 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "cakephp/cakephp": "~4.0", "cakephp/cakephp-codesniffer": "^4.0", "phpunit/phpunit": "^9.5", - "phpstan/phpstan": "^1.9" + "phpstan/phpstan": "^2.0" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..86901ed --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +parameters: + level: 4 +# reportUnmatchedIgnoredErrors: false +# treatPhpDocTypesAsCertain: false + paths: + - src/ + - tests/ \ No newline at end of file diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index 4023a68..9cb031a 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -256,6 +256,11 @@ public function serialize() throw new RuntimeException('You cannot serialize this result set.'); } + public function __serialize(): array + { + throw new RuntimeException('You cannot serialize this result set.'); + } + /** * {@inheritDoc} * @@ -265,4 +270,9 @@ public function unserialize($serialized) { throw new RuntimeException('You cannot unserialize this result set.'); } + + public function __unserialize(array $data): void + { + throw new RuntimeException('You cannot unserialize this result set.'); + } } From 58cd7252a3faa53d3a3159ebbdc0a1ce1411b98d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:00:04 +0100 Subject: [PATCH 7/8] Update workflows --- .github/workflows/php.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index a668078..fc6cd7a 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -33,10 +33,10 @@ jobs: run: composer install --prefer-dist --no-progress - name: Run CS - run: vendor/bin/phpcs -p --extensions=php ./src ./tests + run: vendor/bin/phpcs - name: Run stan - run: vendor/bin/phpstan analyse ./src --level 4 + run: vendor/bin/phpstan analyse - name: Run tests run: vendor/bin/phpunit --coverage-clover=coverage.xml From eacdf6ca8e3c1e60ded5852496257c0e55dc7566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Pustu=C5=82ka?= Date: Thu, 12 Dec 2024 09:06:50 +0100 Subject: [PATCH 8/8] Hide deprecations --- .gitignore | 1 + tests/bootstrap.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 98aab43..a192a98 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /nbproject /phpunit.xml /coverage.xml +.phpunit.* diff --git a/tests/bootstrap.php b/tests/bootstrap.php index fadd248..eac92ad 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -12,6 +12,8 @@ use Cake\Core\BasePlugin; use Cake\Core\Plugin; +error_reporting(E_ALL & ~E_USER_DEPRECATED); + $findRoot = function ($root) { do { $lastRoot = $root;