5 require_once('../../lib/Status.php');
6 require_once('DB.php');
10 class StatusTest extends \PHPUnit\Framework\TestCase
14 public function testNoDatabaseGiven()
16 $this->setExpectedException(Exception::class, 'No database', 700);
19 $oStatus = new Status($oDB);
20 $this->assertEquals('No database', $oStatus->status());
23 public function testNoDatabaseConnectionFail()
25 $this->setExpectedException(Exception::class, 'No database', 700);
27 // causes 'Non-static method should not be called statically, assuming $this from incompatible context'
29 // $oDB = \DB::connect('', false); // returns a DB_Error instance
32 $oStatus = new Status($oDB);
33 $this->assertEquals('No database', $oStatus->status());
36 $oStatus = new Status($oDB);
37 $this->assertEquals('No database', $oStatus->status());
41 public function testModuleFail()
43 $this->setExpectedException(Exception::class, 'Module call failed', 702);
45 // stub has getOne method but doesn't return anything
46 $oDbStub = $this->getMock(\DB::class, array('getOne'));
48 $oStatus = new Status($oDbStub);
49 $this->assertNull($oStatus->status());
53 public function testWordIdQueryFail()
55 $this->setExpectedException(Exception::class, 'No value', 704);
57 $oDbStub = $this->getMock(\DB::class, array('getOne'));
60 $oDbStub->method('getOne')
61 ->will($this->returnCallback(function ($sql) {
62 if (preg_match("/make_standard_name\('a'\)/", $sql)) return 'a';
63 if (preg_match('/SELECT word_id, word_token/', $sql)) return null;
66 $oStatus = new Status($oDbStub);
67 $this->assertNull($oStatus->status());
71 public function testOK()
73 $oDbStub = $this->getMock(\DB::class, array('getOne'));
75 $oDbStub->method('getOne')
76 ->will($this->returnCallback(function ($sql) {
77 if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
78 if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
81 $oStatus = new Status($oDbStub);
82 $this->assertNull($oStatus->status());
85 public function testDataDate()
87 $oDbStub = $this->getMock(\DB::class, array('getOne'));
89 $oDbStub->method('getOne')
90 ->willReturn(1519430221);
92 $oStatus = new Status($oDbStub);
93 $this->assertEquals(1519430221, $oStatus->dataDate());