5 require_once(CONST_BasePath.'/lib/db.php');
 
   6 require_once(CONST_BasePath.'/lib/Status.php');
 
   9 class StatusTest extends \PHPUnit\Framework\TestCase
 
  12     public function testNoDatabaseGiven()
 
  14         $this->expectException(\Exception::class);
 
  15         $this->expectExceptionMessage('No database');
 
  16         $this->expectExceptionCode(700);
 
  19         $oStatus = new Status($oDB);
 
  20         $this->assertEquals('No database', $oStatus->status());
 
  23     public function testNoDatabaseConnectionFail()
 
  25         $this->expectException(\Exception::class);
 
  26         $this->expectExceptionMessage('No database');
 
  27         $this->expectExceptionCode(700);
 
  29         // causes 'Non-static method should not be called statically, assuming $this from incompatible context'
 
  31         // $oDB = \DB::connect('', false); // returns a DB_Error instance
 
  34         $oStatus = new Status($oDB);
 
  35         $this->assertEquals('No database', $oStatus->status());
 
  38         $oStatus = new Status($oDB);
 
  39         $this->assertEquals('No database', $oStatus->status());
 
  43     public function testModuleFail()
 
  45         $this->expectException(\Exception::class);
 
  46         $this->expectExceptionMessage('Module call failed');
 
  47         $this->expectExceptionCode(702);
 
  49         // stub has getOne method but doesn't return anything
 
  50         $oDbStub = $this->getMockBuilder(\DB::class)
 
  51                         ->setMethods(array('getOne'))
 
  54         $oStatus = new Status($oDbStub);
 
  55         $this->assertNull($oStatus->status());
 
  59     public function testWordIdQueryFail()
 
  61         $this->expectException(\Exception::class);
 
  62         $this->expectExceptionMessage('No value');
 
  63         $this->expectExceptionCode(704);
 
  65         $oDbStub = $this->getMockBuilder(\DB::class)
 
  66                         ->setMethods(array('getOne'))
 
  70         $oDbStub->method('getOne')
 
  71                 ->will($this->returnCallback(function ($sql) {
 
  72                     if (preg_match("/make_standard_name\('a'\)/", $sql)) return 'a';
 
  73                     if (preg_match('/SELECT word_id, word_token/', $sql)) return null;
 
  76         $oStatus = new Status($oDbStub);
 
  77         $this->assertNull($oStatus->status());
 
  81     public function testOK()
 
  83         $oDbStub = $this->getMockBuilder(\DB::class)
 
  84                         ->setMethods(array('getOne'))
 
  87         $oDbStub->method('getOne')
 
  88                 ->will($this->returnCallback(function ($sql) {
 
  89                     if (preg_match("/make_standard_name\('(\w+)'\)/", $sql, $aMatch)) return $aMatch[1];
 
  90                     if (preg_match('/SELECT word_id, word_token/', $sql)) return 1234;
 
  93         $oStatus = new Status($oDbStub);
 
  94         $this->assertNull($oStatus->status());
 
  97     public function testDataDate()
 
  99         $oDbStub = $this->getMockBuilder(\DB::class)
 
 100                         ->setMethods(array('getOne'))
 
 103         $oDbStub->method('getOne')
 
 104                 ->willReturn(1519430221);
 
 106         $oStatus = new Status($oDbStub);
 
 107         $this->assertEquals(1519430221, $oStatus->dataDate());