From aab155f225b67381cd9ba5f684114a737f37a459 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Mon, 21 Dec 2020 23:16:04 +0100 Subject: [PATCH] GitHub actions (#2) Use Github Actions for CI pipeline --- .github/workflows/main.yaml | 119 ++++++++++++++++++++++++++++ .phpcs.xml.dist | 3 + .travis.yml | 71 ----------------- composer.json | 19 ++--- phpunit.xml.dist | 11 +-- unitTests/bootstrap.php | 2 + unitTests/classes/SpyMasterTest.php | 40 +++++----- unitTests/classes/SpyTest.php | 52 ++++++------ unitTests/phpunit.xml | 35 -------- 9 files changed, 185 insertions(+), 167 deletions(-) create mode 100644 .github/workflows/main.yaml delete mode 100644 .travis.yml delete mode 100644 unitTests/phpunit.xml diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml new file mode 100644 index 0000000..9d47ea3 --- /dev/null +++ b/.github/workflows/main.yaml @@ -0,0 +1,119 @@ +name: main +on: [ push, pull_request ] +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + php-version: + - '7.0' + - '7.1' + - '7.2' + - '7.3' + - '7.4' + - '8.0' + - '8.1' + + name: PHP ${{ matrix.php-version }} + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP, with composer and extensions + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib + coverage: none + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache composer dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Set composer flags + id: composer-lock + if: ${{ matrix.php-version == '8.0' || matrix.php-version == '8.1' }} + run: | + echo "::set-output name=flags::--ignore-platform-reqs" + + - name: Install dependencies + run: composer install --no-progress --prefer-dist --optimize-autoloader ${{ steps.composer-lock.outputs.flags }} + + - name: Setup problem matchers for PHP + run: echo "::add-matcher::${{ runner.tool_cache }}/php.json" + + - name: Setup problem matchers for PHPUnit + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Test with PHPUnit + run: ./vendor/bin/phpunit + + phpcs: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP, with composer and extensions + uses: shivammathur/setup-php@v2 + with: + php-version: 7.4 + extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib + coverage: none + tools: cs2pr + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache composer dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer install --no-progress --prefer-dist --optimize-autoloader + + - name: Code style with PHP_CodeSniffer + run: ./vendor/bin/phpcs -q --report=checkstyle | cs2pr --graceful-warnings --colorize + + coverage: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP, with composer and extensions + uses: shivammathur/setup-php@v2 + with: + php-version: 7.4 + extensions: ctype, dom, gd, iconv, fileinfo, libxml, mbstring, simplexml, xml, xmlreader, xmlwriter, zip, zlib + coverage: pcov + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache composer dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer install --no-progress --prefer-dist --optimize-autoloader + + - name: Coverage + run: | + ./vendor/bin/phpunit --coverage-text diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 2a43dcf..c04c2a9 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -14,6 +14,9 @@ /vendor/* + /examples/sampleClasses/* + /unitTests/data/* + /unitTests/bootstrap.php diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 56349e4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,71 +0,0 @@ -sudo: false - -dist: trusty - -cache: - apt: true - -language: php - -## Cache composer downloads. -cache: - directories: - # Cache directory for older Composer versions. - - $HOME/.composer/cache/files - # Cache directory for more recent Composer versions. - - $HOME/.cache/composer/files - -php: - - 7.1 - - 7.4snapshot - - nightly - -matrix: - fast_finish: true - include: - - php: 7.2 - env: EXTRA_CHECKS=1 - - php: 7.3 - env: COVERAGE=1 - - php: 7.0 - env: COVERAGE=1 - allow_failures: - - php: nightly - -before_install: - # Speed up build time by disabling Xdebug when its not needed. - - if [[ $COVERAGE != "1" ]]; then phpenv config-rm xdebug.ini || echo 'No xdebug config.'; fi - ## Packages - - sudo apt-get -qq update - -before_script: - ## Composer - - composer self-update - - composer install --prefer-dist - ## PHPDocumentor - ## - mkdir -p build/docs - ## - mkdir -p build/coverage - -script: - # Search for PHP syntax errors. - - find -L . -path ./vendor -prune -o -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l - ## PHPUnit - - if [[ "$COVERAGE" == "1" && $TRAVIS_PHP_VERSION == "7.2" ]]; then phpunit -c ./unitTests/ --coverage-text --coverage-html ./build/coverage; fi - - if [[ "$COVERAGE" == "1" && $TRAVIS_PHP_VERSION != "7.2" ]]; then ./vendor/bin/phpunit -c ./unitTests/ --coverage-text --coverage-html ./build/coverage; fi - - if [[ "$COVERAGE" != "1" ]]; then ./vendor/bin/phpunit -c ./unitTests/; fi - # Validate the composer.json file. - # @link https://getcomposer.org/doc/03-cli.md#validate - - composer validate --no-check-all - ## PHP_CodeSniffer - - if [[ "$EXTRA_CHECKS" == "1" ]]; then ./vendor/bin/phpcs --runtime-set ignore_warnings_on_exit 1 ./classes/src; fi - ## PHP Copy/Paste Detector - # - if [[ "$EXTRA_CHECKS" == "1" ]]; then ./vendor/bin/phpcpd ./classes/src/; fi - ## PHP Mess Detector - - if [[ "$EXTRA_CHECKS" == "1" ]]; then ./vendor/bin/phpmd ./classes/src/ text ./phpmd.xml.dist; fi - ## PHPLOC - - if [[ "$EXTRA_CHECKS" == "1" ]]; then ./vendor/bin/phploc ./classes/src/; fi - ## PHPDocumentor - ## - if [[ "$EXTRA_CHECKS" == "1" ]]; then ./vendor/bin/phpdoc -q -d ./classes/src -t ./build/docs --template="responsive-twig"; fi - -notifications: - email: false diff --git a/composer.json b/composer.json index 31c9583..c36d19f 100644 --- a/composer.json +++ b/composer.json @@ -12,17 +12,18 @@ } ], "require": { - "php": "^7.0.0" + "php": "^7.0 || ^8.0" }, "require-dev": { - "squizlabs/php_codesniffer": "3.3.2", - "phpmd/phpmd": "2.6.0", - "phpunit/phpunit": "^5.4.0", + "squizlabs/php_codesniffer": "^3.4", + "phpmd/phpmd": "2.*", + "phpunit/phpunit": "^6.0 || ^7.0 || ^8.0 || ^9.3", "phpdocumentor/phpdocumentor":"2.*", - "sebastian/phpcpd": "2.*", - "phploc/phploc": "2.*", - "phpcompatibility/php-compatibility": "^8.0", - "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3" + "sebastian/phpcpd": "^3.0 || ^4.0 || ^6.0", + "phploc/phploc": "^4.0", + "phpcompatibility/php-compatibility": "^9.0", + "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", + "yoast/phpunit-polyfills": "1.x-dev" }, "autoload": { "psr-4": { @@ -47,4 +48,4 @@ ] }, "minimum-stability": "dev" -} \ No newline at end of file +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index da5c530..a3f8d79 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,9 +6,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - syntaxCheck="true" - verbose="true" - strict="true" + verbose="true" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" @@ -21,15 +19,12 @@ - ../unitTests/classes + ./classes/src - - - diff --git a/unitTests/bootstrap.php b/unitTests/bootstrap.php index b1e0102..e97e69a 100644 --- a/unitTests/bootstrap.php +++ b/unitTests/bootstrap.php @@ -33,6 +33,8 @@ get_include_path(), ))); +require_once('./vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php'); + /** * @todo Sort out xdebug in vagrant so that this works in all sandboxes * For now, it is safer to test for it rather then remove it. diff --git a/unitTests/classes/SpyMasterTest.php b/unitTests/classes/SpyMasterTest.php index 57a851d..6723f44 100644 --- a/unitTests/classes/SpyMasterTest.php +++ b/unitTests/classes/SpyMasterTest.php @@ -2,15 +2,22 @@ namespace SpyMaster; +use Yoast\PHPUnitPolyfills\TestCases\XTestCase; + include APPLICATION_DATA_PATH . '/testClassForSpyMaster.php'; -class SpyMasterTest extends \PHPUnit\Framework\TestCase +class SpyMasterTest extends XTestCase { protected $targetObject; - protected function setUp() + /** + * @before + */ + public function setUpFixtures() { + parent::setUpFixtures(); + $this->targetObject = new \testing\testClassForSpyMaster(); } @@ -22,30 +29,27 @@ public function testInstantiate() self::assertInstanceOf('SpyMaster\\SpyMaster', $spyMaster); } - /** - * @expectedException Exception - * @expectedExceptionMessage You must specify the object that you want to spy on - */ public function testInstantiateWithoutObject() { + self::expectException(Exception::class); + self::expectExceptionMessage('You must specify the object that you want to spy on'); + $spyMaster = new SpyMaster(); } - /** - * @expectedException Exception - * @expectedExceptionMessage Argument must be an object - */ public function testInstantiateWithNonObject() { + self::expectException(Exception::class); + self::expectExceptionMessage('Argument must be an object'); + $spyMaster = new SpyMaster(true); } - /** - * @expectedException Exception - * @expectedExceptionMessage SpyMaster is unable to access PHP internal classes - */ public function testInstantiateWithInternalObject() { + self::expectException(Exception::class); + self::expectExceptionMessage('SpyMaster is unable to access PHP internal classes'); + $dto = new \DateTime(); $spyMaster = new SpyMaster($dto); @@ -71,15 +75,13 @@ public function testInfiltrateReadWriteSpy() self::assertInstanceOf('SpyMaster\\Spy', $spy); } - /** - * @expectedException Exception - * @expectedExceptionMessage SpyMaster does not support Saboteur spies - */ public function testInfiltrateInvalidSpy() { + self::expectException(Exception::class); + self::expectExceptionMessage('SpyMaster does not support Saboteur spies'); + $spyMaster = new SpyMaster($this->targetObject); $spy = $spyMaster->infiltrate('Saboteur'); } - } diff --git a/unitTests/classes/SpyTest.php b/unitTests/classes/SpyTest.php index 22a1a94..ea9c851 100644 --- a/unitTests/classes/SpyTest.php +++ b/unitTests/classes/SpyTest.php @@ -2,15 +2,22 @@ namespace SpyMaster; +use Yoast\PHPUnitPolyfills\TestCases\XTestCase; + include APPLICATION_DATA_PATH . '/testClassForSpy.php'; -class SpyTest extends \PHPUnit\Framework\TestCase +class SpyTest extends XTestCase { protected $targetObject; - protected function setUp() + /** + * @before + */ + public function setUpFixtures() { + parent::setUpFixtures(); + $this->targetObject = new \testing\testClassForSpy(); } @@ -62,16 +69,15 @@ public function testSpyGetPrivateProperty() $this->assertEquals($privatePropertyValue, 'ElePHPant'); } - /** - * @expectedException Exception - * @expectedExceptionMessage Property nonExistent does not exist - */ public function testSpyGetNonExistentProperty() { + self::expectException(Exception::class); + self::expectExceptionMessage('Property nonExistentProperty does not exist'); + $spyMaster = new SpyMaster($this->targetObject); $spy = $spyMaster->infiltrate(); - $privatePropertyValue = $spy->nonExistent; + $privatePropertyValue = $spy->nonExistentProperty; } public function testSpyPropertyIsSet() @@ -85,36 +91,33 @@ public function testSpyPropertyIsSet() $this->assertFalse($nonExistentPropertyExists); } - /** - * @expectedException Exception - * @expectedExceptionMessage Spies are not permitted to unset properties - */ public function testSpyPropertyUnset() { + self::expectException(Exception::class); + self::expectExceptionMessage('Spies are not permitted to unset properties'); + $spyMaster = new SpyMaster($this->targetObject); $spy = $spyMaster->infiltrate(); unset($spy->private); } - /** - * @expectedException Exception - * @expectedExceptionMessage Property nonExistent does not exist - */ public function testSpyNonExistentPropertyUnset() { + self::expectException(Exception::class); + self::expectExceptionMessage('Property nonExistentProperty does not exist'); + $spyMaster = new SpyMaster($this->targetObject); $spy = $spyMaster->infiltrate(); - unset($spy->nonExistent); + unset($spy->nonExistentProperty); } - /** - * @expectedException Exception - * @expectedExceptionMessage ReadOnly Spies are not permitted to change property values - */ public function testReadOnlySpyCannotChangeProperty() { + self::expectException(Exception::class); + self::expectExceptionMessage('ReadOnly Spies are not permitted to change property values'); + $spyMaster = new SpyMaster($this->targetObject); $spy = $spyMaster->infiltrate(); @@ -133,15 +136,14 @@ public function testReadWriteSpyCanChangeProperty() $this->assertEquals($privatePropertyValue, 'PHP'); } - /** - * @expectedException Exception - * @expectedExceptionMessage Property nonExistent does not exist - */ public function testReadWriteSpySetNonExistentProperty() { + self::expectException(Exception::class); + self::expectExceptionMessage('Property nonExistentProperty does not exist'); + $spyMaster = new SpyMaster($this->targetObject); $spy = $spyMaster->infiltrate(SpyMaster::SPY_READ_WRITE); - $spy->nonExistent = 'PHP'; + $spy->nonExistentProperty = 'PHP'; } } diff --git a/unitTests/phpunit.xml b/unitTests/phpunit.xml deleted file mode 100644 index 4f6aef2..0000000 --- a/unitTests/phpunit.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - ./classes - - - - ../classes - - - - - - - - -