2018-11-23 00:55:54 +00:00
< ? php
/** @ license MIT
* Copyright 2017 J . King , Dustin Wilson et al .
* See LICENSE and AUTHORS files for details */
declare ( strict_types = 1 );
namespace JKingWeb\Arsse\TestCase\Db ;
use JKingWeb\Arsse\Db\Result ;
abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest {
2018-12-20 23:06:28 +00:00
protected static $insertDefault = " INSERT INTO arsse_test default values " ;
2018-11-27 19:26:33 +00:00
protected static $interface ;
2018-11-23 00:55:54 +00:00
protected $resultClass ;
abstract protected function makeResult ( string $q ) : array ;
2019-10-16 18:42:43 +00:00
public static function setUpBeforeClass () : void {
2018-11-27 19:26:33 +00:00
// establish a clean baseline
static :: clearData ();
static :: setConf ();
2019-01-12 17:43:06 +00:00
static :: $interface = static :: dbInterface ();
2018-11-27 19:26:33 +00:00
}
2020-03-01 20:16:50 +00:00
2019-10-16 18:42:43 +00:00
public function setUp () : void {
2018-11-23 15:01:17 +00:00
self :: clearData ();
2018-11-23 00:55:54 +00:00
self :: setConf ();
2018-11-27 19:26:33 +00:00
if ( ! static :: $interface ) {
$this -> markTestSkipped ( static :: $implementation . " database driver not available " );
2018-11-23 00:55:54 +00:00
}
2018-11-27 19:26:33 +00:00
// completely clear the database
2019-01-12 17:43:06 +00:00
static :: dbRaze ( static :: $interface );
$this -> resultClass = static :: $dbResultClass ;
2018-11-23 00:55:54 +00:00
}
2019-10-16 18:42:43 +00:00
public function tearDown () : void {
2018-11-27 19:26:33 +00:00
self :: clearData ();
}
2019-10-16 18:42:43 +00:00
public static function tearDownAfterClass () : void {
2018-12-10 18:17:04 +00:00
if ( static :: $interface ) {
// completely clear the database
2019-01-12 17:43:06 +00:00
static :: dbRaze ( static :: $interface );
2018-12-10 18:17:04 +00:00
}
static :: $interface = null ;
2018-11-23 15:01:17 +00:00
self :: clearData ();
2018-11-23 00:55:54 +00:00
}
2020-01-20 18:52:48 +00:00
public function testConstructResult () : void {
2018-11-23 00:55:54 +00:00
$this -> assertInstanceOf ( Result :: class , new $this -> resultClass ( ... $this -> makeResult ( " SELECT 1 " )));
}
2020-01-20 18:52:48 +00:00
public function testGetChangeCountAndLastInsertId () : void {
2018-12-06 22:46:00 +00:00
$this -> makeResult ( static :: $createMeta );
2018-12-20 23:06:28 +00:00
$r = new $this -> resultClass ( ... $this -> makeResult ( " INSERT INTO arsse_meta( \" key \" ,value) values('test', 1) " ));
2018-12-06 22:46:00 +00:00
$this -> assertSame ( 1 , $r -> changes ());
$this -> assertSame ( 0 , $r -> lastId ());
}
2020-01-20 18:52:48 +00:00
public function testGetChangeCountAndLastInsertIdBis () : void {
2018-12-06 22:46:00 +00:00
$this -> makeResult ( static :: $createTest );
2018-12-20 23:06:28 +00:00
$r = new $this -> resultClass ( ... $this -> makeResult ( static :: $insertDefault ));
2018-12-06 22:46:00 +00:00
$this -> assertSame ( 1 , $r -> changes ());
$this -> assertSame ( 1 , $r -> lastId ());
2018-12-20 23:06:28 +00:00
$r = new $this -> resultClass ( ... $this -> makeResult ( static :: $insertDefault ));
2018-12-06 22:46:00 +00:00
$this -> assertSame ( 1 , $r -> changes ());
$this -> assertSame ( 2 , $r -> lastId ());
2018-11-23 00:55:54 +00:00
}
2020-01-20 18:52:48 +00:00
public function testIterateOverResults () : void {
2018-11-23 00:55:54 +00:00
$exp = [ 0 => 1 , 1 => 2 , 2 => 3 ];
2019-01-12 17:43:06 +00:00
$exp = static :: $stringOutput ? $this -> stringify ( $exp ) : $exp ;
2018-11-23 00:55:54 +00:00
foreach ( new $this -> resultClass ( ... $this -> makeResult ( " SELECT 1 as col union select 2 as col union select 3 as col " )) as $index => $row ) {
$rows [ $index ] = $row [ 'col' ];
}
$this -> assertSame ( $exp , $rows );
}
2020-01-20 18:52:48 +00:00
public function testIterateOverResultsTwice () : void {
2018-11-23 00:55:54 +00:00
$exp = [ 0 => 1 , 1 => 2 , 2 => 3 ];
2019-01-12 17:43:06 +00:00
$exp = static :: $stringOutput ? $this -> stringify ( $exp ) : $exp ;
2018-11-23 00:55:54 +00:00
$result = new $this -> resultClass ( ... $this -> makeResult ( " SELECT 1 as col union select 2 as col union select 3 as col " ));
foreach ( $result as $index => $row ) {
$rows [ $index ] = $row [ 'col' ];
}
$this -> assertSame ( $exp , $rows );
$this -> assertException ( " resultReused " , " Db " );
foreach ( $result as $row ) {
$rows [] = $row [ 'col' ];
}
}
2020-01-20 18:52:48 +00:00
public function testGetSingleValues () : void {
2018-11-23 00:55:54 +00:00
$exp = [ 1867 , 1970 , 2112 ];
2019-01-12 17:43:06 +00:00
$exp = static :: $stringOutput ? $this -> stringify ( $exp ) : $exp ;
2018-12-06 22:46:00 +00:00
$test = new $this -> resultClass ( ... $this -> makeResult ( " SELECT 1867 as year union all select 1970 as year union all select 2112 as year " ));
2018-11-23 00:55:54 +00:00
$this -> assertSame ( $exp [ 0 ], $test -> getValue ());
$this -> assertSame ( $exp [ 1 ], $test -> getValue ());
$this -> assertSame ( $exp [ 2 ], $test -> getValue ());
$this -> assertSame ( null , $test -> getValue ());
}
2020-01-20 18:52:48 +00:00
public function testGetFirstValuesOnly () : void {
2018-11-23 00:55:54 +00:00
$exp = [ 1867 , 1970 , 2112 ];
2019-01-12 17:43:06 +00:00
$exp = static :: $stringOutput ? $this -> stringify ( $exp ) : $exp ;
2018-12-06 22:46:00 +00:00
$test = new $this -> resultClass ( ... $this -> makeResult ( " SELECT 1867 as year, 19 as century union all select 1970 as year, 20 as century union all select 2112 as year, 22 as century " ));
2018-11-23 00:55:54 +00:00
$this -> assertSame ( $exp [ 0 ], $test -> getValue ());
$this -> assertSame ( $exp [ 1 ], $test -> getValue ());
$this -> assertSame ( $exp [ 2 ], $test -> getValue ());
$this -> assertSame ( null , $test -> getValue ());
}
2020-01-20 18:52:48 +00:00
public function testGetRows () : void {
2018-11-23 00:55:54 +00:00
$exp = [
[ 'album' => '2112' , 'track' => '2112' ],
[ 'album' => 'Clockwork Angels' , 'track' => 'The Wreckers' ],
];
$test = new $this -> resultClass ( ... $this -> makeResult ( " SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track " ));
$this -> assertSame ( $exp [ 0 ], $test -> getRow ());
$this -> assertSame ( $exp [ 1 ], $test -> getRow ());
$this -> assertSame ( null , $test -> getRow ());
}
2020-01-20 18:52:48 +00:00
public function testGetAllRows () : void {
2018-11-23 00:55:54 +00:00
$exp = [
[ 'album' => '2112' , 'track' => '2112' ],
[ 'album' => 'Clockwork Angels' , 'track' => 'The Wreckers' ],
];
$test = new $this -> resultClass ( ... $this -> makeResult ( " SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track " ));
$this -> assertEquals ( $exp , $test -> getAll ());
}
}