From e8f265f494f69fc5f1e58113c3cb452f25e99769 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 18 Jul 2017 12:53:42 +0200 Subject: [PATCH 1/3] Create SimpleContainer, fixes namespaces in tests --- src/Container/Container.php | 36 +++++++++++ src/Container/ContainerException.php | 12 ++++ src/Container/NotFoundException.php | 10 +++ src/Container/SimpleContainer.php | 67 ++++++++++++++++++++ tests/Unit/Container/SimpleContainerTest.php | 63 ++++++++++++++++++ tests/Unit/Template/EngineTest.php | 2 +- tests/Unit/Template/TemplateTest.php | 2 +- 7 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 src/Container/Container.php create mode 100644 src/Container/ContainerException.php create mode 100644 src/Container/NotFoundException.php create mode 100644 src/Container/SimpleContainer.php create mode 100644 tests/Unit/Container/SimpleContainerTest.php diff --git a/src/Container/Container.php b/src/Container/Container.php new file mode 100644 index 00000000..c4d898cc --- /dev/null +++ b/src/Container/Container.php @@ -0,0 +1,36 @@ +has($id)) + { + return $this->data[$id]; + } + + throw new NotFoundException( + sprintf('Entry "%s" don\'t exists in the container', $id) + ); + } + + /** + * Returns true if the container can return an entry for the given identifier. + * Returns false otherwise. + * + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. + * + * @param string $id Identifier of the entry to look for. + * + * @return bool + */ + public function has($id) + { + return array_key_exists($id, $this->data); + } + + /** + * Set a entry with an identifier + * + * @param string $id Identifier of the entry to look for. + * @param mixed $value the entry + * + * @return void + */ + public function set($id, $value) + { + $id = strval($id); + + $this->data[$id] = $value; + } +} diff --git a/tests/Unit/Container/SimpleContainerTest.php b/tests/Unit/Container/SimpleContainerTest.php new file mode 100644 index 00000000..5e13e009 --- /dev/null +++ b/tests/Unit/Container/SimpleContainerTest.php @@ -0,0 +1,63 @@ +assertInstanceOf('\\YoutubeDownloader\\Container\\Container', $container); + } + + /** + * @test set(), has() and get() + * @dataProvider GetterSetterProvider + */ + public function testSetterAndGetter($id, $value) + { + $container = new SimpleContainer(); + + $container->set($id, $value); + + $this->assertTrue($container->has($id)); + $this->assertSame($value, $container->get($id)); + } + + /** + * GetterSetterProvider + */ + public function GetterSetterProvider() + { + return [ + ['null', null], + ['true', true], + ['false', false], + ['int', 123456789], + ['float', 1234.56789], + ['string', 'string'], + ['array', ['array']], + ['object', new \stdClass], + ]; + } + + /** + * @test SimpleContainer throws NotFoundException + */ + public function getThrowsNotFoundException() + { + $container = new SimpleContainer(); + + $this->expectException('\\YoutubeDownloader\\Container\\NotFoundException'); + $this->expectExceptionMessage('Entry "foo" don\'t exists in the container'); + + $container->get('foo'); + } +} diff --git a/tests/Unit/Template/EngineTest.php b/tests/Unit/Template/EngineTest.php index ba4d9c41..f97cc1e6 100644 --- a/tests/Unit/Template/EngineTest.php +++ b/tests/Unit/Template/EngineTest.php @@ -1,6 +1,6 @@ Date: Tue, 18 Jul 2017 15:46:07 +0200 Subject: [PATCH 2/3] Test that SimpleContainer is compatible with PSR-11 --- composer.json | 3 +- .../Container/Psr11ContainerAdapter.php | 74 +++++++++++++++++++ .../Container/Psr11ContainerException.php | 13 ++++ .../Container/Psr11NotFoundException.php | 12 +++ tests/Unit/Container/SimpleContainerTest.php | 14 ++++ 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 tests/Fixture/Container/Psr11ContainerAdapter.php create mode 100644 tests/Fixture/Container/Psr11ContainerException.php create mode 100644 tests/Fixture/Container/Psr11NotFoundException.php diff --git a/composer.json b/composer.json index d389711f..bfe62a0e 100755 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ }, "require-dev": { "mikey179/vfsStream": "^1.6", - "phpunit/phpunit": "^4.8.35 || ^6.0" + "phpunit/phpunit": "^4.8.35 || ^6.0", + "psr/container": "^1.0" }, "autoload": { "psr-4": { diff --git a/tests/Fixture/Container/Psr11ContainerAdapter.php b/tests/Fixture/Container/Psr11ContainerAdapter.php new file mode 100644 index 00000000..937cb413 --- /dev/null +++ b/tests/Fixture/Container/Psr11ContainerAdapter.php @@ -0,0 +1,74 @@ +container = $container; + } + + /** + * Finds an entry of the container by its identifier and returns it. + * + * @param string $id Identifier of the entry to look for. + * + * @throws Psr\Container\NotFoundExceptionInterface No entry was found for **this** identifier. + * @throws Psr\Container\ContainerExceptionInterface Error while retrieving the entry. + * + * @return mixed Entry. + */ + public function get($id) + { + try + { + return $this->container->get($id); + } + catch (NotFoundException $e) + { + throw new Psr11NotFoundException($e->getMessage()); + } + catch (ContainerException $e) + { + throw new Psr11ContainerException($e->getMessage()); + } + } + + /** + * Returns true if the container can return an entry for the given identifier. + * Returns false otherwise. + * + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. + * + * @param string $id Identifier of the entry to look for. + * + * @return bool + */ + public function has($id) + { + return $this->container->has($id); + } +} diff --git a/tests/Fixture/Container/Psr11ContainerException.php b/tests/Fixture/Container/Psr11ContainerException.php new file mode 100644 index 00000000..35e462e1 --- /dev/null +++ b/tests/Fixture/Container/Psr11ContainerException.php @@ -0,0 +1,13 @@ +assertInstanceOf('\\YoutubeDownloader\\Container\\Container', $container); } + /** + * @test SimpleContainer is campatible with Psr\Container\ContainerInterface + */ + public function isPsr11Compatible() + { + $container = new SimpleContainer(); + + $adapter = new Psr11ContainerAdapter($container); + + $this->assertInstanceOf('\\Psr\\Container\\ContainerInterface', $adapter); + $this->assertInstanceOf('\\YoutubeDownloader\\Container\\Container', $adapter); + } + /** * @test set(), has() and get() * @dataProvider GetterSetterProvider From c6bd837c4fd25482c5cdeac894576fa3c61956ab Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 18 Jul 2017 16:08:36 +0200 Subject: [PATCH 3/3] Add Container to all files --- common.php | 29 +++++++++++++++++++++-------- download.php | 5 ++++- getimage.php | 6 ++++-- getvideo.php | 5 ++++- index.php | 4 +++- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/common.php b/common.php index 247bccf0..0fde48cd 100644 --- a/common.php +++ b/common.php @@ -39,32 +39,45 @@ }); /** - * Closure to create a config class + * Closure to create a container class */ -$config = call_user_func_array( +$container = call_user_func_array( function($custom = 'custom') { + // Create Container + $container = new \YoutubeDownloader\Container\SimpleContainer; + + // Create Config $ds = DIRECTORY_SEPARATOR; $config_dir = realpath(__DIR__) . $ds . 'config' . $ds; - return \YoutubeDownloader\Config::createFromFiles( + $config = \YoutubeDownloader\Config::createFromFiles( $config_dir . 'default.php', $config_dir . $custom . '.php' ); + + $container->set('config', $config); + + // Create Template\Engine + $template = \YoutubeDownloader\Template\Engine::createFromDirectory( + __DIR__ . DIRECTORY_SEPARATOR . 'templates' + ); + + $container->set('template', $template); + + return $container; }, [getenv('CONFIG_ENV') ?: 'custom'] ); // Show all errors on debug -if ( $config->get('debug') === true ) +if ( $container->get('config')->get('debug') === true ) { error_reporting(E_ALL); ini_set('display_errors', 1); } -$template = \YoutubeDownloader\Template\Engine::createFromDirectory( - __DIR__ . DIRECTORY_SEPARATOR . 'templates' -); +date_default_timezone_set($container->get('config')->get('default_timezone')); -date_default_timezone_set($config->get('default_timezone')); +return $container; diff --git a/download.php b/download.php index f4dba48e..082cc977 100644 --- a/download.php +++ b/download.php @@ -1,5 +1,8 @@ get('config'); +$template = $container->get('template'); // Check download token if (empty($_GET['mime']) OR empty($_GET['token'])) diff --git a/getimage.php b/getimage.php index f847624b..2aa341c8 100644 --- a/getimage.php +++ b/getimage.php @@ -1,5 +1,8 @@ get('config'); +$template = $container->get('template'); if ( ! isset($_GET['videoid']) ) { @@ -11,7 +14,6 @@ $my_id = \YoutubeDownloader\YoutubeDownloader::validateVideoId($_GET['videoid']); - if ( $my_id === null ) { echo $template->render('error.php', [ diff --git a/getvideo.php b/getvideo.php index 0e4b777e..6b4cf52f 100644 --- a/getvideo.php +++ b/getvideo.php @@ -6,7 +6,10 @@ // Takes a VideoID and outputs a list of formats in which the video can be // downloaded -include_once('common.php'); +$container = include_once('common.php'); + +$config = $container->get('config'); +$template = $container->get('template'); if( ! isset($_GET['videoid']) ) { diff --git a/index.php b/index.php index d299a5a2..a08fbc49 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,8 @@ get('config'); +$template = $container->get('template'); echo $template->render('index.php', [ 'is_chrome' => \YoutubeDownloader\YoutubeDownloader::is_chrome(),