From b4e5ef9edf391c1b71dcb35fe9fdda8d3b46ad27 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 5 Jun 2024 14:19:08 +0530 Subject: [PATCH 01/14] Initial commit to create PR --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9ca3409..28cae77 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,4 @@ With https://fractal.thephpleague.com - Run once `make installdocker` - Run `make testdocker` or `make cli` and inside docker env `make test` + From 2a40560d8926cf899f04a60d2d3213a6e4372199 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Wed, 5 Jun 2024 16:45:21 +0530 Subject: [PATCH 02/14] Fix issues faced during local setup --- Makefile | 10 +++++----- README.md | 2 -- tests/docker/Dockerfile | 4 ++-- tests/docker/xdebug.ini | 10 +++++----- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index e8cd385..c3a41d4 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PHPARGS=-dmemory_limit=64M -#PHPARGS=-dmemory_limit=64M -dzend_extension=xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_autostart=1 -#PHPARGS=-dmemory_limit=64M -dxdebug.remote_enable=1 +#PHPARGS=-dmemory_limit=64M -dzend_extension=xdebug.so -dxdebug.mode=debug -dxdebug.remote_host=127.0.0.1 -dxdebug.start_with_request=yes +#PHPARGS=-dmemory_limit=64M -dxdebug.mode=debug all: @@ -24,9 +24,9 @@ clean_all: up: docker-compose up -d - chmod +rw -R tests/tmp - chmod +rw -R tests/codeception - mkdir -p tests/testapp/runtime && chmod +rw -R tests/testapp/runtime + docker-compose run --rm php bash -c 'chmod +rw -R tests/tmp' + docker-compose run --rm php bash -c 'chmod +rw -R tests/codeception' + docker-compose run --rm php bash -c 'mkdir -p tests/testapp/runtime && chmod +rw -R tests/testapp/runtime' cli: docker-compose exec php bash diff --git a/README.md b/README.md index 28cae77..cf48eb1 100644 --- a/README.md +++ b/README.md @@ -27,5 +27,3 @@ With https://fractal.thephpleague.com - Run `make up` - Run once `make installdocker` - Run `make testdocker` or `make cli` and inside docker env `make test` - - diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile index 6db72fb..8a79b2a 100755 --- a/tests/docker/Dockerfile +++ b/tests/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM yiisoftware/yii2-php:7.1-apache +FROM yiisoftware/yii2-php:7.4-apache ENV INSIDE_DOCKER=1 @@ -6,4 +6,4 @@ COPY apache.conf /etc/apache2/sites-enabled/000-default.conf # enable xdebug RUN docker-php-ext-enable xdebug -COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini \ No newline at end of file +COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini diff --git a/tests/docker/xdebug.ini b/tests/docker/xdebug.ini index 948fec6..56648cc 100755 --- a/tests/docker/xdebug.ini +++ b/tests/docker/xdebug.ini @@ -1,7 +1,7 @@ -xdebug.remote_enable=1 -xdebug.remote_autostart=1 -xdebug.remote_connect_back=1 +xdebug.mode=debug +xdebug.start_with_request=yes +xdebug.discover_client_host=1 ;xdebug.remote_host=host.docker.internal -xdebug.remote_port=9000 +xdebug.client_port=9000 xdebug.idekey=PHPSTORM -xdebug.remote_timeout = 10 +xdebug.connect_timeout_ms = 10 From b69885a01ffaa4326673c9a3751aa4976321137e Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 6 Jun 2024 16:50:40 +0530 Subject: [PATCH 03/14] Add docs - WIP --- .env.dist | 1 + CONTRIBUTING.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 18 ++++++++----- 3 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 .env.dist create mode 100644 CONTRIBUTING.md diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..063c3ec --- /dev/null +++ b/.env.dist @@ -0,0 +1 @@ +PHP_VERSION=7.4 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..1020648 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,71 @@ +Contribute to yii2-fractal library +================================== + +### Local setup + +[Docker](https://docs.docker.com/engine/install/) is required + +```bash +git clone git@github.com:php-openapi/yii2-fractal.git +cd yii2-fractal +make up +make installdocker # only once during initiation +``` + +### Test + +Ensure you followed steps mentioned in Local setup, then + +```bash +make cli +make test +``` + +### Switching PHP versions + + +You can switch the PHP version of the docker runtime by changing the `PHP_VERSION` environment variable in the `.env` file. + +If you have no `.env` file yet, create it by copying `.env.dist` to `.env`. + +After changing the PHP Version you need to run `make clean_all up` to start the new container with new version. + +Example: + +``` +$ echo "PHP_VERSION=7.4" > .env +$ make clean_all up cli +Stopping yii2-openapi_php_1 ... done # TODO +Stopping yii2-openapi_maria_1 ... done +Stopping yii2-openapi_postgres_1 ... done +Stopping yii2-openapi_mysql_1 ... done +Removing yii2-openapi_php_1 ... done +Removing yii2-openapi_maria_1 ... done +Removing yii2-openapi_postgres_1 ... done +Removing yii2-openapi_mysql_1 ... done +Removing network yii2-openapi_default +Creating network "yii2-openapi_default" with driver "bridge" +Creating yii2-openapi_maria_1 ... done +Creating yii2-openapi_mysql_1 ... done +Creating yii2-openapi_postgres_1 ... done +Creating yii2-openapi_php_1 ... done +docker-compose exec php bash + +root@f9928598f841:/app# php -v + +PHP 7.4.27 (cli) (built: Jan 26 2022 18:08:44) ( NTS ) +Copyright (c) The PHP Group +Zend Engine v3.4.0, Copyright (c) Zend Technologies +with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies +with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans +``` + + + + + ### Testing # todo remove + + - Clone project + - Run `make up` + - Run once `make installdocker` + - Run `make testdocker` or `make cli` and inside docker env `make test` diff --git a/README.md b/README.md index cf48eb1..8195436 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![yii2-fractal](https://github.com/php-openapi/yii2-fractal/workflows/yii2-fractal/badge.svg) -The set of utils and actions for prepare Rest API accordingly JSON:Api https://jsonapi.org/format/ +The set of utils and actions for prepare Rest API accordingly JSON:API https://jsonapi.org/format/ With https://fractal.thephpleague.com ### Installation @@ -21,9 +21,13 @@ With https://fractal.thephpleague.com ['bootstrap' section]: https://www.yiiframework.com/doc/guide/2.0/en/runtime-bootstrapping - ### Testing - - - Clone project - - Run `make up` - - Run once `make installdocker` - - Run `make testdocker` or `make cli` and inside docker env `make test` + + +### Contributing / Local Development / Testing + +See [CONTRIBUTING.md](CONTRIBUTING.md) file + + +### License + +See [LICENSE](LICENSE) file \ No newline at end of file From 30b0107023d24d4ae77c937411a82ee007e0ba1d Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 6 Jun 2024 18:26:33 +0530 Subject: [PATCH 04/14] Make enhancements --- .gitignore | 3 +++ Makefile | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7d285b7..1bff10b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ /composer.lock /phpunit.xml /.php_cs.cache + +/.env +/.phpunit.result.cache diff --git a/Makefile b/Makefile index c3a41d4..51c6af7 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ test: php $(PHPARGS) vendor/bin/codecept run clean_all: - docker-compose down + docker-compose down --remove-orphans sudo rm -rf tests/tmp/* up: From 11bcb4c94264b962ad0f2a3af8feb3791323414f Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 6 Jun 2024 19:05:26 +0530 Subject: [PATCH 05/14] Fix issues related to dependencies --- .env.dist | 2 +- .github/workflows/test.yml | 2 +- CONTRIBUTING.md | 6 +++--- composer.json | 12 ++++++------ tests/docker/Dockerfile | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.env.dist b/.env.dist index 063c3ec..0782604 100644 --- a/.env.dist +++ b/.env.dist @@ -1 +1 @@ -PHP_VERSION=7.4 +PHP_VERSION=8.0 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0b31352..1348ec9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,7 +32,7 @@ jobs: strategy: fail-fast: false matrix: - php-versions: ['7.4', '8.1', '8.2', '8.3'] + php-versions: ['8.0', '8.1', '8.2', '8.3'] steps: - uses: actions/checkout@v2 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 1020648..724d4a1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -33,7 +33,7 @@ After changing the PHP Version you need to run `make clean_all up` to start the Example: ``` -$ echo "PHP_VERSION=7.4" > .env +$ echo "PHP_VERSION=8.1" > .env $ make clean_all up cli Stopping yii2-openapi_php_1 ... done # TODO Stopping yii2-openapi_maria_1 ... done @@ -53,10 +53,10 @@ docker-compose exec php bash root@f9928598f841:/app# php -v -PHP 7.4.27 (cli) (built: Jan 26 2022 18:08:44) ( NTS ) +PHP 8.1.27 (cli) (built: Jan 26 2022 18:08:44) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies -with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies +with Zend OPcache v8.1.27, Copyright (c), by Zend Technologies with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans ``` diff --git a/composer.json b/composer.json index 7e3c63d..78e5c6d 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ "type": "library", "license": "MIT", "require": { - "php": ">=7.4.0", + "php": ">=8", "ext-json": "*", "league/fractal": "^0.20.0", "yiisoft/yii2": "~2.0.15" @@ -19,13 +19,13 @@ "require-dev": { "cebe/indent": "*", "friendsofphp/php-cs-fixer": "~2.16", - "codeception/codeception": "^4.1 | ^5.0", + "codeception/codeception": "5.1.*", "codeception/module-yii2": "^1.1", - "codeception/module-rest": "^2.0 | ^3.0", - "codeception/module-phpbrowser": "^1.0 | ^2.0", - "codeception/module-asserts": "^1.0 | ^2.0 | ^3.0", + "codeception/module-rest": "^3.0", + "codeception/module-phpbrowser": "3.0.*", + "codeception/module-asserts": "^3.0", "fzaninotto/faker": "^1.9@dev", - "codeception/module-db": "^1.0 | ^2.0 | ^3.0", + "codeception/module-db": "^3.0", "codeception/assert-throws": "^1.0", "codeception/verify": "^1.5" }, diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile index 8a79b2a..c850545 100755 --- a/tests/docker/Dockerfile +++ b/tests/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM yiisoftware/yii2-php:7.4-apache +FROM yiisoftware/yii2-php:8.0-apache ENV INSIDE_DOCKER=1 From dccb6ce51af040b371546a07127f221f290af942 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 6 Jun 2024 19:35:52 +0530 Subject: [PATCH 06/14] Fix few failing tests --- Makefile | 2 ++ src/JsonApiErrorHandler.php | 1 + tests/codeception/suites/api/ApiUserCest.php | 2 +- tests/docker/xdebug.ini | 2 ++ tests/testapp/config/api.php | 2 +- 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 51c6af7..0183d44 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,8 @@ up: docker-compose run --rm php bash -c 'chmod +rw -R tests/tmp' docker-compose run --rm php bash -c 'chmod +rw -R tests/codeception' docker-compose run --rm php bash -c 'mkdir -p tests/testapp/runtime && chmod +rw -R tests/testapp/runtime' + docker-compose run --rm php bash -c 'chmod -R 777 tests/testapp/runtime' # TODO avoid 777 + cli: docker-compose exec php bash diff --git a/src/JsonApiErrorHandler.php b/src/JsonApiErrorHandler.php index 5ca5da3..c3961c9 100644 --- a/src/JsonApiErrorHandler.php +++ b/src/JsonApiErrorHandler.php @@ -70,6 +70,7 @@ class JsonApiErrorHandler extends ErrorHandler protected function renderException($exception) { + Yii::error($exception); if (Yii::$app->has('response')) { $response = Yii::$app->getResponse(); // reset parameters of response to avoid interference with partially created response data diff --git a/tests/codeception/suites/api/ApiUserCest.php b/tests/codeception/suites/api/ApiUserCest.php index f9785bd..ffe449a 100755 --- a/tests/codeception/suites/api/ApiUserCest.php +++ b/tests/codeception/suites/api/ApiUserCest.php @@ -122,7 +122,7 @@ public function testPostsCountAction(ApiTester $I) $I->amBearerAuthenticated('Gamma_secret_token'); $I->sendHEAD('/me/posts-count'); $I->seeResponseCodeIsSuccessful(); - $I->seeHttpHeader('X-Pagination-Total-Count', 18); + $I->seeHttpHeader('X-Pagination-Total-Count', '18'); } } \ No newline at end of file diff --git a/tests/docker/xdebug.ini b/tests/docker/xdebug.ini index 56648cc..d17be49 100755 --- a/tests/docker/xdebug.ini +++ b/tests/docker/xdebug.ini @@ -5,3 +5,5 @@ xdebug.discover_client_host=1 xdebug.client_port=9000 xdebug.idekey=PHPSTORM xdebug.connect_timeout_ms = 10 +;https://stackoverflow.com/a/68605372 +xdebug.log_level=0 diff --git a/tests/testapp/config/api.php b/tests/testapp/config/api.php index 6507a33..1464216 100755 --- a/tests/testapp/config/api.php +++ b/tests/testapp/config/api.php @@ -63,7 +63,7 @@ [ 'class' => yii\log\FileTarget::class, 'levels' => ['error', 'warning'], - 'logFile' => '@runtime/error.log', + 'logFile' => '/tmp/yii2-fractal-test-error.log', 'logVars' => ['_GET', '_POST'] ], ], From fd50e64da8b63e8393233b5bfb9d5acdab4e7140 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Thu, 6 Jun 2024 21:03:06 +0530 Subject: [PATCH 07/14] Fix failing test ApiUserCest.php:testMyPostsListAction WIP --- tests/testapp/transformers/CommentTransformer.php | 2 +- tests/testapp/transformers/PostShortTransformer.php | 6 +++--- tests/testapp/transformers/PostTransformer.php | 2 +- tests/testapp/transformers/UserExtendTransformer.php | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/testapp/transformers/CommentTransformer.php b/tests/testapp/transformers/CommentTransformer.php index d441316..28b12b4 100644 --- a/tests/testapp/transformers/CommentTransformer.php +++ b/tests/testapp/transformers/CommentTransformer.php @@ -7,7 +7,7 @@ class CommentTransformer extends TransformerAbstract { - public $availableIncludes = ['post']; + public array $availableIncludes = ['post']; public function transform(Comment $comment):array { diff --git a/tests/testapp/transformers/PostShortTransformer.php b/tests/testapp/transformers/PostShortTransformer.php index d8132b3..4f2acbb 100755 --- a/tests/testapp/transformers/PostShortTransformer.php +++ b/tests/testapp/transformers/PostShortTransformer.php @@ -12,7 +12,7 @@ class PostShortTransformer extends TransformerAbstract { - protected $availableIncludes = [ + protected array $availableIncludes = [ 'author', 'category' ]; @@ -26,8 +26,8 @@ public function includeAuthor(Post $post): Item $author = $post->author; return $this->item($author, function(User $author){ - return $author->getAttributes(['id', 'username', 'email', 'created_at']); - }, 'users') + ['links'=>['self'=>'/users/'.$author->id]]; + return $author->getAttributes(['id', 'username', 'email', 'created_at']) + ['links'=>['self'=>'/users/'.$author->id]]; + }, 'users'); } diff --git a/tests/testapp/transformers/PostTransformer.php b/tests/testapp/transformers/PostTransformer.php index 0fb5400..441e397 100755 --- a/tests/testapp/transformers/PostTransformer.php +++ b/tests/testapp/transformers/PostTransformer.php @@ -13,7 +13,7 @@ class PostTransformer extends TransformerAbstract { - protected $availableIncludes = [ + protected array $availableIncludes = [ 'author', 'category', 'comments' ]; diff --git a/tests/testapp/transformers/UserExtendTransformer.php b/tests/testapp/transformers/UserExtendTransformer.php index f9811ee..eb5555e 100755 --- a/tests/testapp/transformers/UserExtendTransformer.php +++ b/tests/testapp/transformers/UserExtendTransformer.php @@ -13,13 +13,13 @@ class UserExtendTransformer extends TransformerAbstract * Resources that can be included if requested. * @var array */ - protected $availableIncludes = ['comments']; + protected array $availableIncludes = ['comments']; /** * Include resources without needing it to be requested. * @var array */ - protected $defaultIncludes = ['posts']; + protected array $defaultIncludes = ['posts']; public function transform(User $user) { From b0c39cbd4d88c87024d6b73673b80414d61343f2 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Jun 2024 19:30:02 +0530 Subject: [PATCH 08/14] WIP 2 --- src/actions/ListForIdentityAction.php | 1 + src/providers/JsonApiActiveDataProvider.php | 4 ++++ tests/testapp/config/api.php | 5 ++--- tests/testapp/config/console.php | 12 +++++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/actions/ListForIdentityAction.php b/src/actions/ListForIdentityAction.php index c538a37..e3d8b60 100644 --- a/src/actions/ListForIdentityAction.php +++ b/src/actions/ListForIdentityAction.php @@ -113,6 +113,7 @@ protected function prepareDataFilter($requestParams) protected function prepareParentQuery(ActiveQueryInterface $query):ActiveQueryInterface { $userId = Yii::$app->user->id; + $condition = []; $condition[$this->modelTable().'.'.$this->userIdAttribute] = $userId; $query->where($condition); return $query; diff --git a/src/providers/JsonApiActiveDataProvider.php b/src/providers/JsonApiActiveDataProvider.php index 23f17b1..6ec63c0 100644 --- a/src/providers/JsonApiActiveDataProvider.php +++ b/src/providers/JsonApiActiveDataProvider.php @@ -63,8 +63,12 @@ public function setPagination($value):void if (!$this->_pagination instanceof JsonApiPaginator) { throw new InvalidArgumentException('Only JsonApiPaginator instance or false allowed'); } + $this->_pagination->totalCount = $this->getTotalCount(); } elseif ($value instanceof JsonApiPaginator || $value === false) { $this->_pagination = $value; + if ($value instanceof JsonApiPaginator) { + $this->_pagination->totalCount = $this->getTotalCount(); + } } else { throw new InvalidArgumentException('Only JsonApiPaginator instance, configuration array or false is allowed.'); } diff --git a/tests/testapp/config/api.php b/tests/testapp/config/api.php index 1464216..242afd2 100755 --- a/tests/testapp/config/api.php +++ b/tests/testapp/config/api.php @@ -58,11 +58,10 @@ 'validationErrorFormat' => JsonApiErrorHandler::ERROR_FORMAT_SPEC ], 'log' => [ - 'traceLevel' => YII_DEBUG ? 3 : 0, + 'traceLevel' => 3, 'targets' => [ [ 'class' => yii\log\FileTarget::class, - 'levels' => ['error', 'warning'], 'logFile' => '/tmp/yii2-fractal-test-error.log', 'logVars' => ['_GET', '_POST'] ], @@ -127,4 +126,4 @@ ], ], ], -]; \ No newline at end of file +]; diff --git a/tests/testapp/config/console.php b/tests/testapp/config/console.php index 40f00f6..ba1622c 100755 --- a/tests/testapp/config/console.php +++ b/tests/testapp/config/console.php @@ -24,5 +24,15 @@ 'password' => 'dbpass', 'charset' => 'utf8', ], + 'log' => [ + 'traceLevel' => 3, + 'targets' => [ + [ + 'class' => yii\log\FileTarget::class, + 'logFile' => '/tmp/yii2-fractal-test-error.log', + 'logVars' => ['_GET', '_POST'] + ], + ], + ], ], -]; \ No newline at end of file +]; From f31158e70ac6ea454b972fea6e1132faabff7beb Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Jun 2024 19:57:56 +0530 Subject: [PATCH 09/14] Fix bug and fix failing test --- src/actions/ListForIdentityAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/actions/ListForIdentityAction.php b/src/actions/ListForIdentityAction.php index e3d8b60..d55e31a 100644 --- a/src/actions/ListForIdentityAction.php +++ b/src/actions/ListForIdentityAction.php @@ -143,7 +143,7 @@ protected function makeDataProvider() $query->andWhere($filter); } - $dataProvider = Yii::createObject($this->dataProvider); + $dataProvider = Yii::createObject($this->dataProvider, ['query' => $query]); if (!$dataProvider instanceof JsonApiActiveDataProvider && !$dataProvider instanceof CursorActiveDataProvider) { throw new InvalidConfigException('Invalid dataProvider configuration'); } From f5d6084f0b89ae94ab0b3e9a9fd54065cebcaab6 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Jun 2024 20:32:03 +0530 Subject: [PATCH 10/14] Fix bug + Fix other failing tests --- src/actions/ListAction.php | 2 +- tests/codeception/suites/api/ApiCategoryCest.php | 2 +- tests/codeception/suites/api/ApiDefaultCest.php | 7 ++++--- tests/codeception/suites/api/ApiPostCest.php | 16 ++++++++-------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/actions/ListAction.php b/src/actions/ListAction.php index e4969e4..d9c4326 100644 --- a/src/actions/ListAction.php +++ b/src/actions/ListAction.php @@ -148,7 +148,7 @@ protected function makeDataProvider() $query->andWhere($filter); } - $dataProvider = Yii::createObject($this->dataProvider); + $dataProvider = Yii::createObject($this->dataProvider, ['query' => $query]); if (!$dataProvider instanceof JsonApiActiveDataProvider && !$dataProvider instanceof CursorActiveDataProvider) { throw new InvalidConfigException('Invalid dataProvider configuration'); } diff --git a/tests/codeception/suites/api/ApiCategoryCest.php b/tests/codeception/suites/api/ApiCategoryCest.php index 244b8b4..372b49a 100755 --- a/tests/codeception/suites/api/ApiCategoryCest.php +++ b/tests/codeception/suites/api/ApiCategoryCest.php @@ -151,7 +151,7 @@ public function testListActionWithSort(ApiTester $I) ] ]); $selfLink = $I->grabDataFromResponseByJsonPath('$.links.self'); - $I->assertContains('sort=-name', $selfLink[0]); + $I->assertStringContainsString('sort=-name', $selfLink[0]); } public function testCreateInvalidFormat1(ApiTester $I) diff --git a/tests/codeception/suites/api/ApiDefaultCest.php b/tests/codeception/suites/api/ApiDefaultCest.php index ca8e570..a9c9b64 100755 --- a/tests/codeception/suites/api/ApiDefaultCest.php +++ b/tests/codeception/suites/api/ApiDefaultCest.php @@ -100,7 +100,8 @@ public function testActionWithInternalError(ApiTester $I) $I->seeResponseCodeIsServerError(); $I->seeResponseIsJsonApiError(); $I->seeResponseHasMeta(); - $I->seeResponseContainsJson(['title' => 'PHP Warning', 'detail' => 'json_decode() expects parameter 1 to be string, array given']); - $I->seeResponseContainsJson(['meta' => ['error_type' => "yii\\base\\ErrorException"]]); + // $I->seeResponseContainsJson(['title' => 'PHP Warning', 'detail' => 'json_decode() expects parameter 1 to be string, array given']); + // $I->seeResponseContainsJson(['meta' => ['error_type' => "yii\\base\\ErrorException"]]); + $I->seeResponseContainsJson(['meta' => ['error_type' => "TypeError"]]); } -} \ No newline at end of file +} diff --git a/tests/codeception/suites/api/ApiPostCest.php b/tests/codeception/suites/api/ApiPostCest.php index 3b88569..fd5d471 100755 --- a/tests/codeception/suites/api/ApiPostCest.php +++ b/tests/codeception/suites/api/ApiPostCest.php @@ -8,11 +8,11 @@ public function testCountPostsAction(ApiTester $I) $I->amBearerAuthenticated('Delta_secret_token'); $I->sendHEAD('/post/count'); $I->seeResponseCodeIs(204); - $I->seeHttpHeader('X-Pagination-Total-Count', 54); + $I->seeHttpHeader('X-Pagination-Total-Count', '54'); $I->sendHEAD('/post/count', ['filter'=>['category_id'=>['neq'=>3]]]); $I->seeResponseCodeIs(204); - $I->seeHttpHeader('X-Pagination-Total-Count', 36); + $I->seeHttpHeader('X-Pagination-Total-Count', '36'); } public function testCountPostsForCategoryAction(ApiTester $I) @@ -21,10 +21,10 @@ public function testCountPostsForCategoryAction(ApiTester $I) $I->amBearerAuthenticated('Delta_secret_token'); $I->sendHEAD('/categories/1/posts-count'); $I->seeResponseCodeIs(204); - $I->seeHttpHeader('X-Pagination-Total-Count', 18); + $I->seeHttpHeader('X-Pagination-Total-Count', '18'); $I->sendHEAD('/categories/2/posts-count'); $I->seeResponseCodeIs(204); - $I->seeHttpHeader('X-Pagination-Total-Count', 0); + $I->seeHttpHeader('X-Pagination-Total-Count', '0'); } public function testListHasDataWithRelationships(ApiTester $I) @@ -53,10 +53,10 @@ public function testListHasDataWithRelationships(ApiTester $I) $I->sendHEAD('/post'); $I->seeResponseCodeIs(200); - $I->seeHttpHeader('X-Pagination-Total-Count', 54); - $I->seeHttpHeader('X-Pagination-Current-Page', 1); - $I->seeHttpHeader('X-Pagination-Per-Page', 20); - $I->seeHttpHeader('X-Pagination-Page-Count', 3); + $I->seeHttpHeader('X-Pagination-Total-Count', '54'); + $I->seeHttpHeader('X-Pagination-Current-Page', '1'); + $I->seeHttpHeader('X-Pagination-Per-Page', '20'); + $I->seeHttpHeader('X-Pagination-Page-Count', '3'); } public function testListFilterWithNotAllowedAttrs(ApiTester $I) From ea963f9e71df4027c03bd4e651848ff841a6f828 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Fri, 7 Jun 2024 20:33:31 +0530 Subject: [PATCH 11/14] Refactor --- src/providers/JsonApiActiveDataProvider.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/providers/JsonApiActiveDataProvider.php b/src/providers/JsonApiActiveDataProvider.php index 6ec63c0..872f5f3 100644 --- a/src/providers/JsonApiActiveDataProvider.php +++ b/src/providers/JsonApiActiveDataProvider.php @@ -64,11 +64,11 @@ public function setPagination($value):void throw new InvalidArgumentException('Only JsonApiPaginator instance or false allowed'); } $this->_pagination->totalCount = $this->getTotalCount(); - } elseif ($value instanceof JsonApiPaginator || $value === false) { + } elseif ($value instanceof JsonApiPaginator) { $this->_pagination = $value; - if ($value instanceof JsonApiPaginator) { - $this->_pagination->totalCount = $this->getTotalCount(); - } + $this->_pagination->totalCount = $this->getTotalCount(); + } elseif ($value === false) { + $this->_pagination = false; } else { throw new InvalidArgumentException('Only JsonApiPaginator instance, configuration array or false is allowed.'); } From 1e7ba896fd769237a7f745b4f1aa24a0490a448d Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 11 Jun 2024 20:14:32 +0530 Subject: [PATCH 12/14] Enable development for multiple PHP version --- Makefile | 3 +++ docker-compose.yml | 5 ++++- tests/docker/Dockerfile | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0183d44..d086e1a 100644 --- a/Makefile +++ b/Makefile @@ -23,12 +23,15 @@ clean_all: sudo rm -rf tests/tmp/* up: + docker-compose build docker-compose up -d docker-compose run --rm php bash -c 'chmod +rw -R tests/tmp' docker-compose run --rm php bash -c 'chmod +rw -R tests/codeception' docker-compose run --rm php bash -c 'mkdir -p tests/testapp/runtime && chmod +rw -R tests/testapp/runtime' docker-compose run --rm php bash -c 'chmod -R 777 tests/testapp/runtime' # TODO avoid 777 +down: + docker-compose down --remove-orphans cli: docker-compose exec php bash diff --git a/docker-compose.yml b/docker-compose.yml index 74320c1..9ade2ef 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,10 @@ version: "3.5" services: php: - build: tests/docker + build: + context: tests/docker + args: + - BUILD_PHP_VERSION=${PHP_VERSION} volumes: - ./tests/tmp/.composer:/root/.composer:rw - .:/app diff --git a/tests/docker/Dockerfile b/tests/docker/Dockerfile index c850545..46a8034 100755 --- a/tests/docker/Dockerfile +++ b/tests/docker/Dockerfile @@ -1,4 +1,8 @@ -FROM yiisoftware/yii2-php:8.0-apache +ARG BUILD_PHP_VERSION + +FROM yiisoftware/yii2-php:${BUILD_PHP_VERSION}-apache + +ARG BUILD_PHP_VERSION ENV INSIDE_DOCKER=1 From 2f53d08f138d35c847e92d25219dddefe12233b2 Mon Sep 17 00:00:00 2001 From: Sohel Ahmed Mesaniya Date: Tue, 11 Jun 2024 20:23:09 +0530 Subject: [PATCH 13/14] Modify docs --- CONTRIBUTING.md | 64 ++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 724d4a1..3c5e8b8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -23,49 +23,43 @@ make test ### Switching PHP versions +While development, you might need to support multiple versions of PHP. You can run tests locally in a Docker container in a different PHP version. You can switch the PHP version of the docker runtime by changing the `PHP_VERSION` environment variable in the `.env` file. If you have no `.env` file yet, create it by copying `.env.dist` to `.env`. -After changing the PHP Version you need to run `make clean_all up` to start the new container with new version. +After changing the PHP Version you need to run `make clean_all up cli` or `make down up cli` to start the new container with new version. Example: ``` -$ echo "PHP_VERSION=8.1" > .env -$ make clean_all up cli -Stopping yii2-openapi_php_1 ... done # TODO -Stopping yii2-openapi_maria_1 ... done -Stopping yii2-openapi_postgres_1 ... done -Stopping yii2-openapi_mysql_1 ... done -Removing yii2-openapi_php_1 ... done -Removing yii2-openapi_maria_1 ... done -Removing yii2-openapi_postgres_1 ... done -Removing yii2-openapi_mysql_1 ... done -Removing network yii2-openapi_default -Creating network "yii2-openapi_default" with driver "bridge" -Creating yii2-openapi_maria_1 ... done -Creating yii2-openapi_mysql_1 ... done -Creating yii2-openapi_postgres_1 ... done -Creating yii2-openapi_php_1 ... done +$ echo "PHP_VERSION=8.2" > .env +$ make down up cli +docker-compose down --remove-orphans +Stopping yii2-fractal_php_1 ... done +Stopping yii2-fractal_pgsql_1 ... done +Removing yii2-fractal_php_1 ... done +Removing yii2-fractal_pgsql_1 ... done +Removing network yii2-fractal_default +docker-compose build +... +... +... +docker-compose up -d +... +... +... docker-compose exec php bash - -root@f9928598f841:/app# php -v - -PHP 8.1.27 (cli) (built: Jan 26 2022 18:08:44) ( NTS ) -Copyright (c) The PHP Group -Zend Engine v3.4.0, Copyright (c) Zend Technologies -with Zend OPcache v8.1.27, Copyright (c), by Zend Technologies -with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans + _ _ __ _ + (_|_)/ _| | | + _ _ _ _| |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __ + | | | | | | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ / + | |_| | | | | | | | (_| | | | | | | __/\ V V / (_) | | | < + \__, |_|_|_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\ + __/ | + |___/ + +PHP version: 8.2.20 +root@713d31b5ed8c:/app# ``` - - - - - ### Testing # todo remove - - - Clone project - - Run `make up` - - Run once `make installdocker` - - Run `make testdocker` or `make cli` and inside docker env `make test` From f0d08c6f090c7af5e861207f3853423fa45ebb7a Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 12 Jun 2024 09:34:31 +0200 Subject: [PATCH 14/14] Apply suggestions from code review --- Makefile | 2 +- tests/docker/xdebug.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index d086e1a..240bea0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PHPARGS=-dmemory_limit=64M -#PHPARGS=-dmemory_limit=64M -dzend_extension=xdebug.so -dxdebug.mode=debug -dxdebug.remote_host=127.0.0.1 -dxdebug.start_with_request=yes +#PHPARGS=-dmemory_limit=64M -dzend_extension=xdebug.so -dxdebug.mode=debug -dxdebug.client_host=127.0.0.1 -dxdebug.start_with_request=yes #PHPARGS=-dmemory_limit=64M -dxdebug.mode=debug all: diff --git a/tests/docker/xdebug.ini b/tests/docker/xdebug.ini index d17be49..269b0f4 100755 --- a/tests/docker/xdebug.ini +++ b/tests/docker/xdebug.ini @@ -1,7 +1,7 @@ xdebug.mode=debug xdebug.start_with_request=yes xdebug.discover_client_host=1 -;xdebug.remote_host=host.docker.internal +;xdebug.client_host=host.docker.internal xdebug.client_port=9000 xdebug.idekey=PHPSTORM xdebug.connect_timeout_ms = 10