Skip to content

Commit

Permalink
Tidies up tests
Browse files Browse the repository at this point in the history
Use phpdoc notation and snakecase for better readability. Arrange directly in test methods to provide clarity. Simplify test names.
  • Loading branch information
BenConstable committed Jun 4, 2017
1 parent d6f6279 commit 7e156bb
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 158 deletions.
23 changes: 15 additions & 8 deletions tests/Localize/DeterminerManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Mockery;
use PHPUnit_Framework_TestCase;
use BenConstable\Localize\DeterminerManager;
use BenConstable\Localize\Determiners;
use BenConstable\Localize\DeterminerManager;

class DeterminerManagerTest extends PHPUnit_Framework_TestCase
{
Expand All @@ -28,42 +28,48 @@ public function tearDown()
Mockery::close();
}

public function testThatItCreatesACookieDeterminer()
/** @test **/
public function create_a_cookie_determiner()
{
$manager = new DeterminerManager($this->app);

$this->assertInstanceOf(Determiners\Cookie::class, $manager->driver('cookie'));
}

public function testThatItCreatesAHostDeterminer()
/** @test **/
public function create_a_host_determiner()
{
$manager = new DeterminerManager($this->app);

$this->assertInstanceOf(Determiners\Host::class, $manager->driver('host'));
}

public function testThatItCreatesAParameterDeterminer()
/** @test **/
public function create_a_parameter_determiner()
{
$manager = new DeterminerManager($this->app);

$this->assertInstanceOf(Determiners\Parameter::class, $manager->driver('parameter'));
}

public function testThatItCreatesASessionDeterminer()
/** @test **/
public function create_a_session_determiner()
{
$manager = new DeterminerManager($this->app);

$this->assertInstanceOf(Determiners\Session::class, $manager->driver('session'));
}

public function testThatItCreatesAHeaderDeterminer()
/** @test **/
public function create_a_header_determiner()
{
$manager = new DeterminerManager($this->app);

$this->assertInstanceOf(Determiners\Header::class, $manager->driver('header'));
}

public function testThatItCreatesAStackDeterminer()
/** @test **/
public function create_a_stack_determiner()
{
$manager = new DeterminerManager($this->app);

Expand All @@ -76,7 +82,8 @@ public function testThatItCreatesAStackDeterminer()
$this->assertInstanceOf(Determiners\Parameter::class, $determiner->getDeterminers()->first());
}

public function testThatItReturnsADefaultDeterminer()
/** @test **/
public function create_a_default_determiner()
{
$manager = new DeterminerManager($this->app);

Expand Down
31 changes: 12 additions & 19 deletions tests/Localize/Determiners/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,42 @@

class CookieTest extends PHPUnit_Framework_TestCase
{
private $cookieName;
private $fallback;
private $determiner;

public function setUp()
{
$this->cookieName = 'locale';
$this->fallback = 'de';
$this->determiner = (new Cookie($this->cookieName))->setFallback($this->fallback);
}

public function tearDown()
{
Mockery::close();
}

public function testDeterminesLocaleFromCookie()
/** @test **/
public function determine_locale()
{
$locale = 'en';

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('cookie')
->with($this->cookieName, $this->fallback)
->with('locale', null)
->andReturn($locale);

$result = $this->determiner->determineLocale($request);
$result = (new Cookie('locale'))->determineLocale($request);

$this->assertEquals($result, $locale);
}

public function testReturnsFallbackLocaleIfNeeded()
/** @test **/
public function fallback_if_no_locale_found()
{
$fallback = 'de';

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('cookie')
->with($this->cookieName, $this->fallback)
->andReturn($this->fallback);
->with('locale', $fallback)
->andReturn($fallback);

$result = $this->determiner->determineLocale($request);
$result = (new Cookie('locale'))->setFallback($fallback)->determineLocale($request);

$this->assertEquals($result, $this->fallback);
$this->assertEquals($result, $fallback);
}
}
31 changes: 12 additions & 19 deletions tests/Localize/Determiners/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,42 @@

class HeaderTest extends PHPUnit_Framework_TestCase
{
private $header;
private $fallback;
private $determiner;

public function setUp()
{
$this->header = 'Accept-Language';
$this->fallback = 'de';
$this->determiner = (new Header($this->header))->setFallback($this->fallback);
}

public function tearDown()
{
Mockery::close();
}

public function testDeterminesLocaleFromHeader()
/** @test **/
public function determine_locale()
{
$locale = 'en';

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('header')
->with($this->header, $this->fallback)
->with('Accept-Language', null)
->andReturn($locale);

$result = $this->determiner->determineLocale($request);
$result = (new Header('Accept-Language'))->determineLocale($request);

$this->assertEquals($result, $locale);
}

public function testReturnsFallbackLocaleIfNeeded()
/** @test **/
public function fallback_if_no_locale_found()
{
$fallback = 'de';

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('header')
->with($this->header, $this->fallback)
->andReturn($this->fallback);
->with('Accept-Language', $fallback)
->andReturn($fallback);

$result = $this->determiner->determineLocale($request);
$result = (new Header('Accept-Language'))->setFallback($fallback)->determineLocale($request);

$this->assertEquals($result, $this->fallback);
$this->assertEquals($result, $fallback);
}
}
60 changes: 15 additions & 45 deletions tests/Localize/Determiners/HostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,38 @@

class HostTest extends PHPUnit_Framework_TestCase
{
private $determiner;

public function setUp()
{
$this->determiner = (new Host(new Collection([
'en' => 'en.example.host',
'fr' => 'france.example.host'
])))->setFallback('de');
}

public function tearDown()
{
Mockery::close();
}

public function testDeterminesLocaleFromHost()
/** @test **/
public function determine_locale()
{
// One
$determiner = new Host(new Collection([
'en' => 'en.example.host',
'fr' => 'france.example.host'
]));

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('getHost')
->andReturn('france.example.host');

$locale = $this->determiner->determineLocale($request);

$request->shouldReceive('getHost')->andReturn('france.example.host');
$locale = $determiner->determineLocale($request);
$this->assertEquals('fr', $locale);

// Two

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('getHost')
->andReturn('en.example.host');

$locale = $this->determiner->determineLocale($request);

$request->shouldReceive('getHost')->andReturn('en.example.host');
$locale = $determiner->determineLocale($request);
$this->assertEquals('en', $locale);
}

public function testReturnsFallbackLocaleIfNeeded()
/** @test **/
public function fallback_if_no_locale_found()
{
// One
$determiner = (new Host(new Collection()))->setFallback('de');

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('getHost')
->andReturn('other.example.host');

$locale = $this->determiner->determineLocale($request);

$this->assertEquals('de', $locale);

// Two

$determiner = (new Host(new Collection([])))->setFallback('es');

$request->shouldReceive('getHost')->andReturn('other.example.host');
$locale = $determiner->determineLocale($request);

$this->assertEquals('es', $locale);
$this->assertEquals('de', $locale);
}
}
31 changes: 12 additions & 19 deletions tests/Localize/Determiners/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,42 @@

class ParameterTest extends PHPUnit_Framework_TestCase
{
private $requestParameter;
private $fallback;
private $determiner;

public function setUp()
{
$this->requestParameter = 'locale';
$this->fallback = 'de';
$this->determiner = (new Parameter($this->requestParameter))->setFallback($this->fallback);
}

public function tearDown()
{
Mockery::close();
}

public function testDeterminesLocaleFromRequestParameter()
/** @test **/
public function determine_locale()
{
$locale = 'en';

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('input')
->with($this->requestParameter, $this->fallback)
->with('locale', null)
->andReturn($locale);

$result = $this->determiner->determineLocale($request);
$result = (new Parameter('locale'))->determineLocale($request);

$this->assertEquals($result, $locale);
}

public function testReturnsFallbackLocaleIfNeeded()
/** @test **/
public function fallback_if_no_locale_found()
{
$fallback = 'de';

$request = Mockery::mock('Illuminate\Http\Request');

$request
->shouldReceive('input')
->with($this->requestParameter, $this->fallback)
->andReturn($this->fallback);
->with('locale', $fallback)
->andReturn($fallback);

$result = $this->determiner->determineLocale($request);
$result = (new Parameter('locale'))->setFallback($fallback)->determineLocale($request);

$this->assertEquals($result, $this->fallback);
$this->assertEquals($result, $fallback);
}
}
Loading

0 comments on commit 7e156bb

Please sign in to comment.