Test Failed
Pull Request — master (#2)
by Alex
04:49 queued 01:37
created

testImplementsConfigurationInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\LaminasDoctrine\Service\Configuration;
6
7
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationFactory;
8
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationFactoryInterface;
9
use Arp\LaminasDoctrine\Service\Configuration\Exception\ConfigurationFactoryException;
10
use Doctrine\ORM\Configuration;
11
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
12
use Laminas\ServiceManager\ServiceLocatorInterface;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * @covers  \Arp\LaminasDoctrine\Service\Configuration\ConfigurationFactory
18
 *
19
 * @author  Alex Patterson <[email protected]>
20
 * @package ArpTest\LaminasDoctrine\Service\Configuration
21
 */
22
final class ConfigurationFactoryTest extends TestCase
23
{
24
    /**
25
     * @var ServiceLocatorInterface|MockObject
26
     */
27
    private $serviceManager;
28
29
    /**
30
     * Prepare the test case dependencies
31
     */
32
    public function setUp(): void
33
    {
34
        $this->serviceManager = $this->createMock(ServiceLocatorInterface::class);
35
    }
36
37
    /**
38
     * Assert that the ConfigurationFactory implement ConfigurationFactoryInterface
39
     */
40
    public function testImplementsConfigurationInterface(): void
41
    {
42
        $factory = new ConfigurationFactory($this->serviceManager);
43
44
        $this->assertInstanceOf(ConfigurationFactoryInterface::class, $factory);
45
    }
46
47
    /**
48
     * Assert that a ConfigurationFactoryException is thrown when create() is unable to create an instance
49
     *
50
     * @throws ConfigurationFactoryException
51
     */
52
    public function testCreateWillThrowConfigurationFactoryExceptionIfUnableToCreate(): void
53
    {
54
        $config = [
55
            'foo' => 123,
56
            'bar' => true,
57
            'test' => 'Hello'
58
        ];
59
60
        $factory = new ConfigurationFactory($this->serviceManager);
61
62
        $exceptionMessage = 'This is a test exception message for ' . __FUNCTION__;
63
        $exceptionCode = 999;
64
        $exception = new ServiceNotCreatedException($exceptionMessage, $exceptionCode);
65
66
        $this->serviceManager->expects($this->once())
67
            ->method('build')
68
            ->with(Configuration::class, $config)
69
            ->willThrowException($exception);
70
71
        $this->expectException(ConfigurationFactoryException::class);
72
        $this->expectExceptionCode($exceptionCode);
73
        $this->expectExceptionMessage(
74
            sprintf('Failed to create ORM Configuration: %s', $exceptionMessage),
75
        );
76
77
        $factory->create($config);
78
    }
79
80
    /**
81
     * Assert create() will return a ORM Configuration based on the provided $config
82
     *
83
     * @throws ConfigurationFactoryException
84
     */
85
    public function testCreateReturnAConfiguredOrmConfigurationInstance(): void
86
    {
87
        $config = [
88
            'foo' => 123,
89
            'bar' => true,
90
            'test' => 'Hello'
91
        ];
92
93
        $factory = new ConfigurationFactory($this->serviceManager);
94
95
        /** @var Configuration|MockObject $configuration */
96
        $configuration = $this->createMock(Configuration::class);
97
98
        $this->serviceManager->expects($this->once())
99
            ->method('build')
100
            ->with(Configuration::class, $config)
101
            ->willReturn($configuration);
102
103
        $this->assertSame($configuration, $factory->create($config));
104
    }
105
}
106