Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade local & Silta PHP to 7.4 & add PHPUnit testing setup. #122

Merged
merged 28 commits into from
Jun 7, 2021

Conversation

tormi
Copy link
Member

@tormi tormi commented Sep 1, 2020

This PR upgrades PHP to 7.4 and adds PHPUnit setup. Run lando phpunit --group admin_toolbar to test the PHPUnit setup.

This is the same PR as #121 with default PHP version upgraded to 7.4. See more at #121 (comment).

@tormi
Copy link
Member Author

tormi commented Sep 1, 2020

Should we upgrade PHP here as well: https://github.com/wunderio/drupal-project/blob/master/composer.json#L19 ?

@tormi
Copy link
Member Author

tormi commented Sep 11, 2020

@hkirsman
Copy link
Contributor

hkirsman commented Feb 8, 2021

Please test mysql vs SQLite. Tested with this:

lando phpunit web/core/modules/block/tests/src/Functional/BlockCacheTest.php

Beware to get coffe as it might take few minutes in Mac!

With the current setup commited (using SQLite):

Test 1: Time: 01:57.210, Memory: 10.00 MB
Test2: Time: 01:53.786, Memory: 10.00 MB

Then in in my local changed the SIMPLETEST_DB parameter in .lando.yml to use the MySQL connection:

services:
  appserver:
    overrides:
      environment:
        SIMPLETEST_DB: "mysql://drupal9:drupal9@database/drupal9"

Test 1: Time: 00:41.787, Memory: 10.00 MB
Test2: Time: 00:42.491, Memory: 10.00 MB

That's more than 2x speed increase for me. Is it only my setup?

There is supposed to be also in memory support for SQLLite but didn't get it working.

 SIMPLETEST_DB: "sqlite://localhost/:memory:"

https://www.drupal.org/project/drupal/issues/2983452

In my previous tests with other projects didn't see any speed increase either. Then again I wasn't testing kernel tests.

@tormi
Copy link
Member Author

tormi commented Mar 13, 2021

There are some failures with functional tests, cURL error 6: Could not resolve host: appserver_nginx:

➜  dp git:(feature/lando-phpunit-php-7.4) ✗ lando phpunit --group admin_toolbar
PHPUnit 9.5.2 by Sebastian Bergmann and contributors.

Testing 
EEEESSS                                                             7 / 7 (100%)

Time: 14:28.384, Memory: 927.00 MB

There were 4 errors:

1) Drupal\Tests\admin_toolbar\Functional\AdminToolbarAlterTest::testAdminToolbar
GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: appserver_nginx (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

@tormi
Copy link
Member Author

tormi commented Mar 13, 2021

@hkirsman, thanks :) lando/lando#2283 (comment)

tormi added a commit that referenced this pull request Mar 13, 2021
tormi added a commit that referenced this pull request Mar 13, 2021
@tormi tormi force-pushed the feature/lando-phpunit-php-7.4 branch from 206457c to 7d6f7de Compare March 13, 2021 19:50
@tormi
Copy link
Member Author

tormi commented Mar 13, 2021

Should we run some tests in CircleCI as well?

@tormi tormi changed the title Add PHPUnit testing setup for PHP 7.4. Upgrade local & Silta PHP to 7.4 & add PHPUnit testing setup. Mar 13, 2021
grumphp.yml Outdated Show resolved Hide resolved
@@ -30,6 +30,7 @@
},
"require-dev": {
"drupal/core-dev": "^9.1",
"phpspec/prophecy-phpunit": "^2.0",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"phpspec/prophecy-phpunit" comes from https://www.drupal.org/node/3176567.

- Add minified phpunit_example module from https://www.drupal.org/project/examples.
- Enable phpunit in grumphp.yml.
tormi added a commit that referenced this pull request May 25, 2021
@tormi tormi force-pushed the feature/lando-phpunit-php-7.4 branch from 48ffbde to 52433df Compare May 25, 2021 13:52
@tormi
Copy link
Member Author

tormi commented May 25, 2021

Looks like the problem with | (8658c52) is similar to https://stackoverflow.com/questions/66100477/symfony-5-2-3-interface-type-hinting

@tormi
Copy link
Member Author

tormi commented May 26, 2021

Looks like CI is using PHP 7.3 instead of 7.4 and that could affect the output:

Command: '/home/circleci/project/vendor/bin/phpcs' '--extensions=php,inc,module,install,theme' '--parallel=20' '--runtime-set' 'testVersion' '7.3' '--standard=PHPCompatibility' '--basepath=.' '-s' 'web/modules/custom' 'web/sites'

Full verbose log:

circleci@e27c7346d55c:~/project$ vendor/bin/grumphp run -vvvv
Config file: /home/circleci/project/grumphp.yml
Working dir: /home/circleci/project
Project dir: /home/circleci/project
GIT working dir: /home/circleci/project
GIT repository dir: /home/circleci/project/.git
Bin dir: /home/circleci/project/vendor/bin
Composer file: /home/circleci/project/composer.json
[2021-05-26T17:28:48.943219+00:00] GrumPHP.DEBUG: Repository created (git dir: "/home/circleci/project/.git", working dir: "/home/circleci/project") [] []
[2021-05-26T17:28:48.943858+00:00] GrumPHP.INFO: run command: ls-files "/home/circleci/project"  [] []
[2021-05-26T17:28:48.947529+00:00] GrumPHP.DEBUG: last command (ls-files) duration: 3.60ms [] []
[2021-05-26T17:28:48.947604+00:00] GrumPHP.DEBUG: last command (ls-files) return code: 0 [] []
[2021-05-26T17:28:48.947648+00:00] GrumPHP.DEBUG: last command (ls-files) output: .circleci/config.yml .dockerignore .env.example .eslintignore .eslintrc.json .github/dependabot.yml .gitignore .lando.yml .lando/.env .lando/my_elasticsearch.yml .lando/php.ini .lando/phpunit.sh .lando/xdebug.sh .prettierrc.json .stylelintrc.json .vscode/extensions.json .vscode/launch.json .vscode/settings.json LICENSE README.md composer.json drush/Commands/PolicyCommands.php drush/README.md drush/drush.yml drush/sites/self.site.yml grumphp.yml load.environment.php package-lock.json package.json phpcs.xml phpunit.xml scripts/composer/ScriptHandler.php silta/nginx.Dockerfile silta/php.Dockerfile silta/shell.Dockerfile silta/silta-prod.yml silta/silta.yml web/.dockerignore web/modules/custom/phpunit_example/phpunit_example.info.yml web/modules/custom/phpunit_example/phpunit_example.module web/modules/custom/phpunit_example/src/AddClass.php web/modules/custom/phpunit_example/tests/src/Unit/AddClassTest.php web/sites/default/settings.php  [] []
GrumPHP is sniffing your code!

Running tasks with priority 0!
==============================

Running task 1/9: php_compatibility...
Running task 2/9: check_file_permissions...
Running task 3/9: php_check_syntax...
Running task 4/9: phpcs...
Running task 5/9: php_stan...
Running task 6/9: ecs...
Running task 7/9: yaml_lint...
Running task 8/9: json_lint...
Running task 9/9: phpunit...


Command: '/home/circleci/project/vendor/bin/phpcs' '--extensions=php,inc,module,install,theme' '--parallel=20' '--runtime-set' 'testVersion' '7.3' '--standard=PHPCompatibility' '--basepath=.' '-s' 'web/modules/custom' 'web/sites'

Command: '/home/circleci/project/vendor/bin/phpcs' '--standard=phpcs.xml' '--extensions=php,inc,module,install,theme' '--report=full' '--report-width=120' '--parallel=20' '--ignore=/vendor/,/node_modules/,/core/,/libraries/' '--basepath=.' '-s' 'web/modules/custom'

Command: '/usr/local/bin/php' '-l' 'web/modules/custom/phpunit_example/phpunit_example.module'

Command: '/usr/local/bin/php' '-l' 'web/modules/custom/phpunit_example/src/AddClass.php'

Command: '/home/circleci/project/vendor/bin/ecs' 'check' 'web/modules/custom' '--config=vendor/wunderio/code-quality/config/ecs.php' '--no-progress-bar' '--ansi' '--no-interaction'

Command: '/usr/local/bin/php' '-l' 'web/modules/custom/phpunit_example/tests/src/Unit/AddClassTest.php'

Command: '/usr/local/bin/php' '-l' 'web/sites/default/settings.php'

Command: '/usr/local/bin/php' '-l' 'web/sites/default/default.settings.php'

Command: '/usr/local/bin/php' '-l' 'web/sites/example.settings.local.php'

Command: '/usr/local/bin/php' '-l' 'web/sites/example.sites.php'

Command: '/home/circleci/project/vendor/bin/phpstan' 'analyse' '--configuration=vendor/wunderio/code-quality/config/phpstan.neon' '--no-ansi' '--no-interaction' '--no-progress' 'web/modules/custom'

Command: '/home/circleci/project/vendor/bin/phpunit' '--testsuite=unit'
Aborted ...

ecs
===


 [ERROR] syntax error, unexpected '|', expecting variable (T_VARIABLE)

To skip commit checks, add -n or --no-verify flag to commit command

@tormi
Copy link
Member Author

tormi commented May 26, 2021

Yeah, it's defined in vendor/wunderio/code-quality/.circleci/config.yml:

version: 2
jobs:
  # Install all dependencies and run check tasks.
  build_and_test:
    docker:
      - image: circleci/php:7.3.9-cli-node

@tormi
Copy link
Member Author

tormi commented May 26, 2021

Also in here vendor/wunderio/code-quality/tests/PhpCompatibility/PhpCompatibilityTaskTest.php:

    $taskConfig->method('getOptions')->willReturn([
      'standard' => 'php-compatibility.xm',
      'testVersion' => '7.3',
      'extensions' => ['php'],
      'run_on' => ['.'],
      'ignore_patterns' => ['/vendor/'],
      'parallel' => 10,
    ]);

@tormi
Copy link
Member Author

tormi commented May 26, 2021

Changed the PHP version to 7.3 locally and rerun ecs task, no errors:

➜  drupal-project git:(feature/lando-phpunit-php-7.4) ✗ lando grumphp run --tasks=ecs                
GrumPHP is sniffing your code!

Running tasks with priority 0!
==============================

Running task 1/1: ecs... ✔
➜  drupal-project git:(feature/lando-phpunit-php-7.4) ✗ lando php -v
PHP 7.3.27 (cli) (built: Apr 10 2021 14:33:57) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.27, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.27, Copyright (c) 1999-2018, by Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans

@tormi
Copy link
Member Author

tormi commented May 26, 2021

And PHPUnit tests output locally with PHP 7.3:

➜  drupal-project git:(feature/lando-phpunit-php-7.4) ✗ lando phpunit '--testsuite=unit' 
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

Testing 
Add Class (Drupal\Tests\phpunit_example\Unit\AddClass)
 ✔ Add
 ✔ Add with data provider with data set 0
 ✔ Add with data provider with data set 1
 ✔ Add with bad data provider with data set 0
 ✔ Add with bad data provider with data set 1
 ✔ Add with bad data provider with data set 2
 ✔ Add with bad data provider with data set 3
 ✔ Add with bad data provider with data set 4
 ✔ Add with bad data provider with data set 5
 ✔ Add with bad data provider with data set 6
 ✔ Add with bad data provider with data set 7
 ✔ Add with bad data provider with data set 8
 ✔ Add with bad data provider with data set 9
 ✔ Add with bad data provider with data set 10
 ✔ Add with bad data provider with data set 11
 ✔ Add with bad data provider with data set 12
 ✔ Add with bad data provider with data set 13
 ✔ Add with bad data provider with data set 14
 ✔ Add with bad data provider with data set 15
 ✔ Add with bad data provider with data set 16
 ✔ Add with bad data provider with data set 17
 ✔ Add with bad data provider with data set 18
 ✔ Add with bad data provider with data set 19
 ✔ Add with bad data provider with data set 20
 ✔ Add with bad data provider with data set 21
 ✔ Add with bad data provider with data set 22
 ✔ Add with bad data provider with data set 23

Time: 00:00.091, Memory: 10.00 MB

OK (27 tests, 27 assertions)

@tormi
Copy link
Member Author

tormi commented May 26, 2021

Okay, unittest passes in CI and is using the correct PHP version:

circleci@e27c7346d55c:~/project$ 'vendor/bin/phpunit' '--testsuite=unit' -vvvv
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.4.12
Configuration: /home/circleci/project/phpunit.xml

Testing
...........................                                       27 / 27 (100%)

Time: 00:00.039, Memory: 12.00 MB

OK (27 tests, 27 assertions)

@tormi
Copy link
Member Author

tormi commented May 26, 2021

And ecs task alone in CI:

circleci@e27c7346d55c:~/project$ vendor/bin/grumphp run --tasks=ecs -vvvv
Config file: /home/circleci/project/grumphp.yml
Working dir: /home/circleci/project
Project dir: /home/circleci/project
GIT working dir: /home/circleci/project
GIT repository dir: /home/circleci/project/.git
Bin dir: /home/circleci/project/vendor/bin
Composer file: /home/circleci/project/composer.json
[2021-05-26T18:35:09.923989+00:00] GrumPHP.DEBUG: Repository created (git dir: "/home/circleci/project/.git", working dir: "/home/circleci/project") [] []
[2021-05-26T18:35:09.924966+00:00] GrumPHP.INFO: run command: ls-files "/home/circleci/project"  [] []
[2021-05-26T18:35:09.929283+00:00] GrumPHP.DEBUG: last command (ls-files) duration: 4.21ms [] []
[2021-05-26T18:35:09.929394+00:00] GrumPHP.DEBUG: last command (ls-files) return code: 0 [] []
[2021-05-26T18:35:09.929465+00:00] GrumPHP.DEBUG: last command (ls-files) output: .circleci/config.yml .dockerignore .env.example .eslintignore .eslintrc.json .github/dependabot.yml .gitignore .lando.yml .lando/.env .lando/my_elasticsearch.yml .lando/php.ini .lando/phpunit.sh .lando/xdebug.sh .prettierrc.json .stylelintrc.json .vscode/extensions.json .vscode/launch.json .vscode/settings.json LICENSE README.md composer.json drush/Commands/PolicyCommands.php drush/README.md drush/drush.yml drush/sites/self.site.yml grumphp.yml load.environment.php package-lock.json package.json phpcs.xml phpunit.xml scripts/composer/ScriptHandler.php silta/nginx.Dockerfile silta/php.Dockerfile silta/shell.Dockerfile silta/silta-prod.yml silta/silta.yml web/.dockerignore web/modules/custom/phpunit_example/phpunit_example.info.yml web/modules/custom/phpunit_example/phpunit_example.module web/modules/custom/phpunit_example/src/AddClass.php web/modules/custom/phpunit_example/tests/src/Unit/AddClassTest.php web/sites/default/settings.php  [] []
GrumPHP is sniffing your code!

Running tasks with priority 0!
==============================

Running task 1/1: ecs...


Command: '/home/circleci/project/vendor/bin/ecs' 'check' 'web/modules/custom' '--config=vendor/wunderio/code-quality/config/ecs.php' '--no-progress-bar' '--ansi' '--no-interaction'
Running task 1/1: ecs... ✘


ecs
===


 [ERROR] syntax error, unexpected '|', expecting variable (T_VARIABLE)

To skip commit checks, add -n or --no-verify flag to commit command

@tormi
Copy link
Member Author

tormi commented May 26, 2021

Seems like there's something wrong with ecs setup itself in CI:

circleci@e27c7346d55c:~/project$ vendor/bin/ecs -vvv

 [ERROR] syntax error, unexpected '|', expecting variable (T_VARIABLE)

@tormi
Copy link
Member Author

tormi commented May 26, 2021

Successfully tried out psalm:

www-data@3c731425bb5d:/app$ vendor/bin/psalm      
Scanning files...
Analyzing files...

EE

ERROR: DocblockTypeContradiction - web/modules/custom/phpunit_example/src/AddClass.php:29:12 - Docblock-defined type float|int for $argument is always numeric (see https://psalm.dev/155)
      if (!is_numeric($argument)) {


ERROR: UndefinedClass - web/modules/custom/phpunit_example/tests/src/Unit/AddClassTest.php:36:28 - Class, interface or enum named Drupal\Tests\UnitTestCase does not exist (see https://psalm.dev/019)
class AddClassTest extends UnitTestCase {


------------------------------
2 errors found
------------------------------

Checks took 20.54 seconds and used 304.836MB of memory
Psalm was able to infer types for 100% of the codebase

@guncha25
Copy link
Contributor

guncha25 commented Jun 6, 2021

@tormi @hkirsman Removed ECS and tested code complexity tests with phpstan default settings. Works as discussed. This is ready to merge.

Copy link
Contributor

@guncha25 guncha25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works as expected. Ready for merge.

@tormi tormi merged commit 6d5a9fb into master Jun 7, 2021
@tormi tormi deleted the feature/lando-phpunit-php-7.4 branch June 7, 2021 01:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants