Debian Codegraph @

tests/classes/Database/DatabaseControllerTest.php

raw symbols SCIP
1 <?php
2
3 namespace Liuch\DmarcSrg;
4
5 use Liuch\DmarcSrg\Database\DatabaseController;
6
7 class DatabaseControllerTest extends \PHPUnit\Framework\TestCase
8 {
9 public function testGettingType(): void
10 {
11 $ctl = new DatabaseController($this->getCoreWithSettings([]));
12 $this->assertSame('', $ctl->type());
13 $ctl = new DatabaseController($this->getCoreWithSettings([ 'type' => 'dbType' ]));
14 $this->assertSame('dbType', $ctl->type());
15 }
16
17 public function testGettingName(): void
18 {
19 $ctl = new DatabaseController($this->getCoreWithSettings([]));
20 $this->assertSame('', $ctl->name());
21 $ctl = new DatabaseController($this->getCoreWithSettings([ 'name' => 'dbName' ]));
22 $this->assertSame('dbName', $ctl->name());
23 }
24
25 public function testGettingLocation(): void
26 {
27 $ctl = new DatabaseController($this->getCoreWithSettings([]));
28 $this->assertSame('', $ctl->location());
29 $ctl = new DatabaseController($this->getCoreWithSettings([ 'host' => 'dbLocation' ]));
30 $this->assertSame('dbLocation', $ctl->location());
31 }
32
33 public function testGettingState(): void
34 {
35 $callback = function () {
36 return [
37 'someParam' => 'someValue',
38 'correct' => true,
39 'version' => DatabaseController::REQUIRED_VERSION
40 ];
41 };
42 $ctl = new DatabaseController(
43 $this->getCoreWithSettings([
44 'type' => 'dbType',
45 'name' => 'dbName',
46 'host' => 'dbLocation'
47 ]),
48 $this->getConnector('state', $callback)
49 );
50 $res = $ctl->state();
51 $this->assertIsArray($res);
52 $this->assertTrue($res['correct']);
53 $this->assertFalse($res['needs_upgrade'] ?? false);
54 $this->assertSame('someValue', $res['someParam']);
55 $this->assertSame('dbType', $res['type']);
56 $this->assertSame('dbName', $res['name']);
57 $this->assertSame('dbLocation', $res['location']);
58 }
59
60 public function testGettingStateNeedsUpgrating(): void
61 {
62 $callback1 = function () {
63 return [ 'correct' => true ];
64 };
65 $callback2 = function () {
66 return [ 'correct' => false ];
67 };
68
69 $ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('state', $callback1));
70 $res = $ctl->state();
71 $this->assertFalse($res['correct']);
72 $this->assertTrue($res['needs_upgrade']);
73 $this->assertArrayHasKey('message', $res);
74
75 $ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('state', $callback2));
76 $res = $ctl->state();
77 $this->assertFalse($res['correct']);
78 $this->assertFalse($res['needs_upgrade'] ?? false);
79 }
80
81 public function testInitDb(): void
82 {
83 $callback = function () {
84 };
85 $ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('initDb', $callback));
86 $res = $ctl->initDb();
87 $this->assertIsArray($res);
88 $this->assertSame(0, $res['error_code'] ?? 0);
89 $this->assertArrayHasKey('message', $res);
90 }
91
92 public function testCleanDb(): void
93 {
94 $callback = function () {
95 };
96 $ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('cleanDb', $callback));
97 $res = $ctl->cleanDb();
98 $this->assertIsArray($res);
99 $this->assertSame(0, $res['error_code'] ?? 0);
100 $this->assertArrayHasKey('message', $res);
101 }
102
103 public function testGettingMapper(): void
104 {
105 $callback = function ($param) {
106 $this->assertSame('mapperId', $param);
107 return new \StdClass();
108 };
109 $ctl = new DatabaseController($this->getCoreWithSettings([]), $this->getConnector('getMapper', $callback));
110 $ctl->getMapper('mapperId');
111 }
112
113 private function getCoreWithSettings($data): object
114 {
115 $core = $this->createMock(Core::class);
116 $core->expects($this->once())
117 ->method('config')
118 ->with('database')
119 ->willReturn($data);
120 return $core;
121 }
122
123 private function getConnector(string $method, $callback): object
124 {
125 $con = $this->getMockBuilder(Database\DatabaseConnector::class)
126 ->disableOriginalConstructor()
127 ->getMock();
128 $con->expects($this->once())
129 ->method($this->equalTo($method))
130 ->willReturnCallback($callback);
131 return $con;
132 }
133 }