From f5f15eb2509513d308f30fbe343e418855b6e9ed Mon Sep 17 00:00:00 2001 From: angryTom Date: Thu, 15 Jun 2017 17:25:14 +0200 Subject: [PATCH 1/9] Update symfony/finder dependency to version 3.3 --- composer.json | 4 ++-- tests/Reader/GitExecTest.php | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index caa28a8..163dd5f 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,9 @@ "minimum-stability": "stable", "require": { - "symfony/finder": "~2.5" + "symfony/finder": "~3.3" }, "require-dev": { - "phpunit/phpunit": "~4.2" + "phpunit/phpunit": "~5.4" } } diff --git a/tests/Reader/GitExecTest.php b/tests/Reader/GitExecTest.php index 691f27f..8aa78af 100644 --- a/tests/Reader/GitExecTest.php +++ b/tests/Reader/GitExecTest.php @@ -5,8 +5,6 @@ use Naneau\ProjectVersioner\Reader\Git\Describe\Exec as GitDescribeExecReader; use Naneau\ProjectVersioner\Reader\Git\Tag\Exec as GitTagExecReader; -use \RuntimeException; - class GitExecTest extends \PHPUnit_Framework_TestCase { /** From 318020f675ddf8a03a95945f669eb1338e676e6a Mon Sep 17 00:00:00 2001 From: t0mmie Date: Fri, 24 Jul 2020 17:22:25 +0200 Subject: [PATCH 2/9] Update to Symfony 4 and PHPUnit 8 --- composer.json | 9 ++++++--- src/Naneau/ProjectVersioner/Reader/File.php | 2 +- tests/Reader/ComposerTest.php | 8 ++++---- tests/Reader/FileTest.php | 4 ++-- tests/Reader/FinderTest.php | 14 ++++++------- tests/Reader/GitExecTest.php | 16 +++++++-------- tests/Versioner/CombineReadersTest.php | 22 ++++++++++----------- tests/VersionerTest.php | 12 +++++------ 8 files changed, 44 insertions(+), 43 deletions(-) diff --git a/composer.json b/composer.json index 163dd5f..7d300e5 100644 --- a/composer.json +++ b/composer.json @@ -10,14 +10,17 @@ ], "autoload": { - "psr-4": {"Naneau\\ProjectVersioner\\": "src/Naneau/ProjectVersioner/"} + "psr-4": { + "Naneau\\ProjectVersioner\\": "src/Naneau/ProjectVersioner/" + } }, "minimum-stability": "stable", "require": { - "symfony/finder": "~3.3" + "php": "7.2.* || 7.3.* || 7.4.*", + "symfony/finder": "~4" }, "require-dev": { - "phpunit/phpunit": "~5.4" + "phpunit/phpunit": "~8" } } diff --git a/src/Naneau/ProjectVersioner/Reader/File.php b/src/Naneau/ProjectVersioner/Reader/File.php index 9ed31a5..a9f4288 100644 --- a/src/Naneau/ProjectVersioner/Reader/File.php +++ b/src/Naneau/ProjectVersioner/Reader/File.php @@ -69,7 +69,7 @@ public function getFile() * Set the file * * @param string $file - * @return parent + * @return self */ public function setFile($file) { diff --git a/tests/Reader/ComposerTest.php b/tests/Reader/ComposerTest.php index 5191f39..33730df 100644 --- a/tests/Reader/ComposerTest.php +++ b/tests/Reader/ComposerTest.php @@ -5,7 +5,7 @@ use Naneau\ProjectVersioner\Reader\ComposerPackage as ComposerPackageReader; use Naneau\ProjectVersioner\Reader\ComposerJson as ComposerJsonReader; -class ComposerTest extends \PHPUnit_Framework_TestCase +class ComposerTest extends \PHPUnit\Framework\TestCase { public function testRead() { @@ -15,7 +15,7 @@ public function testRead() $versioner = new Versioner($readers); - $this->assertEquals('aa1f22', $versioner->get($directory)); + self::assertEquals('aa1f22', $versioner->get($directory)); } public function testPackageRead() @@ -26,7 +26,7 @@ public function testPackageRead() $versioner = new Versioner($readers); - $this->assertEquals('v2.5.4', $versioner->get($directory)); + self::assertEquals('v2.5.4', $versioner->get($directory)); } public function testComposerJsonRead() @@ -36,6 +36,6 @@ public function testComposerJsonRead() $readers = array(new ComposerJsonReader); $versioner = new Versioner($readers); - $this->assertEquals('1.0.0', $versioner->get($directory)); + self::assertEquals('1.0.0', $versioner->get($directory)); } } diff --git a/tests/Reader/FileTest.php b/tests/Reader/FileTest.php index 32c593f..3dfcd85 100644 --- a/tests/Reader/FileTest.php +++ b/tests/Reader/FileTest.php @@ -3,7 +3,7 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\File as FileReader; -class FileTest extends \PHPUnit_Framework_TestCase +class FileTest extends \PHPUnit\Framework\TestCase { public function testRead() { @@ -13,6 +13,6 @@ public function testRead() $versioner = new Versioner($readers); - $this->assertEquals('5.4.3', $versioner->get($directory)); + self::assertEquals('5.4.3', $versioner->get($directory)); } } diff --git a/tests/Reader/FinderTest.php b/tests/Reader/FinderTest.php index c94d4ba..7bdc20c 100644 --- a/tests/Reader/FinderTest.php +++ b/tests/Reader/FinderTest.php @@ -6,7 +6,7 @@ use Symfony\Component\Finder\Finder; -class FinderTest extends \PHPUnit_Framework_TestCase +class FinderTest extends \PHPUnit\Framework\TestCase { public function testMtime() { @@ -18,14 +18,14 @@ public function testMtime() $time = time(); touch($directory . '/DirectoryOne/FileFour.txt', $time); - $this->assertEquals($time, $versioner->get($directory)); + self::assertEquals($time, $versioner->get($directory)); } public function testContents() { $versioner = new Versioner(array(new ContentsReader('*.php'))); - $this->assertEquals( + self::assertEquals( 'db9d80', $versioner->get( __DIR__ . '/../projects/finder' @@ -43,7 +43,7 @@ public function testEmptyNames() $time = time(); touch($directory . '/DirectoryOne/FileFour.txt', $time); - $this->assertEquals($time, $versioner->get($directory)); + self::assertEquals($time, $versioner->get($directory)); } public function testEmptyNamesWithFinder() @@ -64,8 +64,8 @@ public function testEmptyNamesWithFinder() touch($directory . '/DirectoryOne/FileThree.php', $timeThree); touch($directory . '/DirectoryOne/FileFour.txt', $timeFour); - $this->assertEquals($timeThree, $versionerPhp->get($directory)); - $this->assertEquals($timeFour, $versionerTxt->get($directory)); + self::assertEquals($timeThree, $versionerPhp->get($directory)); + self::assertEquals($timeFour, $versionerTxt->get($directory)); } public function testNamesAndFinder() @@ -82,6 +82,6 @@ public function testNamesAndFinder() touch($directory . '/DirectoryOne/FileThree.php', $timeThree); touch($directory . '/DirectoryOne/FileFour.txt', $timeFour); - $this->assertEquals($timeThree, $versioner->get($directory)); + self::assertEquals($timeThree, $versioner->get($directory)); } } diff --git a/tests/Reader/GitExecTest.php b/tests/Reader/GitExecTest.php index 8aa78af..324d14c 100644 --- a/tests/Reader/GitExecTest.php +++ b/tests/Reader/GitExecTest.php @@ -5,7 +5,7 @@ use Naneau\ProjectVersioner\Reader\Git\Describe\Exec as GitDescribeExecReader; use Naneau\ProjectVersioner\Reader\Git\Tag\Exec as GitTagExecReader; -class GitExecTest extends \PHPUnit_Framework_TestCase +class GitExecTest extends \PHPUnit\Framework\TestCase { /** * Test reading of latest commit @@ -19,7 +19,7 @@ public function testShortCommitRead() $versioner = new Versioner(array(new GitCommitExecReader)); - $this->assertEquals($version, $versioner->get(self::getDirectory())); + self::assertEquals($version, $versioner->get(self::getDirectory())); } /** @@ -34,7 +34,7 @@ public function testLongCommitRead() $versioner = new Versioner(array(new GitCommitExecReader(false))); - $this->assertEquals($version, $versioner->get(self::getDirectory())); + self::assertEquals($version, $versioner->get(self::getDirectory())); } /** @@ -49,13 +49,13 @@ public function testDescribeRead() $versioner = new Versioner(array(new GitDescribeExecReader)); - $this->assertEquals($version, $versioner->get(self::getDirectory())); + self::assertEquals($version, $versioner->get(self::getDirectory())); } public function testTagRead() { $versioner = new Versioner(array(new GitTagExecReader)); - $this->assertEquals('0.0.2', $versioner->get(self::getDirectory())); + self::assertEquals('0.0.2', $versioner->get(self::getDirectory())); } /** @@ -63,7 +63,7 @@ public function testTagRead() * * @return void **/ - public function setUp() + public function setUp(): void { self::execWithDir(array('rm -rf %s', 'mkdir %s')); self::execInDir(array( @@ -94,7 +94,7 @@ public function setUp() * * @return void **/ - public function tearDown() + public function tearDown(): void { self::execWithDir(array('rm -rf %s')); } @@ -102,7 +102,7 @@ public function tearDown() /** * Exec a sert of shell commands * - * @return void + * @return array **/ private static function execInDir(array $cmds) { diff --git a/tests/Versioner/CombineReadersTest.php b/tests/Versioner/CombineReadersTest.php index 9c8cd88..06ae2bd 100644 --- a/tests/Versioner/CombineReadersTest.php +++ b/tests/Versioner/CombineReadersTest.php @@ -6,19 +6,19 @@ use Naneau\ProjectVersioner\Reader\ComposerPackage as ComposerPackageReader; -class CombineReadersTest extends \PHPUnit_Framework_TestCase +class CombineReadersTest extends \PHPUnit\Framework\TestCase { public function testComposerFirst() { $readers = array( new ComposerPackageReader('symfony/filesystem'), - new ComposerReader('VERSION'), + new ComposerReader(), new FileReader('VERSION') ); $versioner = new Versioner($readers); - return $this->assertEquals( + self::assertEquals( 'v2.5.4', $versioner->get( __DIR__ . '/../projects/composer-file/' @@ -31,12 +31,12 @@ public function testFileFirst() $readers = array( new FileReader('VERSION'), new ComposerPackageReader('symfony/filesystem'), - new ComposerReader('VERSION') + new ComposerReader() ); $versioner = new Versioner($readers); - return $this->assertEquals( + self::assertEquals( '5.4.3', $versioner->get( __DIR__ . '/../projects/composer-file/' @@ -48,13 +48,13 @@ public function testComposerFirstWithCombine() { $readers = array( new ComposerPackageReader('symfony/filesystem'), - new ComposerReader('VERSION'), + new ComposerReader(), new FileReader('VERSION') ); $versioner = new Versioner($readers); - return $this->assertEquals( + self::assertEquals( 'v2.5.4_aa1f22_5.4.3', $versioner->getCombined( __DIR__ . '/../projects/composer-file/', @@ -68,11 +68,11 @@ public function testFileFirstWithCombine() $readers = array( new FileReader('VERSION'), new ComposerPackageReader('symfony/filesystem'), - new ComposerReader('VERSION') + new ComposerReader() ); $versioner = new Versioner($readers); - return $this->assertEquals( + self::assertEquals( '5.4.3-v2.5.4-aa1f22', $versioner->getCombined( __DIR__ . '/../projects/composer-file/' @@ -88,7 +88,7 @@ public function testHasAVersion() $versioner->setReaders( array(new FileReader('VERSION')) ); - return $this->assertTrue( + self::assertTrue( $versioner->has(__DIR__ . '/../projects/composer-file/') ); @@ -96,7 +96,7 @@ public function testHasAVersion() $versioner->setReaders( array(new FileReader('FOO')) ); - return $this->assertFalse( + self::assertFalse( $versioner->has(__DIR__ . '/../projects/composer-file/') ); } diff --git a/tests/VersionerTest.php b/tests/VersionerTest.php index 1cb241b..573c2c3 100644 --- a/tests/VersionerTest.php +++ b/tests/VersionerTest.php @@ -3,22 +3,20 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Finder\MTime as MTimeReader; -class VersionerTest extends \PHPUnit_Framework_TestCase +class VersionerTest extends \PHPUnit\Framework\TestCase { - /** - * @expectedException InvalidArgumentException - **/ public function testNoDir() { + $this->expectException(\InvalidArgumentException::class); + $versioner = new Versioner(array(new MTimeReader('*.txt'))); $versioner->get('foo'); } - /** - * @expectedException RuntimeException - **/ public function testNoReaders() { + $this->expectException(\RuntimeException::class); + $versioner = new Versioner(); $versioner->get('foo'); From d0ab45762f20379cea6da6660d3288ad0e58e14f Mon Sep 17 00:00:00 2001 From: t0mmie Date: Fri, 24 Jul 2020 17:22:35 +0200 Subject: [PATCH 3/9] Fix Travis PHP versions --- .travis.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9dfeebc..1bafe01 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,8 @@ language: php php: - - 5.6 - - 5.5 - - 5.4 - - 5.3 - - hhvm + - 7.2 + - 7.3 + - 7.4 # Install deps before_install: From c87d81dbbcec8b72f39bc94b882dfd44bfa493f9 Mon Sep 17 00:00:00 2001 From: t0mmie Date: Tue, 28 Jul 2020 15:08:41 +0200 Subject: [PATCH 4/9] Remove unused namespace --- src/Naneau/ProjectVersioner/Reader/ComposerJson.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Naneau/ProjectVersioner/Reader/ComposerJson.php b/src/Naneau/ProjectVersioner/Reader/ComposerJson.php index f3d0581..c21d668 100644 --- a/src/Naneau/ProjectVersioner/Reader/ComposerJson.php +++ b/src/Naneau/ProjectVersioner/Reader/ComposerJson.php @@ -10,7 +10,6 @@ use Naneau\ProjectVersioner\ReaderInterface; -use \RuntimeException; use \stdClass; /** @@ -43,8 +42,8 @@ public function read($directory) $json = json_decode($json); if (!($json instanceof stdClass) || empty($json->version)) { return false; - } + } - return trim($json->version); + return trim($json->version); } } From 6b6aeaf16e4154fe7b7338967e728bfc899f0280 Mon Sep 17 00:00:00 2001 From: t0mmie Date: Tue, 28 Jul 2020 15:09:06 +0200 Subject: [PATCH 5/9] Add namespace for unit tests --- composer.json | 3 ++- .../ProjectVersioner/Test}/Reader/ComposerTest.php | 7 ++++--- .../ProjectVersioner/Test}/Reader/FileTest.php | 3 ++- .../ProjectVersioner/Test}/Reader/FinderTest.php | 11 ++++++----- .../ProjectVersioner/Test}/Reader/GitExecTest.php | 5 +++-- .../Test}/Versioner/CombineReadersTest.php | 13 +++++++------ .../ProjectVersioner/Test}/VersionerTest.php | 1 + 7 files changed, 25 insertions(+), 18 deletions(-) rename tests/{ => Naneau/ProjectVersioner/Test}/Reader/ComposerTest.php (80%) rename tests/{ => Naneau/ProjectVersioner/Test}/Reader/FileTest.php (77%) rename tests/{ => Naneau/ProjectVersioner/Test}/Reader/FinderTest.php (86%) rename tests/{ => Naneau/ProjectVersioner/Test}/Reader/GitExecTest.php (96%) rename tests/{ => Naneau/ProjectVersioner/Test}/Versioner/CombineReadersTest.php (83%) rename tests/{ => Naneau/ProjectVersioner/Test}/VersionerTest.php (93%) diff --git a/composer.json b/composer.json index 7d300e5..dffa67e 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "autoload": { "psr-4": { - "Naneau\\ProjectVersioner\\": "src/Naneau/ProjectVersioner/" + "Naneau\\ProjectVersioner\\": "src/Naneau/ProjectVersioner/", + "Naneau\\ProjectVersioner\\Test\\": "tests/Naneau/ProjectVersioner/Test" } }, diff --git a/tests/Reader/ComposerTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php similarity index 80% rename from tests/Reader/ComposerTest.php rename to tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php index 33730df..0e31b5d 100644 --- a/tests/Reader/ComposerTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php @@ -1,4 +1,5 @@ get( - __DIR__ . '/../projects/finder' + __DIR__ . '/../../../../projects/finder' ) ); } public function testEmptyNames() { - $directory = __DIR__ . '/../projects/finder'; + $directory = __DIR__ . '/../../../../projects/finder'; $versioner = new Versioner(array(new MTimeReader())); @@ -48,7 +49,7 @@ public function testEmptyNames() public function testEmptyNamesWithFinder() { - $directory = __DIR__ . '/../projects/finder'; + $directory = __DIR__ . '/../../../../projects/finder'; $finderTxt = new Finder; $finderTxt->name('*.txt'); @@ -70,7 +71,7 @@ public function testEmptyNamesWithFinder() public function testNamesAndFinder() { - $directory = __DIR__ . '/../projects/finder'; + $directory = __DIR__ . '/../../../../projects/finder'; $finder = new Finder; $finder->name('*.txt'); diff --git a/tests/Reader/GitExecTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php similarity index 96% rename from tests/Reader/GitExecTest.php rename to tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php index 324d14c..f7c87bc 100644 --- a/tests/Reader/GitExecTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php @@ -1,4 +1,5 @@ get( - __DIR__ . '/../projects/composer-file/' + __DIR__ . '/../../../../projects/composer-file/' ) ); } @@ -39,7 +40,7 @@ public function testFileFirst() self::assertEquals( '5.4.3', $versioner->get( - __DIR__ . '/../projects/composer-file/' + __DIR__ . '/../../../../projects/composer-file/' ) ); } @@ -57,7 +58,7 @@ public function testComposerFirstWithCombine() self::assertEquals( 'v2.5.4_aa1f22_5.4.3', $versioner->getCombined( - __DIR__ . '/../projects/composer-file/', + __DIR__ . '/../../../../projects/composer-file/', '_' // use _ for separator ) ); @@ -75,7 +76,7 @@ public function testFileFirstWithCombine() self::assertEquals( '5.4.3-v2.5.4-aa1f22', $versioner->getCombined( - __DIR__ . '/../projects/composer-file/' + __DIR__ . '/../../../../projects/composer-file/' ) ); } @@ -89,7 +90,7 @@ public function testHasAVersion() array(new FileReader('VERSION')) ); self::assertTrue( - $versioner->has(__DIR__ . '/../projects/composer-file/') + $versioner->has(__DIR__ . '/../../../../projects/composer-file/') ); // Should not have a version @@ -97,7 +98,7 @@ public function testHasAVersion() array(new FileReader('FOO')) ); self::assertFalse( - $versioner->has(__DIR__ . '/../projects/composer-file/') + $versioner->has(__DIR__ . '/../../../../projects/composer-file/') ); } } diff --git a/tests/VersionerTest.php b/tests/Naneau/ProjectVersioner/Test/VersionerTest.php similarity index 93% rename from tests/VersionerTest.php rename to tests/Naneau/ProjectVersioner/Test/VersionerTest.php index 573c2c3..45d7f94 100644 --- a/tests/VersionerTest.php +++ b/tests/Naneau/ProjectVersioner/Test/VersionerTest.php @@ -1,4 +1,5 @@ Date: Wed, 5 Aug 2020 10:24:59 +0200 Subject: [PATCH 6/9] Add changelog --- CHANGELOG.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c54bb3a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +## 1.1.0 + +### PHP support +* Dropped support for PHP `7.1` and lower. +* Added support for PHP `7.2`, `7.3` and `7.4`. + +### 3rd party updates +* Updated `symfony/finder` to version `4.4`. + +## 1.0.4 +* Updated `symfony/finder` to version `3.3`. + +## 1.0.3 +* Re-added the missing `Naneau\ProjectVersioner\Versioner::has($directory)` method. + +## 1.0.2 +* Added `Naneau\ProjectVersioner\Reader\ComposerJson` for reading versions from a composer.json file. From 420c63468dce5861e7a28edb9704a0be9e1e59b4 Mon Sep 17 00:00:00 2001 From: t0mmie Date: Wed, 5 Aug 2020 10:25:09 +0200 Subject: [PATCH 7/9] Add github workflow --- .github/workflows/php-checks.yml | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/php-checks.yml diff --git a/.github/workflows/php-checks.yml b/.github/workflows/php-checks.yml new file mode 100644 index 0000000..837741b --- /dev/null +++ b/.github/workflows/php-checks.yml @@ -0,0 +1,38 @@ +on: [push] +name: PHP checks +jobs: + composer-check: + runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['7.2', '7.3', '7.4'] + name: PHP ${{ matrix.php-versions }} tests + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + tools: cs2pr + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v1 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + + - name: Run Composer install + run: composer install --prefer-dist + + - name: Run PHP checks + run: composer phpcheck + + - name: Run PHPUnit + run: composer phpunit From 2df8f51721b571a9c712e40e78b93b39abc4c393 Mon Sep 17 00:00:00 2001 From: t0mmie Date: Wed, 5 Aug 2020 10:26:05 +0200 Subject: [PATCH 8/9] Add code analysis tools and fix issues --- .gitignore | 1 + .travis.yml | 14 ---- README.md | 34 ++++---- composer.json | 38 +++++++-- phpunit.xml.dist | 15 ++-- .../ProjectVersioner/Reader/Composer.php | 29 +++---- .../ProjectVersioner/Reader/ComposerJson.php | 29 ++----- .../Reader/ComposerPackage.php | 67 +++++++-------- src/Naneau/ProjectVersioner/Reader/File.php | 45 +++------- .../Reader/Finder/Contents.php | 16 +--- .../ProjectVersioner/Reader/Finder/Finder.php | 39 ++------- .../ProjectVersioner/Reader/Finder/MTime.php | 18 +--- .../Reader/Git/Commit/Exec.php | 47 +++-------- .../Reader/Git/Describe/Exec.php | 15 +--- .../ProjectVersioner/Reader/Git/Exec.php | 47 ++++------- .../ProjectVersioner/Reader/Git/Tag/Exec.php | 18 +--- .../ProjectVersioner/ReaderInterface.php | 25 ++---- src/Naneau/ProjectVersioner/Versioner.php | 53 ++++-------- .../Test/Reader/ComposerTest.php | 13 +-- .../ProjectVersioner/Test/Reader/FileTest.php | 4 +- .../Test/Reader/FinderTest.php | 22 ++--- .../Test/Reader/GitExecTest.php | 83 ++++++++----------- .../Test/Versioner/CombineReadersTest.php | 31 ++++--- .../ProjectVersioner/Test/VersionerTest.php | 6 +- 24 files changed, 255 insertions(+), 454 deletions(-) delete mode 100644 .travis.yml diff --git a/.gitignore b/.gitignore index 3a9875b..0794651 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ composer.lock +.phpunit.result.cache diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1bafe01..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -language: php -php: - - 7.2 - - 7.3 - - 7.4 - -# Install deps -before_install: - - sudo apt-get update -qq - - sudo apt-get install -qq git-core - - git config --global user.email "ci@naneau.net" - - git config --global user.name "Travis" -install: - - composer install diff --git a/README.md b/README.md index e373fd4..cae3f5f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # PHP Project Versioner -[![Latest Stable Version](https://poser.pugx.org/naneau/project-versioner/v/stable.svg)](https://packagist.org/packages/naneau/project-versioner) -[![Build Status](https://travis-ci.org/naneau/php-project-versioner.svg?branch=master)](https://travis-ci.org/naneau/php-project-versioner) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/naneau/php-project-versioner/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/naneau/php-project-versioner/?branch=master) +[![PHP checks](https://github.com/AngryBytes/php-project-versioner/workflows/PHP%20checks/badge.svg)](https://github.com/AngryBytes/php-project-versioner/actions?query=workflow%3A%22PHP+checks%22) + +**Note:** this is a fork of the original project as it appears to be abandoned. This is a simple tool to obtain "versions" for projects in PHP. @@ -31,7 +31,7 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Git\Commit\Exec as GitCommitReader; // Create a versioner -$versioner = new Versioner(array(new GitCommitReader)); +$versioner = new Versioner([new GitCommitReader]); // Short commit hash like "gd8587c8" $version = $versioner->get('/foo/bar'); @@ -46,7 +46,7 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Git\Tag\Exec as GitTagReader; // Create a versioner -$versioner = new Versioner(array(new GitTagReader)); +$versioner = new Versioner([new GitTagReader]); // Last tag $version = $versioner->get('/foo/bar'); @@ -61,7 +61,7 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Git\Tag\Exec as GitTagReader; // Create a versioner -$versioner = new Versioner(array(new GitTagReader)); +$versioner = new Versioner([new GitTagReader]); // Last tag + commit info, like 4.3.2-9-gd504031 $version = $versioner->get('/foo/bar'); @@ -78,10 +78,10 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\File as FileReader; // Create a versioner -$versioner = new Versioner(array( +$versioner = new Versioner([ // Reader for "VERSION" file new FileReader('VERSION') -)); +]); // Retrieve version from versioner $version = $versioner->get('/foo/bar'); @@ -96,9 +96,9 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Finder\MTime as MTimeReader; // Create a versioner -$versioner = new Versioner(array( +$versioner = new Versioner([ new MTimeReader('*.txt') // Look at all *.txt files -)); +]); // Highest mtime, like 1410806782 $version = $versioner->get('/foo/bar'); @@ -112,9 +112,9 @@ Using a different reader it is possible to use the *contents* of the files found use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Finder\Contents as ContentsReader; -$versioner = new Versioner(array( +$versioner = new Versioner([ new ContentsReader('*.jpg') -)); +]); // Short hash of file contents, like gd504031 $version = $versioner->get('/foo/bar'); @@ -132,7 +132,7 @@ To look at all packages combined: use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\Composer as ComposerReader; -$versioner = new Versioner(array(new ComposerReader)); +$versioner = new Versioner([new ComposerReader]); // Short hash like "ae9b8a" $version = $versioner->get('/foo/bar'); @@ -146,9 +146,9 @@ Or, looking for a specific package: use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\ComposerPackage as ComposerPackageReader; -$versioner = new Versioner(array( +$versioner = new Versioner([ new ComposerPackageReader('symfony/filesystem') -)); +]); // Composer Version (SemVer) like "v2.5.4" $version = $versioner->get('/foo/bar'); @@ -165,10 +165,10 @@ use Naneau\ProjectVersioner\Versioner; use Naneau\ProjectVersioner\Reader\ComposerPackage as ComposerPackageReader; use Naneau\ProjectVersioner\Reader\Git\Tag\Exec as GitTagReader; -$versioner = new Versioner(array( +$versioner = new Versioner([ new ComposerPackageReader('symfony/filesystem'), new GitTagReader -)); +]); // First version found $version = $versioner->get('/foo/bar'); diff --git a/composer.json b/composer.json index dffa67e..4bc8387 100644 --- a/composer.json +++ b/composer.json @@ -1,27 +1,55 @@ { - "name": "naneau/project-versioner", + "name": "angrybytes/project-versioner", "description": "A tool to maintain project/sub-project versions based on Composer, file mtimes, etc.", "license": "MIT", "authors": [ { "name": "Maurice Fonk", "email": "maurice@naneau.net" + }, + { + "name": "Angry Bytes BV", + "email": "info@angrybytes.com", + "homepage": "https://angrybytes.com/" } ], - + "support": { + "email": "support@angrybytes.com" + }, "autoload": { "psr-4": { "Naneau\\ProjectVersioner\\": "src/Naneau/ProjectVersioner/", "Naneau\\ProjectVersioner\\Test\\": "tests/Naneau/ProjectVersioner/Test" } }, - + "config": { + "optimize-autoloader": true, + "sort-packages": true + }, + "scripts": { + "phpcheck": [ + "./vendor/bin/parallel-lint src/", + "./vendor/bin/phpstan analyse -l max --memory-limit=1G src/", + "./vendor/bin/phpcs -p --standard=PSR2 --extensions=php src/" + ], + "phpcbf":[ + "./vendor/bin/phpcbf -p --standard=PSR2 --extensions=php src/" + ], + "phpunit": [ + "./vendor/bin/phpunit" + ] + }, "minimum-stability": "stable", "require": { + "ext-json": "*", "php": "7.2.* || 7.3.* || 7.4.*", - "symfony/finder": "~4" + "symfony/finder": "~4.4" }, "require-dev": { - "phpunit/phpunit": "~8" + "jakub-onderka/php-console-highlighter": "^0.4.0", + "jakub-onderka/php-parallel-lint": "^1.0", + "phpstan/phpstan": "0.12.34", + "phpunit/phpunit": "~8.5", + "squizlabs/php_codesniffer": "^3.5" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e740ab5..238f65f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,15 +1,14 @@ - + ./tests/ diff --git a/src/Naneau/ProjectVersioner/Reader/Composer.php b/src/Naneau/ProjectVersioner/Reader/Composer.php index 4a1ef5d..41de516 100644 --- a/src/Naneau/ProjectVersioner/Reader/Composer.php +++ b/src/Naneau/ProjectVersioner/Reader/Composer.php @@ -1,11 +1,4 @@ version)) { - return false; + return null; } return trim($json->version); diff --git a/src/Naneau/ProjectVersioner/Reader/ComposerPackage.php b/src/Naneau/ProjectVersioner/Reader/ComposerPackage.php index 64890b4..c4bccc9 100644 --- a/src/Naneau/ProjectVersioner/Reader/ComposerPackage.php +++ b/src/Naneau/ProjectVersioner/Reader/ComposerPackage.php @@ -1,23 +1,15 @@ setPackage($package); } /** * {@inheritdoc} - **/ - public function canRead($directory) + */ + public function canRead(string $directory): bool { if (!is_readable($directory . '/composer.lock')) { return false; @@ -50,7 +36,7 @@ public function canRead($directory) $package = $this->getPackageFromLockFile($directory); - if ($package === false) { + if ($package === null) { return false; } @@ -59,31 +45,33 @@ public function canRead($directory) /** * {@inheritdoc} - **/ - public function read($directory) + */ + public function read(string $directory) { $package = $this->getPackageFromLockFile($directory); + if ($package === null) { + throw new RuntimeException(sprintf( + 'No composer.lock file found in directory "%s".', + $directory + )); + } + return $package->version; } /** * Get the package - * - * @return string */ - public function getPackage() + public function getPackage(): string { return $this->package; } /** * Set the package - * - * @param string $package - * @return ComposerPackage */ - public function setPackage($package) + public function setPackage(string $package): self { $this->package = $package; @@ -92,14 +80,17 @@ public function setPackage($package) /** * Get the package from the lockfile - * - * @return stdClass|bool returns false when package can not be found - **/ - private function getPackageFromLockFile($directory) + */ + private function getPackageFromLockFile(string $directory): ?stdClass { - $parsed = json_decode(file_get_contents($directory . '/composer.lock')); + $contents = file_get_contents($directory . '/composer.lock'); + if (!$contents) { + return null; + } + + $parsed = json_decode($contents, false); if (!isset($parsed->packages)) { - return false; + return null; } foreach ($parsed->packages as $package) { @@ -108,6 +99,6 @@ private function getPackageFromLockFile($directory) } } - return false; + return null; } } diff --git a/src/Naneau/ProjectVersioner/Reader/File.php b/src/Naneau/ProjectVersioner/Reader/File.php index a9f4288..3a78a23 100644 --- a/src/Naneau/ProjectVersioner/Reader/File.php +++ b/src/Naneau/ProjectVersioner/Reader/File.php @@ -1,11 +1,4 @@ setFile($file); } /** * {@inheritdoc} - **/ - public function canRead($directory) + */ + public function canRead(string $directory): bool { return is_readable($directory . DIRECTORY_SEPARATOR . $this->getFile()); } /** * {@inheritdoc} - **/ - public function read($directory) + */ + public function read(string $directory) { - return trim(file_get_contents($directory . DIRECTORY_SEPARATOR . $this->getFile())); + $contents = file_get_contents($directory . DIRECTORY_SEPARATOR . $this->getFile()); + if (!$contents) { + return null; + } + return trim($contents); } /** * Get the file - * - * @return string */ - public function getFile() + public function getFile(): string { return $this->file; } /** * Set the file - * - * @param string $file - * @return self */ - public function setFile($file) + public function setFile(string $file): self { $this->file = $file; return $this; } - } diff --git a/src/Naneau/ProjectVersioner/Reader/Finder/Contents.php b/src/Naneau/ProjectVersioner/Reader/Finder/Contents.php index 1451a31..81ad013 100644 --- a/src/Naneau/ProjectVersioner/Reader/Finder/Contents.php +++ b/src/Naneau/ProjectVersioner/Reader/Finder/Contents.php @@ -1,32 +1,20 @@ getFinder() as $file) { - // MD5 hash of the file $fileHash = md5_file( $file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename() diff --git a/src/Naneau/ProjectVersioner/Reader/Finder/Finder.php b/src/Naneau/ProjectVersioner/Reader/Finder/Finder.php index 88cb276..42f9480 100644 --- a/src/Naneau/ProjectVersioner/Reader/Finder/Finder.php +++ b/src/Naneau/ProjectVersioner/Reader/Finder/Finder.php @@ -1,11 +1,4 @@ getFinder()->in($directory); // If at least one file/dir can be found, assume we can read - foreach ($this->getFinder() as $file) { - return true; - } - - return false; + return $this->getFinder()->hasResults(); } /** * Get the finder - * - * @return SfFinder */ - public function getFinder() + public function getFinder(): SfFinder { return $this->finder; } /** * Set the finder - * - * @param SfFinder $finder - * @return SfFinder */ - public function setFinder(SfFinder $finder) + public function setFinder(SfFinder $finder): self { $this->finder = $finder; diff --git a/src/Naneau/ProjectVersioner/Reader/Finder/MTime.php b/src/Naneau/ProjectVersioner/Reader/Finder/MTime.php index 2ead49e..4c3a4df 100644 --- a/src/Naneau/ProjectVersioner/Reader/Finder/MTime.php +++ b/src/Naneau/ProjectVersioner/Reader/Finder/MTime.php @@ -1,37 +1,25 @@ getFinder() as $file) { - $mtime = filemtime( $file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename() ); - if ($mtime > $highest) { + if (is_int($mtime) && $mtime > $highest) { $highest = $mtime; } } diff --git a/src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php b/src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php index ea22f03..4845eee 100644 --- a/src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php +++ b/src/Naneau/ProjectVersioner/Reader/Git/Commit/Exec.php @@ -1,11 +1,4 @@ setShort($short); } /** * Get use short commit hash? - * - * @return bool */ - public function getShort() + public function getShort(): bool { return $this->short; } /** * Set use short commit hash? - * - * @param bool $short - * @return Exec */ - public function setShort($short) + public function setShort(bool $short): self { $this->short = $short; @@ -66,22 +44,19 @@ public function setShort($short) /** * Get command for directory - * - * @param string $directory - * @return string - **/ - protected function getCommandForDirectory($directory) + */ + protected function getCommandForDirectory(string $directory): string { if ($this->getShort()) { return sprintf( 'cd %s && git rev-parse --short HEAD', escapeshellarg($directory) ); - } else { - return sprintf( - 'cd %s && git rev-parse HEAD', - escapeshellarg($directory) - ); } + + return sprintf( + 'cd %s && git rev-parse HEAD', + escapeshellarg($directory) + ); } } diff --git a/src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php b/src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php index 359c576..2d1d7cd 100644 --- a/src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php +++ b/src/Naneau/ProjectVersioner/Reader/Git/Describe/Exec.php @@ -1,11 +1,4 @@ canExec( $this->getCommandForDirectory($directory), @@ -34,8 +25,8 @@ public function canRead($directory) /** * {@inheritDoc} - **/ - public function read($directory) + */ + public function read(string $directory) { return $this->exec( $this->getCommandForDirectory($directory) @@ -44,20 +35,13 @@ public function read($directory) /** * Get the command for a directory - * - * @param string $directory - * @return string - **/ - abstract protected function getCommandForDirectory($directory); + */ + abstract protected function getCommandForDirectory(string $directory): string; /** * Can a git command be executed? - * - * @param string $command - * @param string $directory - * @return void - **/ - private function canExec($command, $directory) + */ + private function canExec(string $command, string $directory): bool { // We rely on exec, so it needs to exist if (!function_exists('exec')) { @@ -70,7 +54,7 @@ private function canExec($command, $directory) } // Try to exec() - $output = array(); + $output = []; $return = 0; @exec($command, $output, $return); @@ -80,13 +64,10 @@ private function canExec($command, $directory) /** * Execute a git command and return first line of output - * - * @param string $command - * @return string - **/ - private function exec($command) + */ + private function exec(string $command): string { - $output = array(); + $output = []; $return = 0; // Try to find last commit hash diff --git a/src/Naneau/ProjectVersioner/Reader/Git/Tag/Exec.php b/src/Naneau/ProjectVersioner/Reader/Git/Tag/Exec.php index 2acc4fb..29033f0 100644 --- a/src/Naneau/ProjectVersioner/Reader/Git/Tag/Exec.php +++ b/src/Naneau/ProjectVersioner/Reader/Git/Tag/Exec.php @@ -1,11 +1,4 @@ setReaders($readers); } @@ -44,10 +33,9 @@ public function __construct(array $readers = array()) /** * Get the version for a directory * - * @param string $directory - * @return string - **/ - public function get($directory) + * @return string|int|null + */ + public function get(string $directory) { foreach ($this->getReaders() as $reader) { if ($reader->canRead($directory)) { @@ -68,14 +56,10 @@ public function get($directory) * * Version will be considered "found" if at least one versioner returns * output. - * - * @param string $directory - * @param string $separator - * @return string - **/ - public function getCombined($directory, $separator = '-') + */ + public function getCombined(string $directory, string $separator = '-'): string { - $found = array(); + $found = []; foreach ($this->getReaders() as $reader) { if ($reader->canRead($directory)) { $found[] = $reader->read($directory); @@ -94,11 +78,8 @@ public function getCombined($directory, $separator = '-') /** * Does a directory have a version? - * - * @param string $directory - * @return bool - **/ - public function has($directory) + */ + public function has(string $directory): bool { foreach ($this->getReaders() as $reader) { if ($reader->canRead($directory)) { @@ -114,7 +95,7 @@ public function has($directory) * * @return Reader[] */ - public function getReaders() + public function getReaders(): array { return $this->readers; } @@ -122,10 +103,9 @@ public function getReaders() /** * Set the set of readers * - * @param Reader[] $readers - * @return Versioner + * @param Reader[] $readers */ - public function setReaders(array $readers) + public function setReaders(array $readers): self { $this->readers = $readers; @@ -134,11 +114,8 @@ public function setReaders(array $readers) /** * Add a reader - * - * @param Reader[] $reader - * @return Versioner */ - public function addReader(Reader $reader) + public function addReader(Reader $reader): self { $this->readers[] = $reader; diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php index 0e31b5d..521e106 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php @@ -8,33 +8,34 @@ class ComposerTest extends \PHPUnit\Framework\TestCase { - public function testRead() + public function testRead(): void { $directory = __DIR__ . '/../../../../projects/composer'; - $readers = array(new ComposerReader); + $readers = [new ComposerReader]; $versioner = new Versioner($readers); self::assertEquals('aa1f22', $versioner->get($directory)); } - public function testPackageRead() + public function testPackageRead(): void { $directory = __DIR__ . '/../../../../projects/composer'; - $readers = array(new ComposerPackageReader('symfony/filesystem')); + $readers = [new ComposerPackageReader('symfony/filesystem')]; $versioner = new Versioner($readers); self::assertEquals('v2.5.4', $versioner->get($directory)); } - public function testComposerJsonRead() + public function testComposerJsonRead(): void { $directory = __DIR__ . '/../../../../projects/composer'; - $readers = array(new ComposerJsonReader); + $readers = [new ComposerJsonReader]; + $versioner = new Versioner($readers); self::assertEquals('1.0.0', $versioner->get($directory)); diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php index 230ec9a..a9d0d12 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php @@ -6,11 +6,11 @@ class FileTest extends \PHPUnit\Framework\TestCase { - public function testRead() + public function testRead(): void { $directory = __DIR__ . '/../../../../projects/file'; - $readers = array(new FileReader('VERSION')); + $readers = [new FileReader('VERSION')]; $versioner = new Versioner($readers); diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php index af828b3..92fb486 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php @@ -9,11 +9,11 @@ class FinderTest extends \PHPUnit\Framework\TestCase { - public function testMtime() + public function testMtime(): void { $directory = __DIR__ . '/../../../../projects/finder'; - $versioner = new Versioner(array(new MTimeReader('*.txt'))); + $versioner = new Versioner([new MTimeReader('*.txt')]); // Set the time to now for one of the files $time = time(); @@ -22,9 +22,9 @@ public function testMtime() self::assertEquals($time, $versioner->get($directory)); } - public function testContents() + public function testContents(): void { - $versioner = new Versioner(array(new ContentsReader('*.php'))); + $versioner = new Versioner([new ContentsReader('*.php')]); self::assertEquals( 'db9d80', @@ -34,11 +34,11 @@ public function testContents() ); } - public function testEmptyNames() + public function testEmptyNames(): void { $directory = __DIR__ . '/../../../../projects/finder'; - $versioner = new Versioner(array(new MTimeReader())); + $versioner = new Versioner([new MTimeReader()]); // Set the time to now for one of the files $time = time(); @@ -47,17 +47,17 @@ public function testEmptyNames() self::assertEquals($time, $versioner->get($directory)); } - public function testEmptyNamesWithFinder() + public function testEmptyNamesWithFinder(): void { $directory = __DIR__ . '/../../../../projects/finder'; $finderTxt = new Finder; $finderTxt->name('*.txt'); - $versionerTxt = new Versioner(array(new MTimeReader(null, $finderTxt))); + $versionerTxt = new Versioner([new MTimeReader(null, $finderTxt)]); $finderPhp = new Finder; $finderPhp->name('*.php'); - $versionerPhp = new Versioner(array(new MTimeReader(null, $finderPhp))); + $versionerPhp = new Versioner([new MTimeReader(null, $finderPhp)]); $timeThree = time(); $timeFour = $timeThree - 10; @@ -69,13 +69,13 @@ public function testEmptyNamesWithFinder() self::assertEquals($timeFour, $versionerTxt->get($directory)); } - public function testNamesAndFinder() + public function testNamesAndFinder(): void { $directory = __DIR__ . '/../../../../projects/finder'; $finder = new Finder; $finder->name('*.txt'); - $versioner = new Versioner(array(new MTimeReader('*.php', $finder))); + $versioner = new Versioner([new MTimeReader('*.php', $finder)]); $timeThree = time(); $timeFour = $timeThree - 10; diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php index f7c87bc..8eb7adc 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php @@ -10,71 +10,63 @@ class GitExecTest extends \PHPUnit\Framework\TestCase { /** * Test reading of latest commit - * - * @return void - **/ - public function testShortCommitRead() + */ + public function testShortCommitRead(): void { - $versionOutput = self::execInDir(array('git rev-parse --short HEAD')); + $versionOutput = self::execInDir(['git rev-parse --short HEAD']); $version = $versionOutput[0]; - $versioner = new Versioner(array(new GitCommitExecReader)); + $versioner = new Versioner([new GitCommitExecReader]); self::assertEquals($version, $versioner->get(self::getDirectory())); } /** * Test reading of latest commit - * - * @return void - **/ - public function testLongCommitRead() + */ + public function testLongCommitRead(): void { - $versionOutput = self::execInDir(array('git rev-parse HEAD')); + $versionOutput = self::execInDir(['git rev-parse HEAD']); $version = $versionOutput[0]; - $versioner = new Versioner(array(new GitCommitExecReader(false))); + $versioner = new Versioner([new GitCommitExecReader(false)]); self::assertEquals($version, $versioner->get(self::getDirectory())); } /** * Test reading of latest commit - * - * @return void - **/ - public function testDescribeRead() + */ + public function testDescribeRead(): void { - $versionOutput = self::execInDir(array('git describe')); + $versionOutput = self::execInDir(['git describe']); $version = $versionOutput[0]; - $versioner = new Versioner(array(new GitDescribeExecReader)); + $versioner = new Versioner([new GitDescribeExecReader]); self::assertEquals($version, $versioner->get(self::getDirectory())); } - public function testTagRead() + public function testTagRead(): void { - $versioner = new Versioner(array(new GitTagExecReader)); + $versioner = new Versioner([new GitTagExecReader]); self::assertEquals('0.0.2', $versioner->get(self::getDirectory())); } /** * Set up fixtures - * - * @return void - **/ + */ public function setUp(): void { - self::execWithDir(array('rm -rf %s', 'mkdir %s')); - self::execInDir(array( + self::execWithDir(['rm -rf %s', 'mkdir %s']); + self::execInDir([ 'touch testfile', 'git init' - )); + ]); // Add commits, with matching tags for ($x = 0; $x < 3; $x++) { - self::execInDir(array( + self::execInDir([ // Contained in tag sprintf('touch test.%d', $x), @@ -86,28 +78,27 @@ public function setUp(): void sprintf('touch test.%d.notag', $x), sprintf('git add test.%d.notag', $x), sprintf('git commit -m "commit %d no tag"', $x) - )); + ]); } } /** * Tear down fixtures - * - * @return void - **/ + */ public function tearDown(): void { - self::execWithDir(array('rm -rf %s')); + self::execWithDir(['rm -rf %s']); } /** * Exec a sert of shell commands * - * @return array - **/ - private static function execInDir(array $cmds) + * @param string[] $cmds + * @return string[] + */ + private static function execInDir(array $cmds): array { - foreach($cmds as $key => $cmd) { + foreach ($cmds as $key => $cmd) { $cmds[$key] = 'cd %s && ' . $cmd; } return self::execWithDir($cmds); @@ -116,18 +107,18 @@ private static function execInDir(array $cmds) /** * Exec a bunch of commands with the test directory given * - * @param array $cmds - * @return array output from latest command - **/ - private static function execWithDir(array $cmds) + * @param string[] $cmds + * @return string[] output from latest command + */ + private static function execWithDir(array $cmds): array { - foreach($cmds as $cmd) { + foreach ($cmds as $cmd) { $inflectedCmd = sprintf( $cmd, escapeshellarg(self::getDirectory()) ); - $output = array(); + $output = []; $return = 0; exec($inflectedCmd, $output, $return); if ($return !== 0) { @@ -141,15 +132,13 @@ private static function execWithDir(array $cmds) } // Return latest output - return $output; + return $output ?? []; } /** * Get git tests directory - * - * @return string - **/ - private static function getDirectory() + */ + private static function getDirectory(): string { return __DIR__ . '/../../../../projects/git'; } diff --git a/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php b/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php index eb184f8..12e1ce9 100644 --- a/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php @@ -6,16 +6,15 @@ use Naneau\ProjectVersioner\Reader\Composer as ComposerReader; use Naneau\ProjectVersioner\Reader\ComposerPackage as ComposerPackageReader; - class CombineReadersTest extends \PHPUnit\Framework\TestCase { - public function testComposerFirst() + public function testComposerFirst(): void { - $readers = array( + $readers = [ new ComposerPackageReader('symfony/filesystem'), new ComposerReader(), new FileReader('VERSION') - ); + ]; $versioner = new Versioner($readers); @@ -27,13 +26,13 @@ public function testComposerFirst() ); } - public function testFileFirst() + public function testFileFirst(): void { - $readers = array( + $readers = [ new FileReader('VERSION'), new ComposerPackageReader('symfony/filesystem'), new ComposerReader() - ); + ]; $versioner = new Versioner($readers); @@ -45,13 +44,13 @@ public function testFileFirst() ); } - public function testComposerFirstWithCombine() + public function testComposerFirstWithCombine(): void { - $readers = array( + $readers = [ new ComposerPackageReader('symfony/filesystem'), new ComposerReader(), new FileReader('VERSION') - ); + ]; $versioner = new Versioner($readers); @@ -64,13 +63,13 @@ public function testComposerFirstWithCombine() ); } - public function testFileFirstWithCombine() + public function testFileFirstWithCombine(): void { - $readers = array( + $readers = [ new FileReader('VERSION'), new ComposerPackageReader('symfony/filesystem'), new ComposerReader() - ); + ]; $versioner = new Versioner($readers); self::assertEquals( @@ -81,13 +80,13 @@ public function testFileFirstWithCombine() ); } - public function testHasAVersion() + public function testHasAVersion(): void { $versioner = new Versioner; // This one should have a version $versioner->setReaders( - array(new FileReader('VERSION')) + [new FileReader('VERSION')] ); self::assertTrue( $versioner->has(__DIR__ . '/../../../../projects/composer-file/') @@ -95,7 +94,7 @@ public function testHasAVersion() // Should not have a version $versioner->setReaders( - array(new FileReader('FOO')) + [new FileReader('FOO')] ); self::assertFalse( $versioner->has(__DIR__ . '/../../../../projects/composer-file/') diff --git a/tests/Naneau/ProjectVersioner/Test/VersionerTest.php b/tests/Naneau/ProjectVersioner/Test/VersionerTest.php index 45d7f94..5b2a55f 100644 --- a/tests/Naneau/ProjectVersioner/Test/VersionerTest.php +++ b/tests/Naneau/ProjectVersioner/Test/VersionerTest.php @@ -6,15 +6,15 @@ class VersionerTest extends \PHPUnit\Framework\TestCase { - public function testNoDir() + public function testNoDir(): void { $this->expectException(\InvalidArgumentException::class); - $versioner = new Versioner(array(new MTimeReader('*.txt'))); + $versioner = new Versioner([new MTimeReader('*.txt')]); $versioner->get('foo'); } - public function testNoReaders() + public function testNoReaders(): void { $this->expectException(\RuntimeException::class); From 3139b34140693ebb9aa50cd9e0c4e3fe1ada9225 Mon Sep 17 00:00:00 2001 From: t0mmie Date: Wed, 5 Aug 2020 11:05:44 +0200 Subject: [PATCH 9/9] Set git user for unit test --- tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php index 8eb7adc..0142527 100644 --- a/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php @@ -61,7 +61,9 @@ public function setUp(): void self::execWithDir(['rm -rf %s', 'mkdir %s']); self::execInDir([ 'touch testfile', - 'git init' + 'git init', + 'git config user.email "php@unit.test"', + 'git config user.name "PHPUnit"' ]); // Add commits, with matching tags