diff --git a/.gitignore b/.gitignore index cf8aeb52..e4e2eba5 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ build/ cache/ deploy_docs.sh .vscode/ -composer.lock \ No newline at end of file +composer.lock +.phpunit.result.cache \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 0e3e0d83..b1c4e1b3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: php php: - - 7.1 - 7.2 + - 7.3 before_script: - curl -s https://getcomposer.org/installer | php diff --git a/Dockerfile b/Dockerfile index 9ac980d4..3d1df1f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:7.1.11 +FROM php:7.2-alpine RUN php -r "readfile('https://getcomposer.org/installer');" > composer-setup.php && \ php composer-setup.php && \ diff --git a/README.md b/README.md index fcacc190..249aa8a0 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,17 @@ The api is completely transparent to the end user. You don't have to worry about Requirements ------------ -- php >= 7.0.0 +- php >= `7.2` - *nix system with git installed -*For php 5.x please use GitElephant version 1.x.* +*For php `7.1`, please use GitElephant version 3.x -I work on linux, but the lib should work well with every unix system, as far as a git binary is available. -I don't have a windows installation to test...if someone want to help... +*For php `7.0`, please use GitElephant version 2.x + +*For php `5.x` please use GitElephant version 1.x + +This library is tested on linux, but it should work well with any unix system, as far as a git binary is available. +For windows support, well.. if someone want to help?! Installation ------------ @@ -38,7 +42,7 @@ To install GitElephant with composer you simply need to create a *composer.json* ``` json { "require": { - "cypresslab/gitelephant": "~2.0" + "cypresslab/gitelephant": "~4.0" } } ``` @@ -57,7 +61,7 @@ And an handy autoload file to include in you project in *vendor/autoload.php* How to use ---------- -``` php +```php use GitElephant\Repository; $repo = new Repository('/path/to/git/repository'); // or the factory method @@ -70,14 +74,14 @@ the *Repository* class is the main class where you can find every method you nee **Read repository** -``` php +```php // get the current status $repo->getStatusOutput(); // returns an array of lines of the status message ``` *branches* -``` php +```php $repo->getBranches(); // return an array of Branch objects $repo->getMainBranch(); // return the Branch instance of the current checked out branch $repo->getBranch('master'); // return a Branch instance by its name @@ -87,7 +91,7 @@ $develop = Branch::checkout($repo, 'develop', true); // create and checkout *tags* -``` php +```php $repo->getTags(); // array of Tag instances $repo->getTag('v1.0'); // a Tag instance by name Tag::pick($repo, 'v1.0'); // a Tag instance by name @@ -98,7 +102,7 @@ $repo->getLastTag(); *commits* -``` php +```php $repo->getCommit(); // get a Commit instance of the current HEAD $repo->getCommit('v1.0'); // get a Commit instance for a tag $repo->getCommit('1ac370d'); // full sha or part of it @@ -116,7 +120,7 @@ count($commit); *remotes* -``` php +```php $repo->getRemote('origin'); // a Remote object $repo->getRemotes(); // array of Remote objects @@ -143,7 +147,7 @@ foreach ($log as $commit) { If you build a GitElephant\Status\Status class, you will get a nice api for getting the actual state of the working tree and staging area. -``` php +```php $status = $repo->getStatus(); $status = GitElephant\Status\Status::get($repo); // it's the same... @@ -166,7 +170,7 @@ You could also use GitElephant to manage your git repositories via PHP. Your web server user (like www-data) needs to have access to the folder of the git repository -``` php +```php $repo->init(); // init $repo->cloneFrom("git://github.com/matteosister/GitElephant.git"); // clone @@ -204,7 +208,7 @@ $repo->createTag($repo->getCommit()); If you need to access remote repository you have to install the [ssh2 extension](http://www.php.net/manual/en/book.ssh2.php) and pass a new *Caller* to the repository. *this is a new feature...consider this in a testing phase* -``` php +```php $repo = new Repository('/path/to/git/repository'); $connection = ssh_connect('host', 'port'); // authorize the connection with the method you want @@ -222,7 +226,7 @@ a tree representation of the repository, at a given point in history. **Tree class** -``` php +```php $tree = $repo->getTree(); // retrieve the actual *HEAD* tree $tree = $repo->getTree($repo->getCommit('1ac370d')); // retrieve a tree for a given commit $tree = $repo->getTree('master', 'lib/vendor'); // retrieve a tree for a given path @@ -234,7 +238,7 @@ The Tree class implements *ArrayAccess*, *Countable* and *Iterator* interfaces. You can use it as an array of git objects. -``` php +```php foreach ($tree as $treeObject) { echo $treeObject; } @@ -242,7 +246,7 @@ foreach ($tree as $treeObject) { A Object instance is a php representation of a node in a git tree -``` php +```php echo $treeObject; // the name of the object (folder, file or link) $treeObject->getType(); // one class constant of Object::TYPE_BLOB, Object::TYPE_TREE and Object::TYPE_LINK $treeObject->getSha(); @@ -254,7 +258,7 @@ $treeObject->getPath(); You can also pass a tree object to the repository to get its subtree -``` php +```php $subtree = $repo->getTree('master', $treeObject); ``` @@ -263,7 +267,7 @@ Diffs If you want to check a Diff between two commits the Diff class comes in -``` php +```php // get the diff between the given commit and it parent $diff = $repo->getDiff($repo->getCommit()); // get the diff between two commits @@ -284,7 +288,7 @@ The Diff class implements *ArrayAccess*, *Countable* and *Iterator* interfaces You can iterate over DiffObject -``` php +```php foreach ($diff as $diffObject) { // mode is a constant of the DiffObject class // DiffObject::MODE_INDEX an index change @@ -306,7 +310,7 @@ Every DiffObject can have multiple chunks of changes. For example: You can iterate over DiffObject to get DiffChunks. DiffChunks are the last steps of the Diff process, they are a collection of DiffChunkLine Objects -``` php +```php foreach ($diffObject as $diffChunk) { if (count($diffChunk) > 0) { echo "change detected from line ".$diffChunk->getDestStartLine()." to ".$diffChunk->getDestEndLine(); @@ -331,10 +335,10 @@ $ ./vendor/bin/phpunit # phpunit test suite If you want to run the test suite you should have all the dependencies loaded. -Symfony2 +Symfony -------- -There is a [GitElephantBundle](https://github.com/matteosister/GitElephantBundle) to use this library inside a Symfony2 project. +There is a [GitElephantBundle](https://github.com/matteosister/GitElephantBundle) to use this library inside a Symfony project. Dependencies ------------ @@ -350,9 +354,10 @@ Dependencies Code style ---------- - -* GitElephant follows the [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) -* I'm using [gitflow](https://github.com/nvie/gitflow) +GitElephant follows the: +* [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) +* [Extended Coding Style Guide](https://github.com/php-fig/fig-standards/blob/master/proposed/extended-coding-style-guide.md) +* [gitflow](https://github.com/nvie/gitflow) Want to contribute? ------------------- @@ -361,8 +366,8 @@ Want to contribute? Just remember: -* PSR2 coding standard -* test everything you develop with phpunit +* PSR coding standards +* add tests to everything you develop * if you don't use gitflow, just remember to branch from "develop" and send your PR there. **Please do not send pull requests on the master branch**. Author diff --git a/composer.json b/composer.json index 947cb3b4..3442bfcb 100644 --- a/composer.json +++ b/composer.json @@ -13,15 +13,15 @@ } ], "require": { - "php": ">=7.0.0", - "symfony/process": "~2.0|~3.0|~4.0", - "symfony/filesystem": "~2.0|~3.0|~4.0", - "symfony/finder": "~2.0|~3.0|~4.0", + "php": ">=7.2.0", + "symfony/process": "~2.8|~3.0|~4.0", + "symfony/filesystem": "~2.8|~3.0|~4.0", + "symfony/finder": "~2.8|~3.0|~4.0", "phpcollection/phpcollection": "~0.4" }, "require-dev": { - "php": ">=7.1.0", - "phpunit/phpunit": "~7.0", + "php": ">=7.2.0", + "phpunit/phpunit": "~8.0", "mockery/mockery": "~1.1", "symfony/var-dumper": "3.*|4.*" }, diff --git a/src/GitElephant/Command/Caller/AbstractCaller.php b/src/GitElephant/Command/Caller/AbstractCaller.php index e40cdb06..4c0d7c3a 100644 --- a/src/GitElephant/Command/Caller/AbstractCaller.php +++ b/src/GitElephant/Command/Caller/AbstractCaller.php @@ -74,10 +74,10 @@ public function getBinaryVersion() { if (is_null($this->binaryVersion)) { $version = $this->execute('--version')->getOutput(); - if (!preg_match('/^git version [0-9\.]+$/', $version)) { + if (!preg_match('/^git version [0-9\.]+/', $version)) { throw new \RuntimeException('Could not parse git version. Unexpected format "' . $version . '".'); } - $this->binaryVersion = preg_replace('/^git version ([0-9\.]+)$/', '$1', $version); + $this->binaryVersion = preg_replace('/^git version ([0-9\.]+)/', '$1', $version); } return $this->binaryVersion; diff --git a/src/GitElephant/Status/Status.php b/src/GitElephant/Status/Status.php index 4a123179..d2a6c5d0 100644 --- a/src/GitElephant/Status/Status.php +++ b/src/GitElephant/Status/Status.php @@ -41,6 +41,8 @@ class Status protected $files; /** + * Private constructor in order to follow the singleton pattern + * * @param Repository $repository * * @throws \RuntimeException diff --git a/src/GitElephant/Status/StatusFile.php b/src/GitElephant/Status/StatusFile.php index 62cee451..74248714 100644 --- a/src/GitElephant/Status/StatusFile.php +++ b/src/GitElephant/Status/StatusFile.php @@ -78,7 +78,6 @@ private function __construct(string $x, string $y, string $name, string $renamed $this->y = ' ' === $y ? null : $y; $this->name = $name; $this->renamed = $renamed; - $this->calculateDescription(); } /** @@ -187,6 +186,9 @@ public function setDescription(string $description) */ public function getDescription() { + if ($this->description === null) { + $this->calculateDescription(); + } return $this->description; } diff --git a/src/GitElephant/Utilities.php b/src/GitElephant/Utilities.php index b94bc0d8..c95d31ee 100644 --- a/src/GitElephant/Utilities.php +++ b/src/GitElephant/Utilities.php @@ -1,4 +1,7 @@ */ class Utilities { - /** - * Replace / with the system directory separator - * - * @param string $path the original path - * - * @return mixed - */ - public static function normalizeDirectorySeparator(string $path) - { - return str_replace(DIRECTORY_SEPARATOR, '/', $path); - } - /** * explode an array by lines that match a regular expression * - * @param array $list the original array, should be a non-associative array - * @param string $regexp the regular expression + * @param string[] $list a flat array + * @param string $pattern a regular expression * - * @return array an array of array pieces - * @throws \InvalidArgumentException + * @return string[] */ - public static function pregSplitArray(array $list, string $regexp) + public static function pregSplitArray(array $list, string $pattern): array { - if (static::isAssociative($list)) { - throw new \InvalidArgumentException('pregSplitArray only accepts non-associative arrays.'); - } - $lineNumbers = []; - $arrOut = []; - foreach ($list as $i => $line) { - if (preg_match($regexp, $line)) { - $lineNumbers[] = $i; + $slices = []; + $index = -1; + foreach ($list as $value) { + if (preg_match($pattern, $value) === 1) { + ++$index; } - } - foreach ($lineNumbers as $i => $lineNum) { - if (isset($lineNumbers[$i + 1])) { - $arrOut[] = array_slice($list, $lineNum, $lineNumbers[$i + 1] - $lineNum); - } else { - $arrOut[] = array_slice($list, $lineNum); + if ($index !== -1) { + $slices[$index][] = $value; } } - return $arrOut; + return $slices; } /** - * @param array $list a flat array - * @param string $regexp a regular expression + * @param string[] $list a flat array + * @param string $pattern a regular expression * - * @return array + * @return string[] */ - public static function pregSplitFlatArray(array $list, string $regexp) + public static function pregSplitFlatArray(array $list, string $pattern): array { - $index = 0; $slices = []; - $slice = []; - foreach ($list as $val) { - if (preg_match($regexp, $val) && !empty($slice)) { - $slices[$index] = $slice; + $index = -1; + foreach ($list as $value) { + if (preg_match($pattern, $value) === 1) { ++$index; - $slice = []; } - $slice[] = $val; - } - if (!empty($slice)) { - $slices[$index] = $slice; + + $slices[$index + 1][] = $value; } return $slices; } - - /** - * Tell if an array is associative - * - * @param array $list an array - * - * @return bool - */ - public static function isAssociative(array $list) - { - return array_keys($list) !== range(0, count($list) - 1); - } } diff --git a/tests/GitElephant/Command/BaseCommandTest.php b/tests/GitElephant/Command/BaseCommandTest.php index 5bd89fd1..5f5c39f5 100755 --- a/tests/GitElephant/Command/BaseCommandTest.php +++ b/tests/GitElephant/Command/BaseCommandTest.php @@ -282,6 +282,6 @@ public function testGetBinaryVersion() { $repo = $this->getRepository(); $bc = BaseCommand::getInstance($repo); - $this->assertInternalType('string', $bc->getBinaryVersion()); + $this->assertIsString($bc->getBinaryVersion()); } } diff --git a/tests/GitElephant/Command/BranchCommandTest.php b/tests/GitElephant/Command/BranchCommandTest.php index fd6e08a8..0802866f 100644 --- a/tests/GitElephant/Command/BranchCommandTest.php +++ b/tests/GitElephant/Command/BranchCommandTest.php @@ -28,7 +28,7 @@ class BranchCommandTest extends TestCase /** * setUp, called on every method */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Command/CallerTest.php b/tests/GitElephant/Command/CallerTest.php index 22ebe980..af13333f 100755 --- a/tests/GitElephant/Command/CallerTest.php +++ b/tests/GitElephant/Command/CallerTest.php @@ -27,7 +27,7 @@ class CallerTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->initRepository(); } @@ -56,14 +56,15 @@ public function testGetBinaryPath() public function testGetBinaryVersion() { $c = new Caller(null, $this->repository->getPath()); - $this->assertInternalType('string', $c->getBinaryVersion()); + $this->assertIsString($c->getBinaryVersion()); } /** - * @expectedException \RuntimeException + * testGetError */ public function testGetError() { + $this->expectException(\RuntimeException::class); $binary = null; $caller = new Caller(null, $this->getRepository()->getPath()); $mainCommand = new MainCommand(); @@ -113,10 +114,11 @@ public function testGetRawOutput() } /** - * @expectedException \GitElephant\Exception\InvalidRepositoryPathException + * testRepositoryValidation */ public function testRepositoryValidation() { + $this->expectException(\GitElephant\Exception\InvalidRepositoryPathException::class); $caller = new Caller(null, 'someinvalidpath'); } } diff --git a/tests/GitElephant/Command/CatFileCommandTest.php b/tests/GitElephant/Command/CatFileCommandTest.php index ab04f047..8c07a40e 100644 --- a/tests/GitElephant/Command/CatFileCommandTest.php +++ b/tests/GitElephant/Command/CatFileCommandTest.php @@ -28,7 +28,7 @@ class CatFileCommandTest extends TestCase /** * setUp, called on every method */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Command/CloneCommandTest.php b/tests/GitElephant/Command/CloneCommandTest.php index ba822eaa..79c963ad 100755 --- a/tests/GitElephant/Command/CloneCommandTest.php +++ b/tests/GitElephant/Command/CloneCommandTest.php @@ -32,7 +32,7 @@ class CloneCommandTest extends TestCase /** * set up */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->binaryVersion = exec('git --version | cut -d " " -f 3'); diff --git a/tests/GitElephant/Command/DiffCommandTest.php b/tests/GitElephant/Command/DiffCommandTest.php index b506fd22..2dcdd4d0 100644 --- a/tests/GitElephant/Command/DiffCommandTest.php +++ b/tests/GitElephant/Command/DiffCommandTest.php @@ -32,7 +32,7 @@ class DiffCommandTest extends TestCase /** * set up */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Command/DiffTreeCommandTest.php b/tests/GitElephant/Command/DiffTreeCommandTest.php index 9d92c06a..6306fadc 100644 --- a/tests/GitElephant/Command/DiffTreeCommandTest.php +++ b/tests/GitElephant/Command/DiffTreeCommandTest.php @@ -28,7 +28,7 @@ class DiffTreeCommandTest extends TestCase /** * set up */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Command/FetchCommandTest.php b/tests/GitElephant/Command/FetchCommandTest.php index 04070f45..5c8360ac 100644 --- a/tests/GitElephant/Command/FetchCommandTest.php +++ b/tests/GitElephant/Command/FetchCommandTest.php @@ -35,7 +35,7 @@ class FetchCommandTest extends TestCase /** * set up */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Command/LogCommandTest.php b/tests/GitElephant/Command/LogCommandTest.php index 73a383d3..089878f0 100644 --- a/tests/GitElephant/Command/LogCommandTest.php +++ b/tests/GitElephant/Command/LogCommandTest.php @@ -27,7 +27,7 @@ class LogCommandTest extends TestCase /** * set up */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Command/LsTreeCommandTest.php b/tests/GitElephant/Command/LsTreeCommandTest.php index 4068e3f7..144062c6 100644 --- a/tests/GitElephant/Command/LsTreeCommandTest.php +++ b/tests/GitElephant/Command/LsTreeCommandTest.php @@ -33,7 +33,7 @@ class LsTreeCommandTest extends TestCase /** * setUp function */ - public function setUp() + public function setUp(): void { $this->lsTreeCommand = new LsTreeCommand(); } diff --git a/tests/GitElephant/Command/MainCommandTest.php b/tests/GitElephant/Command/MainCommandTest.php index 34f8fe73..108ff853 100644 --- a/tests/GitElephant/Command/MainCommandTest.php +++ b/tests/GitElephant/Command/MainCommandTest.php @@ -24,7 +24,7 @@ * * @author Matteo Giachino */ - + class MainCommandTest extends TestCase { /** @@ -35,7 +35,7 @@ class MainCommandTest extends TestCase /** * setup */ - public function setUp() + public function setUp(): void { $this->mainCommand = new MainCommand(); } @@ -46,7 +46,7 @@ public function setUp() public function testInit() { $this->assertEquals(MainCommand::GIT_INIT, $this->mainCommand->init()); - $this->assertEquals(MainCommand::GIT_INIT." '--bare'", $this->mainCommand->init(true)); + $this->assertEquals(MainCommand::GIT_INIT . " '--bare'", $this->mainCommand->init(true)); } /** @@ -54,7 +54,7 @@ public function testInit() */ public function testStatus() { - $this->assertEquals("'-c' 'color.status'='false' ".MainCommand::GIT_STATUS, $this->mainCommand->status()); + $this->assertEquals("'-c' 'color.status'='false' " . MainCommand::GIT_STATUS, $this->mainCommand->status()); } /** @@ -62,8 +62,8 @@ public function testStatus() */ public function testAdd() { - $this->assertEquals(MainCommand::GIT_ADD." '--all' '.'", $this->mainCommand->add()); - $this->assertEquals(MainCommand::GIT_ADD." '--all' 'foo'", $this->mainCommand->add('foo')); + $this->assertEquals(MainCommand::GIT_ADD . " '--all' '.'", $this->mainCommand->add()); + $this->assertEquals(MainCommand::GIT_ADD . " '--all' 'foo'", $this->mainCommand->add('foo')); } /** @@ -71,7 +71,7 @@ public function testAdd() */ public function testUnstage() { - $this->assertEquals(MainCommand::GIT_RESET." 'HEAD' -- 'foo'", $this->mainCommand->unstage('foo')); + $this->assertEquals(MainCommand::GIT_RESET . " 'HEAD' -- 'foo'", $this->mainCommand->unstage('foo')); } /** @@ -79,14 +79,20 @@ public function testUnstage() */ public function testCommit() { - $this->assertEquals(MainCommand::GIT_COMMIT." '-m' 'foo'", $this->mainCommand->commit('foo')); - $this->assertEquals(MainCommand::GIT_COMMIT." '-a' '-m' 'foo'", $this->mainCommand->commit('foo', true)); - $this->assertEquals(MainCommand::GIT_COMMIT." '--author' 'example ' '-m' 'foo'", - $this->mainCommand->commit('foo', false, 'example ')); - $this->assertEquals(MainCommand::GIT_COMMIT." '-a' '--author' 'example ' '-m' 'foo'", - $this->mainCommand->commit('foo', true, 'example ')); - $this->assertEquals(MainCommand::GIT_COMMIT." '--author' 'example ' '--allow-empty' '-m' 'foo'", - $this->mainCommand->commit('foo', false, 'example ', true)); + $this->assertEquals(MainCommand::GIT_COMMIT . " '-m' 'foo'", $this->mainCommand->commit('foo')); + $this->assertEquals(MainCommand::GIT_COMMIT . " '-a' '-m' 'foo'", $this->mainCommand->commit('foo', true)); + $this->assertEquals( + MainCommand::GIT_COMMIT . " '--author' 'example ' '-m' 'foo'", + $this->mainCommand->commit('foo', false, 'example ') + ); + $this->assertEquals( + MainCommand::GIT_COMMIT . " '-a' '--author' 'example ' '-m' 'foo'", + $this->mainCommand->commit('foo', true, 'example ') + ); + $this->assertEquals( + MainCommand::GIT_COMMIT . " '--author' 'example ' '--allow-empty' '-m' 'foo'", + $this->mainCommand->commit('foo', false, 'example ', true) + ); } /** @@ -94,6 +100,6 @@ public function testCommit() */ public function testCheckout() { - $this->assertEquals(MainCommand::GIT_CHECKOUT." '-q' 'master'", $this->mainCommand->checkout('master')); + $this->assertEquals(MainCommand::GIT_CHECKOUT . " '-q' 'master'", $this->mainCommand->checkout('master')); } } diff --git a/tests/GitElephant/Command/MergeCommandTest.php b/tests/GitElephant/Command/MergeCommandTest.php index d12c630a..ebdeb8eb 100644 --- a/tests/GitElephant/Command/MergeCommandTest.php +++ b/tests/GitElephant/Command/MergeCommandTest.php @@ -20,7 +20,7 @@ class MergeCommandTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); @@ -44,12 +44,11 @@ public function testMerge() /** * MergeCommand should throw an exception when both --ff-only and --no-ff flags were set. - * - * @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException */ public function test_exception_when_calling_merge_with_conflicting_ff_arguments() { $branch = $this->getRepository()->getBranch('test'); + $this->expectException(\Symfony\Component\Process\Exception\InvalidArgumentException::class); MergeCommand::getInstance()->merge($branch, "test msg", array('--ff-only', '--no-ff')); } } diff --git a/tests/GitElephant/Command/MvCommandTest.php b/tests/GitElephant/Command/MvCommandTest.php index 82c36a11..cabd1cfb 100644 --- a/tests/GitElephant/Command/MvCommandTest.php +++ b/tests/GitElephant/Command/MvCommandTest.php @@ -20,7 +20,7 @@ class MvCommandTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->getRepository()->init(); $this->addFile('test'); diff --git a/tests/GitElephant/Command/PullCommandTest.php b/tests/GitElephant/Command/PullCommandTest.php index de6486d7..1d928d26 100644 --- a/tests/GitElephant/Command/PullCommandTest.php +++ b/tests/GitElephant/Command/PullCommandTest.php @@ -36,7 +36,7 @@ class PullCommandTest extends TestCase /** * set up */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Command/PushCommandTest.php b/tests/GitElephant/Command/PushCommandTest.php index b0759b6b..0edda9e3 100644 --- a/tests/GitElephant/Command/PushCommandTest.php +++ b/tests/GitElephant/Command/PushCommandTest.php @@ -36,7 +36,7 @@ class PushCommandTest extends TestCase /** * set up */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Command/RemoteCommandTest.php b/tests/GitElephant/Command/RemoteCommandTest.php index 253fae34..cef313e1 100644 --- a/tests/GitElephant/Command/RemoteCommandTest.php +++ b/tests/GitElephant/Command/RemoteCommandTest.php @@ -28,7 +28,7 @@ class RemoteCommandTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->initRepository(); $repo = $this->getRepository(); diff --git a/tests/GitElephant/Command/ResetCommandTest.php b/tests/GitElephant/Command/ResetCommandTest.php index 6bdea6ba..9506b547 100644 --- a/tests/GitElephant/Command/ResetCommandTest.php +++ b/tests/GitElephant/Command/ResetCommandTest.php @@ -16,7 +16,7 @@ class ResetCommandTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); @@ -28,6 +28,6 @@ public function setUp() public function testResetHard() { $rstc = ResetCommand::getInstance(); - $this->assertEquals("reset '--hard' 'dbeac'",$rstc->reset('dbeac',array(ResetCommand::OPTION_HARD))); + $this->assertEquals("reset '--hard' 'dbeac'", $rstc->reset('dbeac', array(ResetCommand::OPTION_HARD))); } } diff --git a/tests/GitElephant/Objects/BranchTest.php b/tests/GitElephant/Objects/BranchTest.php index 904d9c27..ea310846 100644 --- a/tests/GitElephant/Objects/BranchTest.php +++ b/tests/GitElephant/Objects/BranchTest.php @@ -45,22 +45,21 @@ public function testGetMatches() /** * testGetMatchesErrors - * - * @expectedException \InvalidArgumentException */ public function testGetMatchesShortShaError() { // short sha + $this->expectException(\InvalidArgumentException::class); $matches = Branch::getMatches('* develop 45eac8c31adfbbf633824cee6ce8cc5040b3351 test message'); } /** * testGetMatchesErrors * - * @expectedException \InvalidArgumentException */ public function testGetMatchesNoSpaceError() { + $this->expectException(\InvalidArgumentException::class); $matches = Branch::getMatches('* develop 45eac8c31adfbbf633824cee6ce8cc5040b33511test message'); } diff --git a/tests/GitElephant/Objects/Commit/MessageTest.php b/tests/GitElephant/Objects/Commit/MessageTest.php index 467087f7..0db38d8d 100644 --- a/tests/GitElephant/Objects/Commit/MessageTest.php +++ b/tests/GitElephant/Objects/Commit/MessageTest.php @@ -29,7 +29,7 @@ class MessageTest extends \PHPUnit\Framework\TestCase */ protected $msg; - protected function setUp() + protected function setUp(): void { $this->shortMsg = 'This is the short message'; $this->longMsg = <<initRepository(); $mainCommand = new MainCommand(); @@ -245,5 +245,4 @@ public function testCommitWithTag() $commitTags = $commit->getTags(); $this->assertEquals('1.0.0', $commitTags[0]->getName()); } - } diff --git a/tests/GitElephant/Objects/Diff/DiffTest.php b/tests/GitElephant/Objects/Diff/DiffTest.php index f993b80a..c1b5acf0 100644 --- a/tests/GitElephant/Objects/Diff/DiffTest.php +++ b/tests/GitElephant/Objects/Diff/DiffTest.php @@ -35,7 +35,7 @@ class DiffTest extends TestCase { - public function setUp() + public function setUp(): void { $this->initRepository(); } diff --git a/tests/GitElephant/Objects/LogRangeTest.php b/tests/GitElephant/Objects/LogRangeTest.php index 0db8039e..1ffc0dc3 100644 --- a/tests/GitElephant/Objects/LogRangeTest.php +++ b/tests/GitElephant/Objects/LogRangeTest.php @@ -43,7 +43,7 @@ class LogRangeTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->initRepository(null, 0); $this->initRepository(null, 1); @@ -72,7 +72,7 @@ public function testCreateFromCommand() public function testToArray() { $logRange = new LogRange($this->getRepository(0), $this->firstCommit, $this->lastCommit); - $this->assertInternalType('array', $logRange->toArray()); + $this->assertIsArray($logRange->toArray()); $this->assertCount(9, $logRange->toArray()); } @@ -92,7 +92,7 @@ public function testArrayAccess() $this->assertTrue(isset($logRange[0])); foreach ($logRange as $key => $commit) { $this->assertInstanceOf('GitElephant\Objects\Commit', $commit); - $this->assertInternalType('int', $key); + $this->assertIsInt($key); } $r = $this->getRepository(1); $logRange->setRepository($r); @@ -100,19 +100,21 @@ public function testArrayAccess() } /** - * @expectedException \RuntimeException + * testExceptionOnSet */ public function testExceptionOnSet() { + $this->expectException(\RuntimeException::class); $logRange = new LogRange($this->getRepository(0), $this->firstCommit, $this->lastCommit); $logRange[9] = 'test'; } /** - * @expectedException \RuntimeException + * testExceptionOnUnSet */ public function testExceptionOnUnset() { + $this->expectException(\RuntimeException::class); $logRange = new LogRange($this->getRepository(0), $this->firstCommit, $this->lastCommit); unset($logRange[0]); } diff --git a/tests/GitElephant/Objects/LogTest.php b/tests/GitElephant/Objects/LogTest.php index 44748137..756ccde6 100644 --- a/tests/GitElephant/Objects/LogTest.php +++ b/tests/GitElephant/Objects/LogTest.php @@ -27,7 +27,7 @@ class LogTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->getRepository()->init(); @@ -143,7 +143,7 @@ public function testLogToArray() $log = $this->getRepository()->getLog(); $this->assertTrue(is_array($log->toArray())); - $this->assertInternalType('array', $log->toArray()); + $this->assertIsArray($log->toArray()); $this->assertEquals($log->count(), count($log->toArray())); } @@ -168,6 +168,5 @@ public function testLogCreatedFromOutputLines() $log = Log::createFromOutputLines($this->getRepository(), $this->caller->execute($command)->getOutputLines()); $this->assertInstanceOf('GitElephant\Objects\Log', $log); $this->assertCount(1, $log); - } } diff --git a/tests/GitElephant/Objects/NodeObjectTest.php b/tests/GitElephant/Objects/NodeObjectTest.php index e67097b3..cd3f378e 100644 --- a/tests/GitElephant/Objects/NodeObjectTest.php +++ b/tests/GitElephant/Objects/NodeObjectTest.php @@ -10,7 +10,7 @@ class NodeObjectTest extends TestCase { - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Objects/RemoteTest.php b/tests/GitElephant/Objects/RemoteTest.php index e55c98c4..8d2b1a85 100644 --- a/tests/GitElephant/Objects/RemoteTest.php +++ b/tests/GitElephant/Objects/RemoteTest.php @@ -42,7 +42,7 @@ class RemoteTest extends TestCase /** * test setup */ - public function setUp() + public function setUp(): void { $this->initRepository(); $repo = $this->getRepository(); @@ -353,9 +353,9 @@ public function getMockRemote() $verboseOutput = explode("\n", $sample); $mockRemote = $this->getMockBuilder(Remote::class) - ->disableOriginalConstructor() - ->setMethods(['getShowOutput', 'getVerboseOutput']) - ->getMock(); + ->disableOriginalConstructor() + ->setMethods(['getShowOutput', 'getVerboseOutput']) + ->getMock(); $mockRemote->expects($this->any()) ->method('getShowOutput') diff --git a/tests/GitElephant/Objects/TagTest.php b/tests/GitElephant/Objects/TagTest.php index a326d9ad..514d477d 100644 --- a/tests/GitElephant/Objects/TagTest.php +++ b/tests/GitElephant/Objects/TagTest.php @@ -49,10 +49,11 @@ public function testTagFromStartPoint() } /** - * @expectedException \InvalidArgumentException + * testNonExistentTag */ public function testNonExistentTag() { + $this->expectException(\InvalidArgumentException::class); $this->getRepository()->init(); $this->addFile('foo'); $this->getRepository()->commit('commit1', true); diff --git a/tests/GitElephant/Objects/TreeObjectTest.php b/tests/GitElephant/Objects/TreeObjectTest.php index a99fbfb1..8c13f483 100644 --- a/tests/GitElephant/Objects/TreeObjectTest.php +++ b/tests/GitElephant/Objects/TreeObjectTest.php @@ -6,7 +6,7 @@ class TreeObjectTest extends TestCase { - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/Objects/TreeTest.php b/tests/GitElephant/Objects/TreeTest.php index 4cc8c603..89feb5cd 100644 --- a/tests/GitElephant/Objects/TreeTest.php +++ b/tests/GitElephant/Objects/TreeTest.php @@ -29,7 +29,7 @@ class TreeTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->initRepository(); $this->getRepository()->init(); diff --git a/tests/GitElephant/RepositoryTest.php b/tests/GitElephant/RepositoryTest.php index 6248eab4..a8f9c0fb 100755 --- a/tests/GitElephant/RepositoryTest.php +++ b/tests/GitElephant/RepositoryTest.php @@ -31,7 +31,7 @@ class RepositoryTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->initRepository(); } @@ -297,8 +297,7 @@ public function testMerge() $this->assertEquals(3, count($this->getRepository()->getTree())); try { $this->getRepository()->merge($this->getRepository()->getBranch('branch2'), '', 'ff-only'); - } - catch (\RuntimeException $e) { + } catch (\RuntimeException $e) { return; } $this->fail("Merge should have produced a runtime exception."); @@ -559,11 +558,11 @@ public function testCheckoutTag() $tag = $this->getRepository()->getTag('v0.0.1'); $this->assertInstanceOf('GitElephant\Objects\Tag', $tag); $lastCommit = $this->getRepository()->getCommit(); - $this->assertNotContains('detached', implode(' ', $this->getRepository()->getStatusOutput())); + $this->assertStringNotContainsString('detached', implode(' ', $this->getRepository()->getStatusOutput())); $this->getRepository()->checkout($tag); $newCommit = $this->getRepository()->getCommit(); $this->assertNotEquals($newCommit->getSha(), $lastCommit->getSha()); - $this->assertContains('detached', implode(' ', $this->getRepository()->getStatusOutput())); + $this->assertStringContainsString('detached', implode(' ', $this->getRepository()->getStatusOutput())); } /** @@ -876,7 +875,6 @@ public function testIsBare() $r->init(true); $this->assertEquals(true, $r->isBare()); - } /** @@ -1060,7 +1058,7 @@ public function testStashShow() $this->getRepository()->commit('Test commit', true); $this->addFile('test2'); $this->getRepository()->stash('My stash', true); - $this->assertInternalType('string', $this->getRepository()->stashShow(0)); + $this->assertIsString($this->getRepository()->stashShow(0)); } /** @@ -1130,7 +1128,7 @@ public function testStashCreate() $this->addFile('test'); $this->getRepository()->commit('Test commit', true); $objectName = $this->getRepository()->stashCreate(); - $this->assertInternalType('string', $objectName); + $this->assertIsString($objectName); } /** @@ -1148,5 +1146,4 @@ public function testStashClear() $this->getRepository()->stashClear(); $this->assertCount(0, $this->getRepository()->stashList()); } - } diff --git a/tests/GitElephant/Status/StatusTest.php b/tests/GitElephant/Status/StatusTest.php index bcc03295..677b356a 100644 --- a/tests/GitElephant/Status/StatusTest.php +++ b/tests/GitElephant/Status/StatusTest.php @@ -20,7 +20,7 @@ class StatusTest extends TestCase /** * setUp */ - public function setUp() + public function setUp(): void { $this->getRepository()->init(); $this->addFile('initial'); diff --git a/tests/GitElephant/TestCase.php b/tests/GitElephant/TestCase.php index 8ec076c7..d5f47fea 100644 --- a/tests/GitElephant/TestCase.php +++ b/tests/GitElephant/TestCase.php @@ -104,7 +104,7 @@ protected function initRepository($name = null, $index = null) } } - protected function tearDown() + protected function tearDown(): void { $fs = new Filesystem(); if (is_array($this->repository)) { @@ -133,8 +133,7 @@ protected function addFile($name, $folder = null, $content = null, $repository = $path = $repository->getPath(); } $filename = $folder == null ? - $path . DIRECTORY_SEPARATOR . $name : - $path . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $name; + $path . DIRECTORY_SEPARATOR . $name : $path . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $name; $handle = fopen($filename, 'w'); $fileContent = $content == null ? 'test content' : $content; $this->assertTrue(false !== fwrite($handle, $fileContent), sprintf('unable to write the file %s', $name)); diff --git a/tests/GitElephant/UtilitiesTest.php b/tests/GitElephant/UtilitiesTest.php index b1006a97..ab197fc3 100644 --- a/tests/GitElephant/UtilitiesTest.php +++ b/tests/GitElephant/UtilitiesTest.php @@ -1,5 +1,7 @@ */ - -class UtilitiesTest extends TestCase +final class UtilitiesTest extends TestCase { - protected $arr = array( + private $arr = [ 'a', 'b', 'c', '1', 'd', 'b', - 'e' - ); - - /** - * testNormalizeDirectorySeparator - */ - public function testNormalizeDirectorySeparator() - { - $this->assertEquals('foo/bar', Utilities::normalizeDirectorySeparator('foo/bar')); - } + 'e', + ]; /** - * @covers GitElephant\Utilities::pregSplitArray + * @dataProvider pregSplitArrayProvider() + * + * @covers \GitElephant\Utilities::pregSplitArray */ - public function testPregSplitArray() + public function testPregSplitArray(array $expected, array $list, string $pattern) { $this->assertEquals( - array( - array('b', 'c', '1', 'd'), - array('b', 'e') - ), - Utilities::pregSplitArray($this->arr, '/^b$/') - ); - $this->assertEquals( - array( - array('1', 'd', 'b', 'e') - ), - Utilities::pregSplitArray($this->arr, '/^\d$/') + $expected, + Utilities::pregSplitArray( + $list, + $pattern + ) ); } + /** + * @dataProvider + */ public function testPregSplitFlatArray() { $this->assertEquals( - array( - array('a'), - array('b', 'c', '1', 'd'), - array('b', 'e')), + [ + ['a'], + ['b', 'c', '1', 'd'], + ['b', 'e'], + ], Utilities::pregSplitFlatArray($this->arr, '/^b$/') ); } - /** - * @covers GitElephant\Utilities::isAssociative - */ - public function testIsAssociative() + public function pregSplitArrayProvider(): array { - $this->assertFalse(Utilities::isAssociative(array(1, 2))); - $this->assertTrue(Utilities::isAssociative(array(1 => 1, 2 => 2))); - $this->assertFalse(Utilities::isAssociative(array(0 => 1, 1 => 2))); + return [ + [ + [ + ['b', 'c', '1', 'd'], + ['b', 'e'], + ], + $this->arr, + '/^b$/', + ], + [ + [ + ['1', 'd', 'b', 'e'], + ], + $this->arr, + '/^\d$/', + ], + ]; } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 73008d4f..0e16e524 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,7 @@