forked from rinvex/countries
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change internal methods visibility to protected
- Loading branch information
Showing
2 changed files
with
62 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,13 +15,31 @@ | |
|
||
namespace Rinvex\Country\Test; | ||
|
||
use ReflectionClass; | ||
use Rinvex\Country\Loader; | ||
use Rinvex\Country\Country; | ||
use PHPUnit_Framework_TestCase; | ||
use Rinvex\Country\Exceptions\CountryLoaderException; | ||
|
||
class LoaderTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** @var array */ | ||
protected $methods; | ||
|
||
public function setUp() | ||
{ | ||
$reflection = new ReflectionClass(Loader::class); | ||
$this->methods['get'] = $reflection->getMethod('get'); | ||
$this->methods['filter'] = $reflection->getMethod('filter'); | ||
$this->methods['pluck'] = $reflection->getMethod('pluck'); | ||
$this->methods['collapse'] = $reflection->getMethod('collapse'); | ||
$this->methods['getFile'] = $reflection->getMethod('getFile'); | ||
|
||
foreach ($this->methods as $method) { | ||
$method->setAccessible(true); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_country_data() | ||
{ | ||
|
@@ -194,15 +212,15 @@ public function it_throws_an_exception_when_invalid_country() | |
public function it_filters_data() | ||
{ | ||
$array1 = [['id' => 1, 'name' => 'Hello'], ['id' => 2, 'name' => 'World']]; | ||
$this->assertEquals([1 => ['id' => 2, 'name' => 'World']], Loader::filter($array1, function ($item) { | ||
$this->assertEquals([1 => ['id' => 2, 'name' => 'World']], $this->methods['filter']->invoke(null, $array1, function ($item) { | ||
return $item['id'] == 2; | ||
})); | ||
|
||
$array2 = ['', 'Hello', '', 'World']; | ||
$this->assertEquals(['Hello', 'World'], array_values(Loader::filter($array2))); | ||
$this->assertEquals(['Hello', 'World'], array_values($this->methods['filter']->invoke(null, $array2))); | ||
|
||
$array3 = ['id' => 1, 'first' => 'Hello', 'second' => 'World']; | ||
$this->assertEquals(['first' => 'Hello', 'second' => 'World'], Loader::filter($array3, function ($item, $key) { | ||
$this->assertEquals(['first' => 'Hello', 'second' => 'World'], $this->methods['filter']->invoke(null, $array3, function ($item, $key) { | ||
return $key != 'id'; | ||
})); | ||
} | ||
|
@@ -213,22 +231,22 @@ public function it_gets_data() | |
$object = (object) ['users' => ['name' => ['Taylor', 'Otwell']]]; | ||
$array = [(object) ['users' => [(object) ['name' => 'Taylor']]]]; | ||
$dottedArray = ['users' => ['first.name' => 'Taylor', 'middle.name' => null]]; | ||
$this->assertEquals('Taylor', Loader::get($object, 'users.name.0')); | ||
$this->assertEquals('Taylor', Loader::get($array, '0.users.0.name')); | ||
$this->assertNull(Loader::get($array, '0.users.3')); | ||
$this->assertEquals('Not found', Loader::get($array, '0.users.3', 'Not found')); | ||
$this->assertEquals('Not found', Loader::get($array, '0.users.3', function () { | ||
$this->assertEquals('Taylor', $this->methods['get']->invoke(null, $object, 'users.name.0')); | ||
$this->assertEquals('Taylor', $this->methods['get']->invoke(null, $array, '0.users.0.name')); | ||
$this->assertNull($this->methods['get']->invoke(null, $array, '0.users.3')); | ||
$this->assertEquals('Not found', $this->methods['get']->invoke(null, $array, '0.users.3', 'Not found')); | ||
$this->assertEquals('Not found', $this->methods['get']->invoke(null, $array, '0.users.3', function () { | ||
return 'Not found'; | ||
})); | ||
$this->assertEquals('Taylor', Loader::get($dottedArray, ['users', 'first.name'])); | ||
$this->assertNull(Loader::get($dottedArray, ['users', 'middle.name'])); | ||
$this->assertEquals('Not found', Loader::get($dottedArray, ['users', 'last.name'], 'Not found')); | ||
$this->assertEquals('Taylor', $this->methods['get']->invoke(null, $dottedArray, ['users', 'first.name'])); | ||
$this->assertNull($this->methods['get']->invoke(null, $dottedArray, ['users', 'middle.name'])); | ||
$this->assertEquals('Not found', $this->methods['get']->invoke(null, $dottedArray, ['users', 'last.name'], 'Not found')); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_target_when_missing_key() | ||
{ | ||
$this->assertEquals(['test'], Loader::get(['test'], null)); | ||
$this->assertEquals(['test'], $this->methods['get']->invoke(null, ['test'], null)); | ||
} | ||
|
||
/** @test */ | ||
|
@@ -239,8 +257,8 @@ public function it_gets_data_with_nested_arrays() | |
['name' => 'abigail'], | ||
['name' => 'dayle'], | ||
]; | ||
$this->assertEquals(['taylor', 'abigail', 'dayle'], Loader::get($array, '*.name')); | ||
$this->assertEquals(['[email protected]', null, null], Loader::get($array, '*.email', 'irrelevant')); | ||
$this->assertEquals(['taylor', 'abigail', 'dayle'], $this->methods['get']->invoke(null, $array, '*.name')); | ||
$this->assertEquals(['[email protected]', null, null], $this->methods['get']->invoke(null, $array, '*.email', 'irrelevant')); | ||
$array = [ | ||
'users' => [ | ||
['first' => 'taylor', 'last' => 'otwell', 'email' => '[email protected]'], | ||
|
@@ -249,10 +267,10 @@ public function it_gets_data_with_nested_arrays() | |
], | ||
'posts' => null, | ||
]; | ||
$this->assertEquals(['taylor', 'abigail', 'dayle'], Loader::get($array, 'users.*.first')); | ||
$this->assertEquals(['[email protected]', null, null], Loader::get($array, 'users.*.email', 'irrelevant')); | ||
$this->assertEquals('not found', Loader::get($array, 'posts.*.date', 'not found')); | ||
$this->assertNull(Loader::get($array, 'posts.*.date')); | ||
$this->assertEquals(['taylor', 'abigail', 'dayle'], $this->methods['get']->invoke(null, $array, 'users.*.first')); | ||
$this->assertEquals(['[email protected]', null, null], $this->methods['get']->invoke(null, $array, 'users.*.email', 'irrelevant')); | ||
$this->assertEquals('not found', $this->methods['get']->invoke(null, $array, 'posts.*.date', 'not found')); | ||
$this->assertNull($this->methods['get']->invoke(null, $array, 'posts.*.date')); | ||
} | ||
|
||
/** @test */ | ||
|
@@ -280,10 +298,10 @@ public function it_gets_data_with_nested_double_nested_arrays_and_collapses_resu | |
], | ||
], | ||
]; | ||
$this->assertEquals(['taylor', 'abigail', 'abigail', 'dayle', 'dayle', 'taylor'], Loader::get($array, 'posts.*.comments.*.author')); | ||
$this->assertEquals([4, 3, 2, null, null, 1], Loader::get($array, 'posts.*.comments.*.likes')); | ||
$this->assertEquals([], Loader::get($array, 'posts.*.users.*.name', 'irrelevant')); | ||
$this->assertEquals([], Loader::get($array, 'posts.*.users.*.name')); | ||
$this->assertEquals(['taylor', 'abigail', 'abigail', 'dayle', 'dayle', 'taylor'], $this->methods['get']->invoke(null, $array, 'posts.*.comments.*.author')); | ||
$this->assertEquals([4, 3, 2, null, null, 1], $this->methods['get']->invoke(null, $array, 'posts.*.comments.*.likes')); | ||
$this->assertEquals([], $this->methods['get']->invoke(null, $array, 'posts.*.users.*.name', 'irrelevant')); | ||
$this->assertEquals([], $this->methods['get']->invoke(null, $array, 'posts.*.users.*.name')); | ||
} | ||
|
||
/** @test */ | ||
|
@@ -316,28 +334,28 @@ public function it_plucks_array() | |
'#baz', | ||
], | ||
], | ||
], Loader::pluck($data, 'comments')); | ||
$this->assertEquals([['#foo', '#bar'], ['#baz']], Loader::pluck($data, 'comments.tags')); | ||
$this->assertEquals([null, null], Loader::pluck($data, 'foo')); | ||
$this->assertEquals([null, null], Loader::pluck($data, 'foo.bar')); | ||
], $this->methods['pluck']->invoke(null, $data, 'comments')); | ||
$this->assertEquals([['#foo', '#bar'], ['#baz']], $this->methods['pluck']->invoke(null, $data, 'comments.tags')); | ||
$this->assertEquals([null, null], $this->methods['pluck']->invoke(null, $data, 'foo')); | ||
$this->assertEquals([null, null], $this->methods['pluck']->invoke(null, $data, 'foo.bar')); | ||
} | ||
|
||
/** @test */ | ||
public function it_plucks_array_with_array_and_object_values() | ||
{ | ||
$array = [(object) ['name' => 'taylor', 'email' => 'foo'], ['name' => 'dayle', 'email' => 'bar']]; | ||
$this->assertEquals(['taylor', 'dayle'], Loader::pluck($array, 'name')); | ||
$this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], Loader::pluck($array, 'email', 'name')); | ||
$this->assertEquals(['taylor', 'dayle'], $this->methods['pluck']->invoke(null, $array, 'name')); | ||
$this->assertEquals(['taylor' => 'foo', 'dayle' => 'bar'], $this->methods['pluck']->invoke(null, $array, 'email', 'name')); | ||
} | ||
|
||
/** @test */ | ||
public function it_plucks_array_with_nested_keys() | ||
{ | ||
$array = [['user' => ['taylor', 'otwell']], ['user' => ['dayle', 'rees']]]; | ||
$this->assertEquals(['taylor', 'dayle'], Loader::pluck($array, 'user.0')); | ||
$this->assertEquals(['taylor', 'dayle'], Loader::pluck($array, ['user', 0])); | ||
$this->assertEquals(['taylor' => 'otwell', 'dayle' => 'rees'], Loader::pluck($array, 'user.1', 'user.0')); | ||
$this->assertEquals(['taylor' => 'otwell', 'dayle' => 'rees'], Loader::pluck($array, ['user', 1], ['user', 0])); | ||
$this->assertEquals(['taylor', 'dayle'], $this->methods['pluck']->invoke(null, $array, 'user.0')); | ||
$this->assertEquals(['taylor', 'dayle'], $this->methods['pluck']->invoke(null, $array, ['user', 0])); | ||
$this->assertEquals(['taylor' => 'otwell', 'dayle' => 'rees'], $this->methods['pluck']->invoke(null, $array, 'user.1', 'user.0')); | ||
$this->assertEquals(['taylor' => 'otwell', 'dayle' => 'rees'], $this->methods['pluck']->invoke(null, $array, ['user', 1], ['user', 0])); | ||
} | ||
|
||
/** @test */ | ||
|
@@ -358,29 +376,29 @@ public function it_plucks_array_with_nested_arrays() | |
], | ||
], | ||
]; | ||
$this->assertEquals([['taylor'], ['abigail', 'dayle']], Loader::pluck($array, 'users.*.first')); | ||
$this->assertEquals(['a' => ['taylor'], 'b' => ['abigail', 'dayle']], Loader::pluck($array, 'users.*.first', 'account')); | ||
$this->assertEquals([['foo'], [null, null]], Loader::pluck($array, 'users.*.email')); | ||
$this->assertEquals([['taylor'], ['abigail', 'dayle']], $this->methods['pluck']->invoke(null, $array, 'users.*.first')); | ||
$this->assertEquals(['a' => ['taylor'], 'b' => ['abigail', 'dayle']], $this->methods['pluck']->invoke(null, $array, 'users.*.first', 'account')); | ||
$this->assertEquals([['foo'], [null, null]], $this->methods['pluck']->invoke(null, $array, 'users.*.email')); | ||
} | ||
|
||
/** @test */ | ||
public function it_collapses_array() | ||
{ | ||
$array = [[1], [2], [3], ['foo', 'bar'], ['baz', 'boom']]; | ||
$this->assertEquals([1, 2, 3, 'foo', 'bar', 'baz', 'boom'], Loader::collapse($array)); | ||
$this->assertEquals([1, 2, 3, 'foo', 'bar', 'baz', 'boom'], $this->methods['collapse']->invoke(null, $array)); | ||
} | ||
|
||
/** @test */ | ||
public function it_gets_file_content() | ||
{ | ||
$this->assertStringEqualsFile(__DIR__.'/../resources/data/eg.json', Loader::getFile(__DIR__.'/../resources/data/eg.json')); | ||
$this->assertStringEqualsFile(__DIR__.'/../resources/data/eg.json', $this->methods['getFile']->invoke(null, __DIR__.'/../resources/data/eg.json')); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_an_exception_when_invalid_file() | ||
{ | ||
$this->expectException(CountryLoaderException::class); | ||
|
||
Loader::getFile(__DIR__.'/../resources/data/invalid.json'); | ||
$this->methods['getFile']->invoke(null, __DIR__.'/../resources/data/invalid.json'); | ||
} | ||
} |