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 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 9dfeebc..0000000 --- a/.travis.yml +++ /dev/null @@ -1,16 +0,0 @@ -language: php -php: - - 5.6 - - 5.5 - - 5.4 - - 5.3 - - hhvm - -# 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/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. 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 caa28a8..4bc8387 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +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/"} + "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": { - "symfony/finder": "~2.5" + "ext-json": "*", + "php": "7.2.* || 7.3.* || 7.4.*", + "symfony/finder": "~4.4" }, "require-dev": { - "phpunit/phpunit": "~4.2" + "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); + 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 9ed31a5..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 parent */ - 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 new file mode 100644 index 0000000..521e106 --- /dev/null +++ b/tests/Naneau/ProjectVersioner/Test/Reader/ComposerTest.php @@ -0,0 +1,43 @@ +get($directory)); + } + + public function testPackageRead(): void + { + $directory = __DIR__ . '/../../../../projects/composer'; + + $readers = [new ComposerPackageReader('symfony/filesystem')]; + + $versioner = new Versioner($readers); + + self::assertEquals('v2.5.4', $versioner->get($directory)); + } + + public function testComposerJsonRead(): void + { + $directory = __DIR__ . '/../../../../projects/composer'; + + $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 new file mode 100644 index 0000000..a9d0d12 --- /dev/null +++ b/tests/Naneau/ProjectVersioner/Test/Reader/FileTest.php @@ -0,0 +1,19 @@ +get($directory)); + } +} diff --git a/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php new file mode 100644 index 0000000..92fb486 --- /dev/null +++ b/tests/Naneau/ProjectVersioner/Test/Reader/FinderTest.php @@ -0,0 +1,88 @@ +get($directory)); + } + + public function testContents(): void + { + $versioner = new Versioner([new ContentsReader('*.php')]); + + self::assertEquals( + 'db9d80', + $versioner->get( + __DIR__ . '/../../../../projects/finder' + ) + ); + } + + public function testEmptyNames(): void + { + $directory = __DIR__ . '/../../../../projects/finder'; + + $versioner = new Versioner([new MTimeReader()]); + + // Set the time to now for one of the files + $time = time(); + touch($directory . '/DirectoryOne/FileFour.txt', $time); + + self::assertEquals($time, $versioner->get($directory)); + } + + public function testEmptyNamesWithFinder(): void + { + $directory = __DIR__ . '/../../../../projects/finder'; + + $finderTxt = new Finder; + $finderTxt->name('*.txt'); + $versionerTxt = new Versioner([new MTimeReader(null, $finderTxt)]); + + $finderPhp = new Finder; + $finderPhp->name('*.php'); + $versionerPhp = new Versioner([new MTimeReader(null, $finderPhp)]); + + $timeThree = time(); + $timeFour = $timeThree - 10; + + touch($directory . '/DirectoryOne/FileThree.php', $timeThree); + touch($directory . '/DirectoryOne/FileFour.txt', $timeFour); + + self::assertEquals($timeThree, $versionerPhp->get($directory)); + self::assertEquals($timeFour, $versionerTxt->get($directory)); + } + + public function testNamesAndFinder(): void + { + $directory = __DIR__ . '/../../../../projects/finder'; + + $finder = new Finder; + $finder->name('*.txt'); + $versioner = new Versioner([new MTimeReader('*.php', $finder)]); + + $timeThree = time(); + $timeFour = $timeThree - 10; + + touch($directory . '/DirectoryOne/FileThree.php', $timeThree); + touch($directory . '/DirectoryOne/FileFour.txt', $timeFour); + + self::assertEquals($timeThree, $versioner->get($directory)); + } +} diff --git a/tests/Reader/GitExecTest.php b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php similarity index 50% rename from tests/Reader/GitExecTest.php rename to tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php index 691f27f..0142527 100644 --- a/tests/Reader/GitExecTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Reader/GitExecTest.php @@ -1,81 +1,74 @@ assertEquals($version, $versioner->get(self::getDirectory())); + 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)]); - $this->assertEquals($version, $versioner->get(self::getDirectory())); + 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]); - $this->assertEquals($version, $versioner->get(self::getDirectory())); + self::assertEquals($version, $versioner->get(self::getDirectory())); } - public function testTagRead() + public function testTagRead(): void { - $versioner = new Versioner(array(new GitTagExecReader)); - $this->assertEquals('0.0.2', $versioner->get(self::getDirectory())); + $versioner = new Versioner([new GitTagExecReader]); + self::assertEquals('0.0.2', $versioner->get(self::getDirectory())); } /** * Set up fixtures - * - * @return void - **/ - public function setUp() + */ + 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' - )); + 'git init', + 'git config user.email "php@unit.test"', + 'git config user.name "PHPUnit"' + ]); // Add commits, with matching tags for ($x = 0; $x < 3; $x++) { - self::execInDir(array( + self::execInDir([ // Contained in tag sprintf('touch test.%d', $x), @@ -87,28 +80,27 @@ public function setUp() 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() + */ + public function tearDown(): void { - self::execWithDir(array('rm -rf %s')); + self::execWithDir(['rm -rf %s']); } /** * Exec a sert of shell commands * - * @return void - **/ - 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); @@ -117,22 +109,22 @@ 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) { - throw new RuntimeException(sprintf( + throw new \RuntimeException(sprintf( 'Could not init git: "%s" returned %d, %s', $inflectedCmd, $return, @@ -142,16 +134,14 @@ 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'; + return __DIR__ . '/../../../../projects/git'; } } diff --git a/tests/Versioner/CombineReadersTest.php b/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php similarity index 54% rename from tests/Versioner/CombineReadersTest.php rename to tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php index 9c8cd88..12e1ce9 100644 --- a/tests/Versioner/CombineReadersTest.php +++ b/tests/Naneau/ProjectVersioner/Test/Versioner/CombineReadersTest.php @@ -1,103 +1,103 @@ assertEquals( + self::assertEquals( 'v2.5.4', $versioner->get( - __DIR__ . '/../projects/composer-file/' + __DIR__ . '/../../../../projects/composer-file/' ) ); } - public function testFileFirst() + public function testFileFirst(): void { - $readers = array( + $readers = [ 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/' + __DIR__ . '/../../../../projects/composer-file/' ) ); } - public function testComposerFirstWithCombine() + public function testComposerFirstWithCombine(): void { - $readers = array( + $readers = [ 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/', + __DIR__ . '/../../../../projects/composer-file/', '_' // use _ for separator ) ); } - public function testFileFirstWithCombine() + public function testFileFirstWithCombine(): void { - $readers = array( + $readers = [ 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/' + __DIR__ . '/../../../../projects/composer-file/' ) ); } - 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')] ); - return $this->assertTrue( - $versioner->has(__DIR__ . '/../projects/composer-file/') + self::assertTrue( + $versioner->has(__DIR__ . '/../../../../projects/composer-file/') ); // Should not have a version $versioner->setReaders( - array(new FileReader('FOO')) + [new FileReader('FOO')] ); - return $this->assertFalse( - $versioner->has(__DIR__ . '/../projects/composer-file/') + self::assertFalse( + $versioner->has(__DIR__ . '/../../../../projects/composer-file/') ); } } diff --git a/tests/Naneau/ProjectVersioner/Test/VersionerTest.php b/tests/Naneau/ProjectVersioner/Test/VersionerTest.php new file mode 100644 index 0000000..5b2a55f --- /dev/null +++ b/tests/Naneau/ProjectVersioner/Test/VersionerTest.php @@ -0,0 +1,25 @@ +expectException(\InvalidArgumentException::class); + + $versioner = new Versioner([new MTimeReader('*.txt')]); + $versioner->get('foo'); + } + + public function testNoReaders(): void + { + $this->expectException(\RuntimeException::class); + + $versioner = new Versioner(); + + $versioner->get('foo'); + } +} diff --git a/tests/Reader/ComposerTest.php b/tests/Reader/ComposerTest.php deleted file mode 100644 index 5191f39..0000000 --- a/tests/Reader/ComposerTest.php +++ /dev/null @@ -1,41 +0,0 @@ -assertEquals('aa1f22', $versioner->get($directory)); - } - - public function testPackageRead() - { - $directory = __DIR__ . '/../projects/composer'; - - $readers = array(new ComposerPackageReader('symfony/filesystem')); - - $versioner = new Versioner($readers); - - $this->assertEquals('v2.5.4', $versioner->get($directory)); - } - - public function testComposerJsonRead() - { - $directory = __DIR__ . '/../projects/composer'; - - $readers = array(new ComposerJsonReader); - $versioner = new Versioner($readers); - - $this->assertEquals('1.0.0', $versioner->get($directory)); - } -} diff --git a/tests/Reader/FileTest.php b/tests/Reader/FileTest.php deleted file mode 100644 index 32c593f..0000000 --- a/tests/Reader/FileTest.php +++ /dev/null @@ -1,18 +0,0 @@ -assertEquals('5.4.3', $versioner->get($directory)); - } -} diff --git a/tests/Reader/FinderTest.php b/tests/Reader/FinderTest.php deleted file mode 100644 index c94d4ba..0000000 --- a/tests/Reader/FinderTest.php +++ /dev/null @@ -1,87 +0,0 @@ -assertEquals($time, $versioner->get($directory)); - } - - public function testContents() - { - $versioner = new Versioner(array(new ContentsReader('*.php'))); - - $this->assertEquals( - 'db9d80', - $versioner->get( - __DIR__ . '/../projects/finder' - ) - ); - } - - public function testEmptyNames() - { - $directory = __DIR__ . '/../projects/finder'; - - $versioner = new Versioner(array(new MTimeReader())); - - // Set the time to now for one of the files - $time = time(); - touch($directory . '/DirectoryOne/FileFour.txt', $time); - - $this->assertEquals($time, $versioner->get($directory)); - } - - public function testEmptyNamesWithFinder() - { - $directory = __DIR__ . '/../projects/finder'; - - $finderTxt = new Finder; - $finderTxt->name('*.txt'); - $versionerTxt = new Versioner(array(new MTimeReader(null, $finderTxt))); - - $finderPhp = new Finder; - $finderPhp->name('*.php'); - $versionerPhp = new Versioner(array(new MTimeReader(null, $finderPhp))); - - $timeThree = time(); - $timeFour = $timeThree - 10; - - touch($directory . '/DirectoryOne/FileThree.php', $timeThree); - touch($directory . '/DirectoryOne/FileFour.txt', $timeFour); - - $this->assertEquals($timeThree, $versionerPhp->get($directory)); - $this->assertEquals($timeFour, $versionerTxt->get($directory)); - } - - public function testNamesAndFinder() - { - $directory = __DIR__ . '/../projects/finder'; - - $finder = new Finder; - $finder->name('*.txt'); - $versioner = new Versioner(array(new MTimeReader('*.php', $finder))); - - $timeThree = time(); - $timeFour = $timeThree - 10; - - touch($directory . '/DirectoryOne/FileThree.php', $timeThree); - touch($directory . '/DirectoryOne/FileFour.txt', $timeFour); - - $this->assertEquals($timeThree, $versioner->get($directory)); - } -} diff --git a/tests/VersionerTest.php b/tests/VersionerTest.php deleted file mode 100644 index 1cb241b..0000000 --- a/tests/VersionerTest.php +++ /dev/null @@ -1,26 +0,0 @@ -get('foo'); - } - - /** - * @expectedException RuntimeException - **/ - public function testNoReaders() - { - $versioner = new Versioner(); - - $versioner->get('foo'); - } -}