Skip to content

Commit bfb4774

Browse files
authored
Merge pull request #5 from php-openapi/4-fix-ci-to-work-with-php-74-and-8x
Resolve: Multiple issues
2 parents 3eab1fc + f0d08c6 commit bfb4774

24 files changed

+160
-58
lines changed

.env.dist

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PHP_VERSION=8.0

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
strategy:
3333
fail-fast: false
3434
matrix:
35-
php-versions: ['7.4', '8.1', '8.2', '8.3']
35+
php-versions: ['8.0', '8.1', '8.2', '8.3']
3636

3737
steps:
3838
- uses: actions/checkout@v2

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
/composer.lock
33
/phpunit.xml
44
/.php_cs.cache
5+
6+
/.env
7+
/.phpunit.result.cache

CONTRIBUTING.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Contribute to yii2-fractal library
2+
==================================
3+
4+
### Local setup
5+
6+
[Docker](https://docs.docker.com/engine/install/) is required
7+
8+
```bash
9+
git clone [email protected]:php-openapi/yii2-fractal.git
10+
cd yii2-fractal
11+
make up
12+
make installdocker # only once during initiation
13+
```
14+
15+
### Test
16+
17+
Ensure you followed steps mentioned in Local setup, then
18+
19+
```bash
20+
make cli
21+
make test
22+
```
23+
24+
### Switching PHP versions
25+
26+
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.
27+
28+
You can switch the PHP version of the docker runtime by changing the `PHP_VERSION` environment variable in the `.env` file.
29+
30+
If you have no `.env` file yet, create it by copying `.env.dist` to `.env`.
31+
32+
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.
33+
34+
Example:
35+
36+
```
37+
$ echo "PHP_VERSION=8.2" > .env
38+
$ make down up cli
39+
docker-compose down --remove-orphans
40+
Stopping yii2-fractal_php_1 ... done
41+
Stopping yii2-fractal_pgsql_1 ... done
42+
Removing yii2-fractal_php_1 ... done
43+
Removing yii2-fractal_pgsql_1 ... done
44+
Removing network yii2-fractal_default
45+
docker-compose build
46+
...
47+
...
48+
...
49+
docker-compose up -d
50+
...
51+
...
52+
...
53+
docker-compose exec php bash
54+
_ _ __ _
55+
(_|_)/ _| | |
56+
_ _ _ _| |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
57+
| | | | | | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
58+
| |_| | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
59+
\__, |_|_|_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
60+
__/ |
61+
|___/
62+
63+
PHP version: 8.2.20
64+
root@713d31b5ed8c:/app#
65+
```

Makefile

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PHPARGS=-dmemory_limit=64M
2-
#PHPARGS=-dmemory_limit=64M -dzend_extension=xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_autostart=1
3-
#PHPARGS=-dmemory_limit=64M -dxdebug.remote_enable=1
2+
#PHPARGS=-dmemory_limit=64M -dzend_extension=xdebug.so -dxdebug.mode=debug -dxdebug.client_host=127.0.0.1 -dxdebug.start_with_request=yes
3+
#PHPARGS=-dmemory_limit=64M -dxdebug.mode=debug
44

55
all:
66

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

2121
clean_all:
22-
docker-compose down
22+
docker-compose down --remove-orphans
2323
sudo rm -rf tests/tmp/*
2424

2525
up:
26+
docker-compose build
2627
docker-compose up -d
27-
chmod +rw -R tests/tmp
28-
chmod +rw -R tests/codeception
29-
mkdir -p tests/testapp/runtime && chmod +rw -R tests/testapp/runtime
28+
docker-compose run --rm php bash -c 'chmod +rw -R tests/tmp'
29+
docker-compose run --rm php bash -c 'chmod +rw -R tests/codeception'
30+
docker-compose run --rm php bash -c 'mkdir -p tests/testapp/runtime && chmod +rw -R tests/testapp/runtime'
31+
docker-compose run --rm php bash -c 'chmod -R 777 tests/testapp/runtime' # TODO avoid 777
32+
33+
down:
34+
docker-compose down --remove-orphans
3035

3136
cli:
3237
docker-compose exec php bash

README.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![yii2-fractal](https://github.com/php-openapi/yii2-fractal/workflows/yii2-fractal/badge.svg)
44

5-
The set of utils and actions for prepare Rest API accordingly JSON:Api https://jsonapi.org/format/
5+
The set of utils and actions for prepare Rest API accordingly JSON:API https://jsonapi.org/format/
66
With https://fractal.thephpleague.com
77

88
### Installation
@@ -21,10 +21,13 @@ With https://fractal.thephpleague.com
2121

2222
['bootstrap' section]: https://www.yiiframework.com/doc/guide/2.0/en/runtime-bootstrapping
2323

24-
### Testing
25-
26-
- Clone project
27-
- Run `make up`
28-
- Run once `make installdocker`
29-
- Run `make testdocker` or `make cli` and inside docker env `make test`
30-
24+
25+
26+
### Contributing / Local Development / Testing
27+
28+
See [CONTRIBUTING.md](CONTRIBUTING.md) file
29+
30+
31+
### License
32+
33+
See [LICENSE](LICENSE) file

composer.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "library",
1111
"license": "MIT",
1212
"require": {
13-
"php": ">=7.4.0",
13+
"php": ">=8",
1414
"ext-json": "*",
1515
"league/fractal": "^0.20.0",
1616
"yiisoft/yii2": "~2.0.15"
@@ -19,13 +19,13 @@
1919
"require-dev": {
2020
"cebe/indent": "*",
2121
"friendsofphp/php-cs-fixer": "~2.16",
22-
"codeception/codeception": "^4.1 | ^5.0",
22+
"codeception/codeception": "5.1.*",
2323
"codeception/module-yii2": "^1.1",
24-
"codeception/module-rest": "^2.0 | ^3.0",
25-
"codeception/module-phpbrowser": "^1.0 | ^2.0",
26-
"codeception/module-asserts": "^1.0 | ^2.0 | ^3.0",
24+
"codeception/module-rest": "^3.0",
25+
"codeception/module-phpbrowser": "3.0.*",
26+
"codeception/module-asserts": "^3.0",
2727
"fzaninotto/faker": "^1.9@dev",
28-
"codeception/module-db": "^1.0 | ^2.0 | ^3.0",
28+
"codeception/module-db": "^3.0",
2929
"codeception/assert-throws": "^1.0",
3030
"codeception/verify": "^1.5"
3131
},

docker-compose.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
version: "3.5"
22
services:
33
php:
4-
build: tests/docker
4+
build:
5+
context: tests/docker
6+
args:
7+
- BUILD_PHP_VERSION=${PHP_VERSION}
58
volumes:
69
- ./tests/tmp/.composer:/root/.composer:rw
710
- .:/app

src/JsonApiErrorHandler.php

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class JsonApiErrorHandler extends ErrorHandler
7070

7171
protected function renderException($exception)
7272
{
73+
Yii::error($exception);
7374
if (Yii::$app->has('response')) {
7475
$response = Yii::$app->getResponse();
7576
// reset parameters of response to avoid interference with partially created response data

src/actions/ListAction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function makeDataProvider()
148148
$query->andWhere($filter);
149149
}
150150

151-
$dataProvider = Yii::createObject($this->dataProvider);
151+
$dataProvider = Yii::createObject($this->dataProvider, ['query' => $query]);
152152
if (!$dataProvider instanceof JsonApiActiveDataProvider && !$dataProvider instanceof CursorActiveDataProvider) {
153153
throw new InvalidConfigException('Invalid dataProvider configuration');
154154
}

src/actions/ListForIdentityAction.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ protected function prepareDataFilter($requestParams)
113113
protected function prepareParentQuery(ActiveQueryInterface $query):ActiveQueryInterface
114114
{
115115
$userId = Yii::$app->user->id;
116+
$condition = [];
116117
$condition[$this->modelTable().'.'.$this->userIdAttribute] = $userId;
117118
$query->where($condition);
118119
return $query;
@@ -142,7 +143,7 @@ protected function makeDataProvider()
142143
$query->andWhere($filter);
143144
}
144145

145-
$dataProvider = Yii::createObject($this->dataProvider);
146+
$dataProvider = Yii::createObject($this->dataProvider, ['query' => $query]);
146147
if (!$dataProvider instanceof JsonApiActiveDataProvider && !$dataProvider instanceof CursorActiveDataProvider) {
147148
throw new InvalidConfigException('Invalid dataProvider configuration');
148149
}

src/providers/JsonApiActiveDataProvider.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ public function setPagination($value):void
6363
if (!$this->_pagination instanceof JsonApiPaginator) {
6464
throw new InvalidArgumentException('Only JsonApiPaginator instance or false allowed');
6565
}
66-
} elseif ($value instanceof JsonApiPaginator || $value === false) {
66+
$this->_pagination->totalCount = $this->getTotalCount();
67+
} elseif ($value instanceof JsonApiPaginator) {
6768
$this->_pagination = $value;
69+
$this->_pagination->totalCount = $this->getTotalCount();
70+
} elseif ($value === false) {
71+
$this->_pagination = false;
6872
} else {
6973
throw new InvalidArgumentException('Only JsonApiPaginator instance, configuration array or false is allowed.');
7074
}

tests/codeception/suites/api/ApiCategoryCest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function testListActionWithSort(ApiTester $I)
151151
]
152152
]);
153153
$selfLink = $I->grabDataFromResponseByJsonPath('$.links.self');
154-
$I->assertContains('sort=-name', $selfLink[0]);
154+
$I->assertStringContainsString('sort=-name', $selfLink[0]);
155155
}
156156

157157
public function testCreateInvalidFormat1(ApiTester $I)

tests/codeception/suites/api/ApiDefaultCest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public function testActionWithInternalError(ApiTester $I)
100100
$I->seeResponseCodeIsServerError();
101101
$I->seeResponseIsJsonApiError();
102102
$I->seeResponseHasMeta();
103-
$I->seeResponseContainsJson(['title' => 'PHP Warning', 'detail' => 'json_decode() expects parameter 1 to be string, array given']);
104-
$I->seeResponseContainsJson(['meta' => ['error_type' => "yii\\base\\ErrorException"]]);
103+
// $I->seeResponseContainsJson(['title' => 'PHP Warning', 'detail' => 'json_decode() expects parameter 1 to be string, array given']);
104+
// $I->seeResponseContainsJson(['meta' => ['error_type' => "yii\\base\\ErrorException"]]);
105+
$I->seeResponseContainsJson(['meta' => ['error_type' => "TypeError"]]);
105106
}
106-
}
107+
}

tests/codeception/suites/api/ApiPostCest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ public function testCountPostsAction(ApiTester $I)
88
$I->amBearerAuthenticated('Delta_secret_token');
99
$I->sendHEAD('/post/count');
1010
$I->seeResponseCodeIs(204);
11-
$I->seeHttpHeader('X-Pagination-Total-Count', 54);
11+
$I->seeHttpHeader('X-Pagination-Total-Count', '54');
1212

1313
$I->sendHEAD('/post/count', ['filter'=>['category_id'=>['neq'=>3]]]);
1414
$I->seeResponseCodeIs(204);
15-
$I->seeHttpHeader('X-Pagination-Total-Count', 36);
15+
$I->seeHttpHeader('X-Pagination-Total-Count', '36');
1616
}
1717

1818
public function testCountPostsForCategoryAction(ApiTester $I)
@@ -21,10 +21,10 @@ public function testCountPostsForCategoryAction(ApiTester $I)
2121
$I->amBearerAuthenticated('Delta_secret_token');
2222
$I->sendHEAD('/categories/1/posts-count');
2323
$I->seeResponseCodeIs(204);
24-
$I->seeHttpHeader('X-Pagination-Total-Count', 18);
24+
$I->seeHttpHeader('X-Pagination-Total-Count', '18');
2525
$I->sendHEAD('/categories/2/posts-count');
2626
$I->seeResponseCodeIs(204);
27-
$I->seeHttpHeader('X-Pagination-Total-Count', 0);
27+
$I->seeHttpHeader('X-Pagination-Total-Count', '0');
2828
}
2929

3030
public function testListHasDataWithRelationships(ApiTester $I)
@@ -53,10 +53,10 @@ public function testListHasDataWithRelationships(ApiTester $I)
5353

5454
$I->sendHEAD('/post');
5555
$I->seeResponseCodeIs(200);
56-
$I->seeHttpHeader('X-Pagination-Total-Count', 54);
57-
$I->seeHttpHeader('X-Pagination-Current-Page', 1);
58-
$I->seeHttpHeader('X-Pagination-Per-Page', 20);
59-
$I->seeHttpHeader('X-Pagination-Page-Count', 3);
56+
$I->seeHttpHeader('X-Pagination-Total-Count', '54');
57+
$I->seeHttpHeader('X-Pagination-Current-Page', '1');
58+
$I->seeHttpHeader('X-Pagination-Per-Page', '20');
59+
$I->seeHttpHeader('X-Pagination-Page-Count', '3');
6060
}
6161

6262
public function testListFilterWithNotAllowedAttrs(ApiTester $I)

tests/codeception/suites/api/ApiUserCest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function testPostsCountAction(ApiTester $I)
122122
$I->amBearerAuthenticated('Gamma_secret_token');
123123
$I->sendHEAD('/me/posts-count');
124124
$I->seeResponseCodeIsSuccessful();
125-
$I->seeHttpHeader('X-Pagination-Total-Count', 18);
125+
$I->seeHttpHeader('X-Pagination-Total-Count', '18');
126126
}
127127

128128
}

tests/docker/Dockerfile

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
FROM yiisoftware/yii2-php:7.1-apache
1+
ARG BUILD_PHP_VERSION
2+
3+
FROM yiisoftware/yii2-php:${BUILD_PHP_VERSION}-apache
4+
5+
ARG BUILD_PHP_VERSION
26

37
ENV INSIDE_DOCKER=1
48

59
COPY apache.conf /etc/apache2/sites-enabled/000-default.conf
610

711
# enable xdebug
812
RUN docker-php-ext-enable xdebug
9-
COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
13+
COPY xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini

tests/docker/xdebug.ini

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
xdebug.remote_enable=1
2-
xdebug.remote_autostart=1
3-
xdebug.remote_connect_back=1
4-
;xdebug.remote_host=host.docker.internal
5-
xdebug.remote_port=9000
1+
xdebug.mode=debug
2+
xdebug.start_with_request=yes
3+
xdebug.discover_client_host=1
4+
;xdebug.client_host=host.docker.internal
5+
xdebug.client_port=9000
66
xdebug.idekey=PHPSTORM
7-
xdebug.remote_timeout = 10
7+
xdebug.connect_timeout_ms = 10
8+
;https://stackoverflow.com/a/68605372
9+
xdebug.log_level=0

tests/testapp/config/api.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@
5858
'validationErrorFormat' => JsonApiErrorHandler::ERROR_FORMAT_SPEC
5959
],
6060
'log' => [
61-
'traceLevel' => YII_DEBUG ? 3 : 0,
61+
'traceLevel' => 3,
6262
'targets' => [
6363
[
6464
'class' => yii\log\FileTarget::class,
65-
'levels' => ['error', 'warning'],
66-
'logFile' => '@runtime/error.log',
65+
'logFile' => '/tmp/yii2-fractal-test-error.log',
6766
'logVars' => ['_GET', '_POST']
6867
],
6968
],
@@ -127,4 +126,4 @@
127126
],
128127
],
129128
],
130-
];
129+
];

tests/testapp/config/console.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,15 @@
2424
'password' => 'dbpass',
2525
'charset' => 'utf8',
2626
],
27+
'log' => [
28+
'traceLevel' => 3,
29+
'targets' => [
30+
[
31+
'class' => yii\log\FileTarget::class,
32+
'logFile' => '/tmp/yii2-fractal-test-error.log',
33+
'logVars' => ['_GET', '_POST']
34+
],
35+
],
36+
],
2737
],
28-
];
38+
];

tests/testapp/transformers/CommentTransformer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class CommentTransformer extends TransformerAbstract
99
{
10-
public $availableIncludes = ['post'];
10+
public array $availableIncludes = ['post'];
1111

1212
public function transform(Comment $comment):array
1313
{

0 commit comments

Comments
 (0)