From 7b82e48cddee1cea29a08d8fba1f8e6cf7bcd976 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Fri, 29 Apr 2016 01:53:51 +0300 Subject: [PATCH] Static Loader class was refactored into AntiTempmailRepo class. Added LoaderInterface. Added fromLoader creator for Storage. --- src/Loader.php | 38 --------------- src/Loader/AntiTempmailRepo.php | 69 +++++++++++++++++++++++++++ src/LoaderInterface.php | 12 +++++ src/Storage.php | 11 +++++ tests/Loader/AntiTempmailRepoTest.php | 45 +++++++++++++++++ tests/LoaderTest.php | 47 ------------------ tests/StorageTest.php | 13 +++++ 7 files changed, 150 insertions(+), 85 deletions(-) delete mode 100644 src/Loader.php create mode 100644 src/Loader/AntiTempmailRepo.php create mode 100644 src/LoaderInterface.php create mode 100644 tests/Loader/AntiTempmailRepoTest.php delete mode 100644 tests/LoaderTest.php diff --git a/src/Loader.php b/src/Loader.php deleted file mode 100644 index 1026c59..0000000 --- a/src/Loader.php +++ /dev/null @@ -1,38 +0,0 @@ -customPaths = $customPaths; + } + + /** + * @inheritdoc + */ + public function load() + { + $paths = $this->getPaths(); + $dataPath = null; + foreach($paths as $path) { + if (file_exists($path)) { + $dataPath = $path; + break; + } + } + + if ($dataPath === null) { + throw new Exception('Cannot find data file. Please check getPaths() implementation.'); + } + + $data = json_decode(file_get_contents($dataPath), true); + if (!is_array($data)) { + throw new Exception('Cannot decode json from data file.'); + } + + return $data; + } + + /** + * Generates file path array to data repository. + * + * Default paths are used in following cases: + * 1. if this library and data repository are included in project as composer dependencies; + * 2. if this library is deployed for development (data repository included as composer dependency to this library). + * + * @see Loader::customPath for custom data repository implementation + * + * @return array + */ + protected function getPaths() + { + return array_merge($this->customPaths, [ + __DIR__ . '/../../../anti-tempmail-repo/data.json', + __DIR__ . '/../../vendor/ely/anti-tempmail-repo/data.json', + ]); + } +} diff --git a/src/LoaderInterface.php b/src/LoaderInterface.php new file mode 100644 index 0000000..0603276 --- /dev/null +++ b/src/LoaderInterface.php @@ -0,0 +1,12 @@ +load()); + } } diff --git a/tests/Loader/AntiTempmailRepoTest.php b/tests/Loader/AntiTempmailRepoTest.php new file mode 100644 index 0000000..0818ad7 --- /dev/null +++ b/tests/Loader/AntiTempmailRepoTest.php @@ -0,0 +1,45 @@ +assertTrue(is_array($loader->load())); + } + + public function testLoadExceptionWrongPaths() + { + $this->expectException('Exception'); + $loader = new AntiTempmailRepoWithWrongPaths(); + $loader->load(); + } + + public function testLoadExceptionInvalidJson() + { + $this->expectException('Exception'); + $loader = new AntiTempmailRepoWithInvalidJson(); + $loader->load(); + } +} + +class AntiTempmailRepoWithWrongPaths extends AntiTempmailRepo +{ + protected function getPaths() + { + return [ + __DIR__ . '/virtual_reality.json', + ]; + } +} + +class AntiTempmailRepoWithInvalidJson extends AntiTempmailRepo +{ + protected function getPaths() + { + return [ + __DIR__ . '/AntiTempmailRepoTest.php', + ]; + } +} diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php deleted file mode 100644 index aa4e67e..0000000 --- a/tests/LoaderTest.php +++ /dev/null @@ -1,47 +0,0 @@ -assertTrue(is_array(Loader::getPaths())); - } - - public function testLoad() - { - $this->assertTrue(is_array(Loader::load())); - } - - public function testLoadExceptionWrongPaths() - { - $this->expectException('Exception'); - LoaderWithWrongPaths::load(); - } - - public function testLoadExceptionInvalidJson() - { - $this->expectException('Exception'); - LoaderWithInvalidJson::load(); - } -} - -class LoaderWithWrongPaths extends Loader -{ - public static function getPaths() - { - return [ - __DIR__ . '/virtual_reality.json', - ]; - } -} - -class LoaderWithInvalidJson extends Loader -{ - public static function getPaths() - { - return [ - __DIR__ . '/LoaderTest.php', - ]; - } -} diff --git a/tests/StorageTest.php b/tests/StorageTest.php index 9f7bf12..825c90a 100644 --- a/tests/StorageTest.php +++ b/tests/StorageTest.php @@ -26,4 +26,17 @@ class StorageTest extends \PHPUnit_Framework_TestCase $this->assertEquals($storage, $storage->appendItems('item2')); $this->assertEquals(['item1', 'item2'], $storage->getItems()); } + + public function testFromLoader() + { + $this->assertInstanceOf('Ely\TempmailBuster\Storage', Storage::fromLoader(new SimpleLoader())); + } +} + +class SimpleLoader implements LoaderInterface +{ + public function load() + { + return ['foo', 'bar']; + } }