Skip to content

Commit

Permalink
Change internal methods visibility to protected
Browse files Browse the repository at this point in the history
  • Loading branch information
Omranic committed Feb 4, 2017
1 parent fc51e9b commit 788570c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
12 changes: 6 additions & 6 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function where($key, $operator, $value = null)
*
* @return \Closure
*/
public static function operatorForWhere($key, $operator, $value)
protected static function operatorForWhere($key, $operator, $value)
{
return function ($item) use ($key, $operator, $value) {
$retrieved = static::get($item, $key);
Expand Down Expand Up @@ -128,7 +128,7 @@ public static function operatorForWhere($key, $operator, $value)
*
* @return array
*/
public static function filter($items, callable $callback = null)
protected static function filter($items, callable $callback = null)
{
if ($callback) {
return array_filter($items, $callback, ARRAY_FILTER_USE_BOTH);
Expand All @@ -146,7 +146,7 @@ public static function filter($items, callable $callback = null)
*
* @return mixed
*/
public static function get($target, $key, $default = null)
protected static function get($target, $key, $default = null)
{
if (is_null($key)) {
return $target;
Expand Down Expand Up @@ -186,7 +186,7 @@ public static function get($target, $key, $default = null)
*
* @return array
*/
public static function pluck($array, $value, $key = null)
protected static function pluck($array, $value, $key = null)
{
$results = [];

Expand Down Expand Up @@ -219,7 +219,7 @@ public static function pluck($array, $value, $key = null)
*
* @return array
*/
public static function collapse($array)
protected static function collapse($array)
{
$results = [];

Expand All @@ -243,7 +243,7 @@ public static function collapse($array)
*
* @return string
*/
public static function getFile($filePath)
protected static function getFile($filePath)
{
if (! file_exists($filePath)) {
throw CountryLoaderException::invalidCountry();
Expand Down
94 changes: 56 additions & 38 deletions tests/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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';
}));
}
Expand All @@ -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 */
Expand All @@ -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]'],
Expand All @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand All @@ -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');
}
}

0 comments on commit 788570c

Please sign in to comment.