This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
PdoServiceFactoryTest.php
111 lines (91 loc) · 3.86 KB
/
PdoServiceFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* @see https://github.com/zendframework/zend-expressive-authentication-oauth2 for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive-authentication-oauth2/blob/master/LICENSE.md
* New BSD License
*/
declare(strict_types=1);
namespace ZendTest\Expressive\Authentication\OAuth2\Repository\Pdo;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Authentication\OAuth2\Exception;
use Zend\Expressive\Authentication\OAuth2\Repository\Pdo\PdoService;
use Zend\Expressive\Authentication\OAuth2\Repository\Pdo\PdoServiceFactory;
class PdoServiceFactoryTest extends TestCase
{
public function setUp()
{
$this->container = $this->prophesize(ContainerInterface::class);
$this->factory = new PdoServiceFactory();
}
public function invalidConfiguration()
{
// @codingStandardsIgnoreStart
return [
'no-config-service' => [false, [], 'PDO configuration is missing'],
'config-empty' => [true, [], 'PDO configuration is missing'],
'config-authentication-empty' => [true, ['authentication' => []], 'PDO configuration is missing'],
'config-authentication-pdo-empty' => [true, ['authentication' => ['pdo' => null]], 'PDO configuration is missing'],
'config-authentication-pdo-dsn-empty' => [true, ['authentication' => ['pdo' => ['dsn' => null]]], 'DSN configuration is missing'],
];
// @codingStandardsIgnoreEnd
}
/**
* @dataProvider invalidConfiguration
*/
public function testRaisesExceptionIfPdoConfigurationIsMissing(
bool $hasConfig,
array $config,
string $expectedMessage
) {
$this->container->has('config')->willReturn($hasConfig);
if ($hasConfig) {
$this->container->get('config')->willReturn($config)->shouldBeCalled();
} else {
$this->container->get('config')->shouldNotBeCalled();
}
$this->expectException(Exception\InvalidConfigException::class);
$this->expectExceptionMessage($expectedMessage);
($this->factory)($this->container->reveal());
}
public function testValidConfigurationResultsInReturnedPdoServiceInstance()
{
$this->container->has('config')->willReturn(true);
$this->container->get('config')->willReturn([
'authentication' => [
'pdo' => [
'dsn' => 'sqlite::memory:',
],
],
]);
$pdo = ($this->factory)($this->container->reveal());
$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());
}
}