From 788570c3258b167e4b35b8263d3b9590acf4bfac Mon Sep 17 00:00:00 2001 From: Abdelrahman Omran Date: Sun, 5 Feb 2017 00:37:00 +0200 Subject: [PATCH] Change internal methods visibility to protected --- src/Loader.php | 12 +++--- tests/LoaderTest.php | 94 ++++++++++++++++++++++++++------------------ 2 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/Loader.php b/src/Loader.php index a284d86..afe6eb9 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -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); @@ -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); @@ -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; @@ -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 = []; @@ -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 = []; @@ -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(); diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php index db8763d..6a6df5d 100644 --- a/tests/LoaderTest.php +++ b/tests/LoaderTest.php @@ -15,6 +15,7 @@ namespace Rinvex\Country\Test; +use ReflectionClass; use Rinvex\Country\Loader; use Rinvex\Country\Country; use PHPUnit_Framework_TestCase; @@ -22,6 +23,23 @@ 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(['taylorotwell@gmail.com', null, null], Loader::get($array, '*.email', 'irrelevant')); + $this->assertEquals(['taylor', 'abigail', 'dayle'], $this->methods['get']->invoke(null, $array, '*.name')); + $this->assertEquals(['taylorotwell@gmail.com', null, null], $this->methods['get']->invoke(null, $array, '*.email', 'irrelevant')); $array = [ 'users' => [ ['first' => 'taylor', 'last' => 'otwell', 'email' => 'taylorotwell@gmail.com'], @@ -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(['taylorotwell@gmail.com', 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(['taylorotwell@gmail.com', 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,22 +376,22 @@ 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 */ @@ -381,6 +399,6 @@ 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'); } }