From be9b79efc6527929482161676813a7b3a5dfa4dd Mon Sep 17 00:00:00 2001 From: Westin Shafer Date: Mon, 2 Apr 2018 10:05:08 -0600 Subject: [PATCH 1/3] Allow existing PDO service to be used --- src/Repository/Pdo/AbstractRepository.php | 4 +-- src/Repository/Pdo/PdoServiceFactory.php | 13 ++++++- test/Repository/Pdo/PdoServiceFactoryTest.php | 35 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Repository/Pdo/AbstractRepository.php b/src/Repository/Pdo/AbstractRepository.php index 7cce630..ebd21c8 100644 --- a/src/Repository/Pdo/AbstractRepository.php +++ b/src/Repository/Pdo/AbstractRepository.php @@ -23,9 +23,9 @@ class AbstractRepository /** * Constructor * - * @param PdoService $pdo + * @param \PDO $pdo */ - public function __construct(PdoService $pdo) + public function __construct(\PDO $pdo) { $this->pdo = $pdo; } diff --git a/src/Repository/Pdo/PdoServiceFactory.php b/src/Repository/Pdo/PdoServiceFactory.php index 4da0057..eda23cc 100644 --- a/src/Repository/Pdo/PdoServiceFactory.php +++ b/src/Repository/Pdo/PdoServiceFactory.php @@ -15,7 +15,7 @@ class PdoServiceFactory { - public function __invoke(ContainerInterface $container) : PdoService + public function __invoke(ContainerInterface $container) : \PDO { $config = $container->has('config') ? $container->get('config') : []; $config = $config['authentication']['pdo'] ?? null; @@ -24,6 +24,17 @@ public function __invoke(ContainerInterface $container) : PdoService 'The PDO configuration is missing' ); } + + if (is_string($config) && !$container->has($config)) { + throw new Exception\InvalidConfigException( + 'Invalid service for PDO' + ); + } + + if (is_string($config) && $container->has($config)) { + return $container->get($config); + } + if (! isset($config['dsn'])) { throw new Exception\InvalidConfigException( 'The DSN configuration is missing for PDO' diff --git a/test/Repository/Pdo/PdoServiceFactoryTest.php b/test/Repository/Pdo/PdoServiceFactoryTest.php index 6a37479..5e60c7c 100644 --- a/test/Repository/Pdo/PdoServiceFactoryTest.php +++ b/test/Repository/Pdo/PdoServiceFactoryTest.php @@ -73,4 +73,39 @@ public function testValidConfigurationResultsInReturnedPdoServiceInstance() $this->assertInstanceOf(PdoService::class, $pdo); } + + public function testValidServiceInConfigurationReturnsPdoService() + { + $mockPdo = $this->prophesize(\PDO::class); + + $this->container->has('config')->willReturn(true); + $this->container->get('config')->willReturn([ + 'authentication' => [ + 'pdo' => 'My\Pdo\Service', + ], + ]); + + $this->container->has('My\Pdo\Service')->willReturn(true); + $this->container->get('My\Pdo\Service')->willReturn($mockPdo->reveal()); + + $pdo = ($this->factory)($this->container->reveal()); + + $this->assertInstanceOf(\PDO::class, $pdo); + } + + public function testRaisesExceptionIfPdoServiceIsInvalid() + { + $this->container->has('config')->willReturn(true); + $this->container->get('config')->willReturn([ + 'authentication' => [ + 'pdo' => 'My\Invalid\Service', + ], + ]); + + $this->container->has('My\Invalid\Service')->willReturn(false); + + $this->expectException(Exception\InvalidConfigException::class); + + ($this->factory)($this->container->reveal()); + } } From 2fb725df124d8c8ed35cd814c804a7bfef0ec681 Mon Sep 17 00:00:00 2001 From: Westin Shafer Date: Mon, 2 Apr 2018 10:18:09 -0600 Subject: [PATCH 2/3] sytle fix --- src/Repository/Pdo/PdoServiceFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Repository/Pdo/PdoServiceFactory.php b/src/Repository/Pdo/PdoServiceFactory.php index eda23cc..25e19dc 100644 --- a/src/Repository/Pdo/PdoServiceFactory.php +++ b/src/Repository/Pdo/PdoServiceFactory.php @@ -25,7 +25,7 @@ public function __invoke(ContainerInterface $container) : \PDO ); } - if (is_string($config) && !$container->has($config)) { + if (is_string($config) && ! $container->has($config)) { throw new Exception\InvalidConfigException( 'Invalid service for PDO' ); From 1215cd6cc690b94a4889998cd07d8f27b52daa97 Mon Sep 17 00:00:00 2001 From: Westin Shafer Date: Wed, 30 May 2018 13:09:52 -0600 Subject: [PATCH 3/3] Added documentaion for passing in existing pdo service --- .gitignore | 1 + docs/book/intro.md | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/.gitignore b/.gitignore index 245087a..8d7f3f7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /vendor/ /zf-mkdoc-theme.tgz /zf-mkdoc-theme/ +.idea diff --git a/docs/book/intro.md b/docs/book/intro.md index e7747cc..f7b0789 100644 --- a/docs/book/intro.md +++ b/docs/book/intro.md @@ -87,6 +87,15 @@ the `username`, and the `password`, if required. The SQL structure of this database is stored in the [data/oauth2.sql](https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/data/oauth2.sql) file. +If you already have a PDO service configured, you can simply pass in the service +name to the `pdo` key as follows: + +```php +return [ + 'pdo' => 'myServiceName', +]; +``` + You need to provide an OAuth2 database yourself, or generate a [SQLite](https://www.sqlite.org) database with the following command (using `sqlite3` for GNU/Linux):