diff --git a/tests/Localize/DeterminerManagerTest.php b/tests/Localize/DeterminerManagerTest.php index 873fefb..7272282 100644 --- a/tests/Localize/DeterminerManagerTest.php +++ b/tests/Localize/DeterminerManagerTest.php @@ -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 { @@ -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); @@ -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); diff --git a/tests/Localize/Determiners/CookieTest.php b/tests/Localize/Determiners/CookieTest.php index 4d0e83d..88be2b5 100644 --- a/tests/Localize/Determiners/CookieTest.php +++ b/tests/Localize/Determiners/CookieTest.php @@ -8,23 +8,13 @@ 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'; @@ -32,25 +22,28 @@ public function testDeterminesLocaleFromCookie() $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); } } diff --git a/tests/Localize/Determiners/HeaderTest.php b/tests/Localize/Determiners/HeaderTest.php index d0aa2b3..d8c7052 100644 --- a/tests/Localize/Determiners/HeaderTest.php +++ b/tests/Localize/Determiners/HeaderTest.php @@ -8,23 +8,13 @@ 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'; @@ -32,25 +22,28 @@ public function testDeterminesLocaleFromHeader() $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); } } diff --git a/tests/Localize/Determiners/HostTest.php b/tests/Localize/Determiners/HostTest.php index 988e7eb..25c7b59 100644 --- a/tests/Localize/Determiners/HostTest.php +++ b/tests/Localize/Determiners/HostTest.php @@ -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); } } diff --git a/tests/Localize/Determiners/ParameterTest.php b/tests/Localize/Determiners/ParameterTest.php index b28e225..17ac639 100644 --- a/tests/Localize/Determiners/ParameterTest.php +++ b/tests/Localize/Determiners/ParameterTest.php @@ -8,23 +8,13 @@ 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'; @@ -32,25 +22,28 @@ public function testDeterminesLocaleFromRequestParameter() $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); } } diff --git a/tests/Localize/Determiners/SessionTest.php b/tests/Localize/Determiners/SessionTest.php index bff38a2..f1e9685 100644 --- a/tests/Localize/Determiners/SessionTest.php +++ b/tests/Localize/Determiners/SessionTest.php @@ -8,28 +8,17 @@ class SessionTest extends PHPUnit_Framework_TestCase { - private $sessionKey; - private $fallback; - private $determiner; - - public function setUp() - { - $this->sessionKey = 'locale'; - $this->fallback = 'de'; - $this->determiner = (new Session($this->sessionKey))->setFallback($this->fallback); - } - public function tearDown() { Mockery::close(); } - public function testDeterminesLocaleFromSession() + /** @test **/ + public function determine_locale() { $locale = 'en'; $request = Mockery::mock('Illuminate\Http\Request'); - $session = Mockery::mock('Illuminate\Session\Store'); $request @@ -38,18 +27,20 @@ public function testDeterminesLocaleFromSession() $session ->shouldReceive('get') - ->with($this->sessionKey, $this->fallback) + ->with('locale', null) ->andReturn($locale); - $result = $this->determiner->determineLocale($request); + $result = (new Session('locale'))->determineLocale($request); $this->assertEquals($result, $locale); } - public function testReturnsFallbackLocaleIfNeeded() + /** @test **/ + public function fallback_if_no_locale_found() { - $request = Mockery::mock('Illuminate\Http\Request'); + $fallback = 'de'; + $request = Mockery::mock('Illuminate\Http\Request'); $session = Mockery::mock('Illuminate\Session\Store'); $request @@ -58,11 +49,11 @@ public function testReturnsFallbackLocaleIfNeeded() $session ->shouldReceive('get') - ->with($this->sessionKey, $this->fallback) - ->andReturn($this->fallback); + ->with('locale', $fallback) + ->andReturn($fallback); - $result = $this->determiner->determineLocale($request); + $result = (new Session('locale'))->setFallback($fallback)->determineLocale($request); - $this->assertEquals($result, $this->fallback); + $this->assertEquals($result, $fallback); } } diff --git a/tests/Localize/Determiners/StackTest.php b/tests/Localize/Determiners/StackTest.php index e5a1b03..2e2bdad 100644 --- a/tests/Localize/Determiners/StackTest.php +++ b/tests/Localize/Determiners/StackTest.php @@ -12,36 +12,26 @@ class StackTest extends PHPUnit_Framework_TestCase { - private $fallback; - private $stack; - private $determiner; - - public function setUp() - { - $this->fallback = 'de'; - - $this->stack = new Collection([ - new Parameter('locale'), - new Cookie('locale'), - new Session('locale') - ]); - - $this->determiner = (new Stack($this->stack))->setFallback($this->fallback); - } - public function tearDown() { Mockery::close(); } - public function testDeterminesLocaleFromStack() + /** @test **/ + public function determine_locale() { + $determiner = new Stack(new Collection([ + new Parameter('locale'), + new Cookie('locale'), + new Session('locale') + ])); + $request = $this->mockRequest([ 'parameter' => 'en', 'cookie' => null, 'session' => null ]); - $result = $this->determiner->determineLocale($request); + $result = $determiner->determineLocale($request); $this->assertEquals($result, 'en'); $request = $this->mockRequest([ @@ -49,7 +39,7 @@ public function testDeterminesLocaleFromStack() 'cookie' => 'fr', 'session' => null ]); - $result = $this->determiner->determineLocale($request); + $result = $determiner->determineLocale($request); $this->assertEquals($result, 'fr'); $request = $this->mockRequest([ @@ -57,7 +47,7 @@ public function testDeterminesLocaleFromStack() 'cookie' => null, 'session' => 'es' ]); - $result = $this->determiner->determineLocale($request); + $result = $determiner->determineLocale($request); $this->assertEquals($result, 'es'); $request = $this->mockRequest([ @@ -65,7 +55,7 @@ public function testDeterminesLocaleFromStack() 'cookie' => 'fr', 'session' => null ]); - $result = $this->determiner->determineLocale($request); + $result = $determiner->determineLocale($request); $this->assertEquals($result, 'en'); $request = $this->mockRequest([ @@ -73,7 +63,7 @@ public function testDeterminesLocaleFromStack() 'cookie' => null, 'session' => 'es' ]); - $result = $this->determiner->determineLocale($request); + $result = $determiner->determineLocale($request); $this->assertEquals($result, 'en'); $request = $this->mockRequest([ @@ -81,19 +71,30 @@ public function testDeterminesLocaleFromStack() 'cookie' => 'fr', 'session' => 'es' ]); - $result = $this->determiner->determineLocale($request); + $result = $determiner->determineLocale($request); $this->assertEquals($result, 'fr'); } - public function testUsesFallbackIfNothingFoundInStack() + /** @test **/ + public function fallback_if_no_locale_found() { + $fallback = 'de'; + + $determiner = (new Stack(new Collection([ + new Parameter('locale'), + new Cookie('locale'), + new Session('locale') + ])))->setFallback($fallback); + $request = $this->mockRequest([ 'parameter' => null, 'cookie' => null, 'session' => null ]); - $result = $this->determiner->determineLocale($request); - $this->assertEquals($result, 'de'); + + $result = $determiner->determineLocale($request); + + $this->assertEquals($result, $fallback); } private function mockRequest($results)