Skip to content

Commit

Permalink
Makes latest version code testable
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Aug 2, 2021
1 parent 83f89bf commit 7e39417
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
22 changes: 20 additions & 2 deletions app/Commands/Concerns/InteractsWithVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

trait InteractsWithVersions
{
/**
* The latest version resolver.
*
* @var callable|null
*/
protected static $latestVersionResolver = null;

/**
* Warns the user about the latest version of Forge CLI.
*
* @return void
*/
protected function ensureLatestVersion()
{
$current = 'v' . config('app.version');
$current = 'v'.config('app.version');

if (version_compare($remote = $this->getLatestVersion(), $current) > 0) {
$this->warnStep(['You are using an outdated version %s of Forge CLI. Please update to %s.', $current, $remote]);
Expand All @@ -25,7 +32,7 @@ protected function ensureLatestVersion()
*/
protected function getLatestVersion()
{
$resolver = function () {
$resolver = static::$latestVersionResolver ?? function () {
$package = json_decode(file_get_contents(
'https://packagist.org/p2/laravel/forge-cli.json'
), true);
Expand All @@ -49,4 +56,15 @@ protected function getLatestVersion()

return $this->config->get('latest_version');
}

/**
* Sets the latest version resolver.
*
* @param callable $resolver
* @return void
*/
public static function resolveLatestVersionUsing($resolver)
{
static::$latestVersionResolver = $resolver;
}
}
2 changes: 1 addition & 1 deletion app/Repositories/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function flush()
* @param string $key
* @param mixed $default
*
* @return array|int|string
* @return array|int|string|null
*/
public function get($key, $default = null)
{
Expand Down
46 changes: 46 additions & 0 deletions tests/Feature/CommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use App\Commands\Command;

beforeEach(function () {
$this->client->shouldReceive('servers')->andReturn([
(object) ['id' => 1, 'name' => 'production', 'ipAddress' => '123.456.789.000'],
]);
});

afterEach(function () {
Command::resolveLatestVersionUsing(null);
});

it('warns the user about the latest unstable versions', function () {
config()->set('app.version', '0.2.1');

Command::resolveLatestVersionUsing(function () {
return 'v0.2.2';
});

$this->artisan('server:list')
->expectsOutput('==> You Are Using An Outdated Version [v0.2.1] Of Forge CLI. Please Update To [v0.2.2].');
});

it('warns the user about the latest stable versions', function () {
config()->set('app.version', '0.2.1');

Command::resolveLatestVersionUsing(function () {
return 'v1.0.0';
});

$this->artisan('server:list')
->expectsOutput('==> You Are Using An Outdated Version [v0.2.1] Of Forge CLI. Please Update To [v1.0.0].');
});

it('do not warns the user if the version is already up-to-date', function () {
config()->set('app.version', '1.0.1');

Command::resolveLatestVersionUsing(function () {
return 'v1.0.1';
});

$this->artisan('server:list')
->doesntExpectOutput('==> You Are Using An Outdated Version [v1.0.1] Of Forge CLI. Please Update To [v1.0.1].');
});
5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Clients\Forge;
use App\Commands\Command;
use App\Repositories\ConfigRepository;
use App\Repositories\ForgeRepository;
use App\Repositories\KeyRepository;
Expand Down Expand Up @@ -41,6 +42,10 @@
);

$this->remote = resolve(RemoteRepository::class);

Command::resolveLatestVersionUsing(function () {
return config('app.version');
});
})->afterEach(function () {
(new Filesystem)->deleteDirectory(base_path('tests/.laravel-forge'));
})->in('Feature', 'Unit');
Expand Down

0 comments on commit 7e39417

Please sign in to comment.