From d2958888388de52df8c3693fd398666d2d4ae739 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Fri, 8 Mar 2019 16:15:49 +1300 Subject: [PATCH] MINOR: Improve type testing --- tests/php/ORM/DatabaseTest.php | 42 +++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/tests/php/ORM/DatabaseTest.php b/tests/php/ORM/DatabaseTest.php index 68fa4c121..a13ff7d97 100644 --- a/tests/php/ORM/DatabaseTest.php +++ b/tests/php/ORM/DatabaseTest.php @@ -222,24 +222,50 @@ class DatabaseTest extends SapphireTest // Dates are returned as strings $this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string'); + + // Ensure that the same is true when calling a query a second time (cached prepared statement) + + $record = DB::prepared_query( + 'SELECT * FROM "DatabaseTest_MyObject" WHERE "ID" = ?', + [ $obj->ID ] + )->record(); + + // IDs and ints are returned as ints + $this->assertInternalType('int', $record['ID'], 'Primary key should be integer (2nd call)'); + $this->assertInternalType('int', $record['MyInt'], 'DBInt fields should be integer (2nd call)'); + + $this->assertInternalType('float', $record['MyFloat'], 'DBFloat fields should be float (2nd call)'); + $this->assertInternalType('float', $record['MyDecimal'], 'DBDecimal fields should be float (2nd call)'); + + // Booleans are returned as ints – we follow MySQL's lead + $this->assertInternalType('int', $record['MyBoolean'], 'DBBoolean fields should be int (2nd call)'); + + // Strings and enums are returned as strings + $this->assertInternalType('string', $record['MyField'], 'DBVarchar fields should be string (2nd call)'); + $this->assertInternalType('string', $record['ClassName'], 'DBEnum fields should be string (2nd call)'); + + // Dates are returned as strings + $this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string (2nd call)'); + + // Ensure that the same is true when using non-prepared statements $record = DB::query('SELECT * FROM "DatabaseTest_MyObject" WHERE "ID" = ' . (int)$obj->ID)->record(); // IDs and ints are returned as ints - $this->assertInternalType('int', $record['ID'], 'Primary key should be integer'); - $this->assertInternalType('int', $record['MyInt'], 'DBInt fields should be integer'); + $this->assertInternalType('int', $record['ID'], 'Primary key should be integer (non-prepared)'); + $this->assertInternalType('int', $record['MyInt'], 'DBInt fields should be integer (non-prepared)'); - $this->assertInternalType('float', $record['MyFloat'], 'DBFloat fields should be float'); - $this->assertInternalType('float', $record['MyDecimal'], 'DBDecimal fields should be float'); + $this->assertInternalType('float', $record['MyFloat'], 'DBFloat fields should be float (non-prepared)'); + $this->assertInternalType('float', $record['MyDecimal'], 'DBDecimal fields should be float (non-prepared)'); // Booleans are returned as ints – we follow MySQL's lead - $this->assertInternalType('int', $record['MyBoolean'], 'DBBoolean fields should be int'); + $this->assertInternalType('int', $record['MyBoolean'], 'DBBoolean fields should be int (non-prepared)'); // Strings and enums are returned as strings - $this->assertInternalType('string', $record['MyField'], 'DBVarchar fields should be string'); - $this->assertInternalType('string', $record['ClassName'], 'DBEnum fields should be string'); + $this->assertInternalType('string', $record['MyField'], 'DBVarchar fields should be string (non-prepared)'); + $this->assertInternalType('string', $record['ClassName'], 'DBEnum fields should be string (non-prepared)'); // Dates are returned as strings - $this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string'); + $this->assertInternalType('string', $record['Created'], 'DBDatetime fields should be string (non-prepared)'); } }