Skip to content

Commit

Permalink
Merge pull request #21 from salahhusa9/facade-tests
Browse files Browse the repository at this point in the history
updater facade tests
  • Loading branch information
salahhusa9 committed Nov 11, 2023
2 parents 3bf1024 + ff32524 commit c67d46d
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Facades/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @method static array newVersionAvailable() Check if a new version is available
* @method static string getLatestVersion() Get the latest version
* @method static string getCurrentVersion() Get the current version
* @method static string getLatestVersionData() Get the latest version data
* @method static array getLatestVersionData() Get the latest version data
* @method static string versions() Get all versions
*/
class Updater extends Facade
Expand Down
2 changes: 1 addition & 1 deletion src/Pipelines/GitPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle($content, Closure $next)
Git::pull();
$checkout = Git::checkout($version);

if (Updater::getCurrentVersion() != $version) {
if ($checkout != 'TEST' and Updater::getCurrentVersion() != $version) {
if (is_callable($content['output'])) {
call_user_func($content['output'], 'git checkout failed: '.$checkout);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Updater
*/
public function update(callable $output = null): string
{
if (! is_callable($output)) {
if (! is_null($output) and ! is_callable($output)) {
throw new \Exception('Output must be callable');
}

Expand Down Expand Up @@ -204,7 +204,7 @@ public function getLatestVersion(): string
public function getLatestVersionData(): array
{
return Cache::remember('latest_version_data', 5, function () {
return app(Repository::class)->getLatestVersionData();
return app(Repository::class)->getLatestVersionData()->toArray();
});
}

Expand Down
4 changes: 4 additions & 0 deletions tests/GitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public function test_get_git_path(): void

public function test_git_auth(): void
{
config()->set('updater.github_token', 'token');
config()->set('updater.github_username', 'username');
config()->set('updater.github_repository', 'repository');

Process::fake([
'git remote set-url origin https://'.config('updater.github_username').':'.config('updater.github_token').'@github.com/'.config('updater.github_username').'/'.config('updater.github_repository').'.git' => Process::result(''),
]);
Expand Down
174 changes: 174 additions & 0 deletions tests/UpdaterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Process;
use Salahhusa9\Updater\Facades\Updater;
use Salahhusa9\Updater\Tests\TestCase;

class UpdaterTest extends TestCase
{
public function testGetCurrentVersion()
{
config()->set('updater.git_path', 'git');

Process::fake([
'git rev-parse --abbrev-ref HEAD' => 'HEAD',
'git describe --tags --abbrev=0' => '1.0.0',
]);

$this->assertEquals('1.0.0', Updater::getCurrentVersion());
}

public function testGetLatestVersion()
{
Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-test/releases/latest' => Http::response([
'tag_name' => '1.0.1',
], 200),
]);

config()->set('updater.git_path', 'git');
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-test');

Process::fake([
'git rev-parse --abbrev-ref HEAD' => 'HEAD',
'git describe --tags --abbrev=0' => '1.0.0',
'git pull' => '',
'git checkout 1.0.1' => 'TEST',
'git remote set-url origin https://salahhusa9:salahhusa9@github.com/salahhusa9/laravel-test.git' => '',
'git fetch' => '',
]);

$this->assertEquals('1.0.1', Updater::getLatestVersion());
}

public function testGetLatestVersionData()
{
Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-test/releases/latest' => Http::response([
'tag_name' => '1.0.1',
], 200),
]);

config()->set('updater.git_path', 'git');
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-test');

Process::fake([
'git rev-parse --abbrev-ref HEAD' => 'HEAD',
'git describe --tags --abbrev=0' => '1.0.0',
'git pull' => '',
'git checkout 1.0.1' => 'TEST',
'git remote set-url origin https://salahhusa9:salahhusa9@github.com/salahhusa9/laravel-test.git' => '',
'git fetch' => '',
]);

$this->assertEquals(['tag_name' => '1.0.1'], Updater::getLatestVersionData());
}

public function testVersions()
{
Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-test/releases' => Http::response([
[
'tag_name' => '1.0.0',
],
[
'tag_name' => '1.1.0',
],
[
'tag_name' => '2.0.0',
],
], 200),
]);

config()->set('updater.git_path', 'git');
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-test');

Process::fake([
'git rev-parse --abbrev-ref HEAD' => 'HEAD',
'git describe --tags --abbrev=0' => '1.0.0',
'git pull' => '',
'git checkout 1.0.1' => 'TEST',
'git remote set-url origin https://salahhusa9:salahhusa9@github.com/salahhusa9/laravel-test.git' => '',
'git fetch' => '',
]);

$this->assertEquals(['1.0.0', '1.1.0', '2.0.0'], Updater::versions());
}

public function testNewVersionAvailable()
{
Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-test/releases/latest' => Http::response([
'tag_name' => '1.0.1',
], 200),
]);

config()->set('updater.git_path', 'git');
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-test');

Process::fake([
'git rev-parse --abbrev-ref HEAD' => 'HEAD',
'git describe --tags --abbrev=0' => '1.0.0',
'git pull' => '',
'git checkout 1.0.1' => 'TEST',
'git remote set-url origin https://salahhusa9:salahhusa9@github.com/salahhusa9/laravel-test.git' => '',
'git fetch' => '',
]);

$this->assertEquals(['current_version' => '1.0.0', 'latest_version' => '1.0.1'], Updater::newVersionAvailable());
}

public function testUpdate()
{
Event::fake();
// Artisan::fake();

Http::fake([
'https://api.github.com/repos/salahhusa9/laravel-test/releases/latest' => Http::response([
'tag_name' => '1.0.1',
], 200),
]);

config()->set('updater.git_path', 'git');
config()->set('updater.github_token', 'salahhusa9');
config()->set('updater.github_username', 'salahhusa9');
config()->set('updater.github_repository', 'laravel-test');

Process::fake([
'git rev-parse --abbrev-ref HEAD' => 'HEAD',
'git describe --tags --abbrev=0' => '1.0.0',
'git pull' => '',
'git checkout 1.0.1' => 'TEST',
'git remote set-url origin https://salahhusa9:salahhusa9@github.com/salahhusa9/laravel-test.git' => '',
'git fetch' => '',
]);

// Artisan::shouldReceive('call')->once()->with('up');
$this->assertEquals('Updated to version 1.0.1', Updater::update());

// Test update failure
Process::fake([
'git rev-parse --abbrev-ref HEAD' => 'HEAD',
'git describe --tags --abbrev=0' => '1.0.0',
'git pull origin HEAD' => '',
'git checkout 1.0.1' => '...',
'git remote set-url origin https://salahhusa9:salahhusa9@github.com/salahhusa9/laravel-test.git' => '',
'git fetch' => '',
]);

$this->expectException(Exception::class);
$this->expectExceptionMessage('git checkout failed: ...');
Updater::update();
}
}

0 comments on commit c67d46d

Please sign in to comment.