Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Allow existing PDO service to be used #41

Merged
merged 5 commits into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/vendor/
/zf-mkdoc-theme.tgz
/zf-mkdoc-theme/
.idea
10 changes: 10 additions & 0 deletions docs/book/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,21 @@ 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',
];
```

The `grants` array is for enabling/disabling grants. By default all the supported
grants are configured to be available. If you would like to disable any of the
supplied grants, simply change the value for the grant to NULL. Additionally,
you can extend this array to add your own custom grants.


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):

Expand Down
4 changes: 2 additions & 2 deletions src/Repository/Pdo/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Repository/Pdo/PdoServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'
Expand Down
35 changes: 35 additions & 0 deletions test/Repository/Pdo/PdoServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}