Complete unit tests for Application

This commit is contained in:
Phan An 2017-06-10 16:30:54 +01:00
parent 53c4bedf7b
commit 2eb0d7089c
2 changed files with 98 additions and 57 deletions

View file

@ -1,57 +0,0 @@
<?php
namespace Tests\Feature;
use App;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Tests\BrowserKitTestCase;
class ApplicationTest extends BrowserKitTestCase
{
public function setUp()
{
parent::setUp();
@unlink(App::publicPath().'/public/hot');
}
public function testStaticUrlWithoutCDN()
{
config(['koel.cdn.url' => '']);
$this->assertEquals('http://localhost/', App::staticUrl());
$this->assertEquals('http://localhost/foo.css', App::staticUrl('/foo.css '));
}
public function testStaticUrlWithCDN()
{
config(['koel.cdn.url' => 'http://cdn.bar']);
$this->assertEquals('http://cdn.bar/', App::staticUrl());
$this->assertEquals('http://cdn.bar/foo.css', App::staticUrl('/foo.css '));
}
public function testRev()
{
config(['koel.cdn.url' => '']);
$manifestFile = __DIR__.'../../blobs/rev-manifest.json';
$this->assertEquals('http://localhost/public/foo00.css', App::rev('/foo.css', $manifestFile));
config(['koel.cdn.url' => 'http://cdn.bar']);
$this->assertEquals('http://cdn.bar/public/bar00.js', App::rev('/bar.js', $manifestFile));
}
public function testGetLatestVersion()
{
$mock = new MockHandler([
new Response(200, [], file_get_contents(__DIR__.'../../blobs/github-tags.json')),
]);
$client = new Client(['handler' => HandlerStack::create($mock)]);
$this->assertEquals('v1.1.2', App::getLatestVersion($client));
}
}

View file

@ -0,0 +1,98 @@
<?php
namespace Tests\Unit;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Tests\TestCase;
class ApplicationTest extends TestCase
{
public function setUp()
{
parent::setUp();
@unlink(app()->publicPath().'/public/hot');
}
/** @test */
public function static_urls_without_cdn_are_constructed_correctly()
{
// Given we are not using a CDN
config(['koel.cdn.url' => '']);
// When I get the static URLs for the assets
$root = app()->staticUrl();
$assetURL = app()->staticUrl('/foo.css ');
// Then I see they're constructed correctly
$this->assertEquals('http://localhost/', $root);
$this->assertEquals('http://localhost/foo.css', $assetURL);
}
/** @test */
public function static_urls_with_cdn_are_constructed_correctly()
{
// Given we're using a CDN
config(['koel.cdn.url' => 'http://cdn.tld']);
// When I get the static URLs for the assets
$root = app()->staticUrl();
$assetURL = app()->staticUrl('/foo.css ');
// Then I see they're constructed correctly
$this->assertEquals('http://cdn.tld/', $root);
$this->assertEquals('http://cdn.tld/foo.css', $assetURL);
}
/** @test */
public function application_asset_revision_urls_are_constructed_correctly_when_not_using_cdn()
{
// Given we have revisioned assets in the manifest file
$manifestFile = __DIR__.'../../blobs/rev-manifest.json';
// and we're not using a CDN
config(['koel.cdn.url' => '']);
// When I get the static URLs for the assets
$assetURL = app()->rev('/foo.css', $manifestFile);
// Then I see they're constructed correctly
$this->assertEquals('http://localhost/public/foo00.css', $assetURL);
}
/** @test */
public function application_asset_revision_urls_are_constructed_correctly_when_using_cdn()
{
// Given we have revisioned assets in the manifest file
$manifestFile = __DIR__.'../../blobs/rev-manifest.json';
// and we're using a CDN
config(['koel.cdn.url' => 'http://cdn.tld']);
// When I get the static URLs for the assets
$assetURL = app()->rev('/foo.css', $manifestFile);
// Then I see they're constructed correctly
$this->assertEquals('http://cdn.tld/public/foo00.css', $assetURL);
}
/** @test */
public function koels_latest_version_can_be_retrieved()
{
// Given there is a latest version
$latestVersion = 'v1.1.2';
// When I check for the latest version
$mock = new MockHandler([
new Response(200, [], file_get_contents(__DIR__.'../../blobs/github-tags.json')),
]);
$client = new Client(['handler' => HandlerStack::create($mock)]);
$checkedVersion = app()->getLatestVersion($client);
// Then I receive the latest version
$this->assertEquals($latestVersion, $checkedVersion);
}
}