Skip to content

Commit

Permalink
Merge pull request #5 from php-openapi/4-fix-ci-to-work-with-php-74-a…
Browse files Browse the repository at this point in the history
…nd-8x

Resolve: Multiple issues
  • Loading branch information
SOHELAHMED7 authored Jun 14, 2024
2 parents 3eab1fc + f0d08c6 commit bfb4774
Show file tree
Hide file tree
Showing 24 changed files with 160 additions and 58 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PHP_VERSION=8.0
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/composer.lock
/phpunit.xml
/.php_cs.cache

/.env
/.phpunit.result.cache
65 changes: 65 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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

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 cli` or `make down up cli` to start the new container with new version.

Example:

```
$ 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
_ _ __ _
(_|_)/ _| | |
_ _ _ _| |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
| | | | | | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
| |_| | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
\__, |_|_|_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
__/ |
|___/
PHP version: 8.2.20
root@713d31b5ed8c:/app#
```
17 changes: 11 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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.client_host=127.0.0.1 -dxdebug.start_with_request=yes
#PHPARGS=-dmemory_limit=64M -dxdebug.mode=debug

all:

Expand All @@ -19,14 +19,19 @@ test:
php $(PHPARGS) vendor/bin/codecept run

clean_all:
docker-compose down
docker-compose down --remove-orphans
sudo rm -rf tests/tmp/*

up:
docker-compose build
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'
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
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,10 +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
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
},
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions src/JsonApiErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/actions/ListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
3 changes: 2 additions & 1 deletion src/actions/ListForIdentityAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -142,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');
}
Expand Down
6 changes: 5 additions & 1 deletion src/providers/JsonApiActiveDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ public function setPagination($value):void
if (!$this->_pagination instanceof JsonApiPaginator) {
throw new InvalidArgumentException('Only JsonApiPaginator instance or false allowed');
}
} elseif ($value instanceof JsonApiPaginator || $value === false) {
$this->_pagination->totalCount = $this->getTotalCount();
} elseif ($value instanceof JsonApiPaginator) {
$this->_pagination = $value;
$this->_pagination->totalCount = $this->getTotalCount();
} elseif ($value === false) {
$this->_pagination = false;
} else {
throw new InvalidArgumentException('Only JsonApiPaginator instance, configuration array or false is allowed.');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/codeception/suites/api/ApiCategoryCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions tests/codeception/suites/api/ApiDefaultCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]);
}
}
}
16 changes: 8 additions & 8 deletions tests/codeception/suites/api/ApiPostCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/codeception/suites/api/ApiUserCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
8 changes: 6 additions & 2 deletions tests/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FROM yiisoftware/yii2-php:7.1-apache
ARG BUILD_PHP_VERSION

FROM yiisoftware/yii2-php:${BUILD_PHP_VERSION}-apache

ARG BUILD_PHP_VERSION

ENV INSIDE_DOCKER=1

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
COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
14 changes: 8 additions & 6 deletions tests/docker/xdebug.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=1
;xdebug.remote_host=host.docker.internal
xdebug.remote_port=9000
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.discover_client_host=1
;xdebug.client_host=host.docker.internal
xdebug.client_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_timeout = 10
xdebug.connect_timeout_ms = 10
;https://stackoverflow.com/a/68605372
xdebug.log_level=0
7 changes: 3 additions & 4 deletions tests/testapp/config/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@
'validationErrorFormat' => JsonApiErrorHandler::ERROR_FORMAT_SPEC
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'traceLevel' => 3,
'targets' => [
[
'class' => yii\log\FileTarget::class,
'levels' => ['error', 'warning'],
'logFile' => '@runtime/error.log',
'logFile' => '/tmp/yii2-fractal-test-error.log',
'logVars' => ['_GET', '_POST']
],
],
Expand Down Expand Up @@ -127,4 +126,4 @@
],
],
],
];
];
12 changes: 11 additions & 1 deletion tests/testapp/config/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
],
],
],
],
];
];
2 changes: 1 addition & 1 deletion tests/testapp/transformers/CommentTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class CommentTransformer extends TransformerAbstract
{
public $availableIncludes = ['post'];
public array $availableIncludes = ['post'];

public function transform(Comment $comment):array
{
Expand Down
Loading

0 comments on commit bfb4774

Please sign in to comment.