summaryrefslogtreecommitdiff
path: root/tests/Unit/Database/DatabaseServiceProviderTest.php
blob: 241c47b701b30f2d4b9381aca93d3c87d333c7f7 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

namespace Engelsystem\Test\Unit\Database;

use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Database\Database;
use Engelsystem\Database\DatabaseServiceProvider;
use Engelsystem\Database\Db;
use Engelsystem\Test\Unit\ServiceProviderTest;
use Exception;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Database\Connection;
use PDO;
use PDOException;
use PHPUnit\Framework\MockObject\MockObject;

class DatabaseServiceProviderTest extends ServiceProviderTest
{
    /**
     * @covers \Engelsystem\Database\DatabaseServiceProvider::register()
     */
    public function testRegister()
    {
        /** @var Application|MockObject $app */
        /** @var CapsuleManager|MockObject $dbManager */
        /** @var PDO|MockObject $pdo */
        /** @var Database|MockObject $database */
        /** @var Connection|MockObject $connection */
        list($app, $dbManager, $pdo, $database, $connection) = $this->prepare(
            [
                'driver'   => 'sqlite',
                'database' => ':memory:',
            ]
        );

        $app->expects($this->exactly(7))
            ->method('instance')
            ->withConsecutive(
                [CapsuleManager::class, $dbManager],
                [Db::class, $dbManager],
                [Connection::class, $connection],
                [Database::class, $database],
                ['db', $database],
                ['db.pdo', $pdo],
                ['db.connection', $connection]
            );

        $serviceProvider = new DatabaseServiceProvider($app);
        $serviceProvider->register();
    }

    /**
     * @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError()
     * @covers \Engelsystem\Database\DatabaseServiceProvider::register()
     */
    public function testRegisterError()
    {
        list($app) = $this->prepare([
            'host'     => 'localhost',
            'database' => 'database',
            'username' => 'user',
            'password' => 'password',
        ], true);

        $this->expectException(Exception::class);

        $serviceProvider = new DatabaseServiceProvider($app);
        $serviceProvider->register();
    }

    /**
     * Prepare some mocks
     *
     * @param array $dbConfigData
     * @param bool  $getPdoThrowException
     * @return array
     */
    protected function prepare($dbConfigData, $getPdoThrowException = false)
    {
        /** @var Config|MockObject $config */
        $config = $this->getMockBuilder(Config::class)
            ->getMock();
        /** @var CapsuleManager|MockObject $config */
        $dbManager = $this->getMockBuilder(CapsuleManager::class)
            ->getMock();
        /** @var Connection|MockObject $connection */
        $connection = $this->getMockBuilder(Connection::class)
            ->disableOriginalConstructor()
            ->getMock();
        /** @var PDO|MockObject $pdo */
        $pdo = $this->getMockBuilder(PDO::class)
            ->disableOriginalConstructor()
            ->getMock();
        /** @var Database|MockObject $database */
        $database = $this->getMockBuilder(Database::class)
            ->disableOriginalConstructor()
            ->getMock();

        $app = $this->getApp(['get', 'make', 'instance']);

        $this->setExpects($app, 'get', ['config'], $config);
        $config->expects($this->exactly(2))
            ->method('get')
            ->withConsecutive(['timezone'], ['database'])
            ->willReturnOnConsecutiveCalls('UTC', $dbConfigData);

        $app->expects($this->atLeastOnce())
            ->method('make')
            ->withConsecutive(
                [CapsuleManager::class],
                [Database::class]
            )
            ->willReturn(
                $dbManager,
                $database
            );

        $this->setExpects($dbManager, 'setAsGlobal');
        $this->setExpects($dbManager, 'bootEloquent');

        $this->setExpects($connection, 'useDefaultSchemaGrammar');
        $connection->expects($this->once())
            ->method('getPdo')
            ->willReturnCallback(function () use ($getPdoThrowException, $pdo) {
                if ($getPdoThrowException) {
                    throw new PDOException();
                }

                return $pdo;
            });
        $this->setExpects($dbManager, 'getConnection', [], $connection, $this->atLeastOnce());

        return [$app, $dbManager, $pdo, $database, $connection];
    }
}