5 require_once(CONST_BasePath.'/lib/Status.php');
8 class StatusTest extends \PHPUnit\Framework\TestCase
11 public function testNoDatabaseGiven()
13 $this->expectException(\Exception::class);
14 $this->expectExceptionMessage('No database');
15 $this->expectExceptionCode(700);
18 $oStatus = new Status($oDB);
19 $this->assertEquals('No database', $oStatus->status());
22 public function testNoDatabaseConnectionFail()
24 $this->expectException(\Exception::class);
25 $this->expectExceptionMessage('No database');
26 $this->expectExceptionCode(700);
28 // causes 'Non-static method should not be called statically, assuming $this from incompatible context'
30 // $oDB = \DB::connect('', false); // returns a DB_Error instance
33 $oStatus = new Status($oDB);
34 $this->assertEquals('No database', $oStatus->status());
37 $oStatus = new Status($oDB);
38 $this->assertEquals('No database', $oStatus->status());
42 public function testModuleFail()
44 $this->expectException(\Exception::class);
45 $this->expectExceptionMessage('Module call failed');
46 $this->expectExceptionCode(702);
48 // stub has getOne method but doesn't return anything
49 $oDbStub = $this->getMockBuilder(\DB::class)
50 ->setMethods(array('getOne'))
53 $oStatus = new Status($oDbStub);
54 $this->assertNull($oStatus->status());
58 public function testWordIdQueryFail()
60 $this->expectException(\Exception::class);
61 $this->expectExceptionMessage('No value');
62 $this->expectExceptionCode(704);
64 $oDbStub = $this->getMockBuilder(\DB::class)
65 ->setMethods(array('getOne'))
69 $oDbStub->method('getOne')
70 ->will($this->returnCallback(function ($sql) {
71 if (preg_match("/make_standard_name\('a'\)/", $sql)) return 'a';
72 if (preg_match('/SELECT word_id, word_token/', $sql)) return null;
75 $oStatus = new Status($oDbStub);
76 $this->assertNull($oStatus->status());
80 public function testOK()
82 $oDbStub = $this->getMockBuilder(\DB::class)
83 ->setMethods(array('getOne'))
86 $oDbStub->method('getOne')
87 ->will($this->returnCallback(function ($sql) {
88 if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
89 if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
92 $oStatus = new Status($oDbStub);
93 $this->assertNull($oStatus->status());
96 public function testDataDate()
98 $oDbStub = $this->getMockBuilder(\DB::class)
99 ->setMethods(array('getOne'))
102 $oDbStub->method('getOne')
103 ->willReturn(1519430221);
105 $oStatus = new Status($oDbStub);
106 $this->assertEquals(1519430221, $oStatus->dataDate());