From 3495c0826e73811886f3fd5feb5e4f62de4b2a71 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Thu, 18 May 2017 11:01:55 +0100 Subject: [PATCH] Cleanup SapphireTest and time related tests (#6898) * Test databases now include timestamp for easier debugging * Use classname::class instead of string literal classnames * Remove DataObject::get_one() from SapphireTest * More fixes to ICU DB inconsitency for time formatting * Correctly restore PHPUnits error handler --- src/Dev/SapphireTest.php | 31 +++++++++++++++---------------- tests/php/ORM/DBDatetimeTest.php | 5 ++--- tests/php/ORM/DBTimeTest.php | 4 ++-- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Dev/SapphireTest.php b/src/Dev/SapphireTest.php index 85603083f..bb861e3f2 100644 --- a/src/Dev/SapphireTest.php +++ b/src/Dev/SapphireTest.php @@ -23,6 +23,7 @@ use SilverStripe\Core\Manifest\ClassManifest; use SilverStripe\Core\Manifest\ClassLoader; use SilverStripe\Core\Resettable; use SilverStripe\i18n\i18n; +use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\SS_List; use SilverStripe\Versioned\Versioned; use SilverStripe\ORM\DataObject; @@ -461,7 +462,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase public function getFixtureFactory() { if (!$this->fixtureFactory) { - $this->fixtureFactory = Injector::inst()->create('SilverStripe\\Dev\\FixtureFactory'); + $this->fixtureFactory = Injector::inst()->create(FixtureFactory::class); } return $this->fixtureFactory; } @@ -538,7 +539,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase */ public function loadFixture($fixtureFile) { - $fixture = Injector::inst()->create('SilverStripe\\Dev\\YamlFixture', $fixtureFile); + $fixture = Injector::inst()->create(YamlFixture::class, $fixtureFile); $fixture->writeInto($this->getFixtureFactory()); } @@ -1061,8 +1062,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase { $dbConn = DB::get_conn(); $prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_'; - return $dbConn && (substr($dbConn->getSelectedDatabase(), 0, strlen($prefix) + 5) - == strtolower(sprintf('%stmpdb', $prefix))); + return 1 === preg_match(sprintf('/^%stmpdb_[0-9]+_[0-9]+$/i', preg_quote($prefix, '/')), $dbConn->getSelectedDatabase()); } public static function kill_temp_db() @@ -1074,7 +1074,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase if ($dbName && DB::get_conn()->databaseExists($dbName)) { // Some DataExtensions keep a static cache of information that needs to // be reset whenever the database is killed - foreach (ClassInfo::subclassesFor('SilverStripe\\ORM\\DataExtension') as $class) { + foreach (ClassInfo::subclassesFor(DataExtension::class) as $class) { $toCall = array($class, 'on_db_reset'); if (is_callable($toCall)) { call_user_func($toCall); @@ -1097,7 +1097,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase // Some DataExtensions keep a static cache of information that needs to // be reset whenever the database is cleaned out - $classes = array_merge(ClassInfo::subclassesFor('SilverStripe\\ORM\\DataExtension'), ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject')); + $classes = array_merge(ClassInfo::subclassesFor(DataExtension::class), ClassInfo::subclassesFor(DataObject::class)); foreach ($classes as $class) { $toCall = array($class, 'on_db_reset'); if (is_callable($toCall)) { @@ -1110,7 +1110,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase public static function create_temp_db() { // Disable PHPUnit error handling - restore_error_handler(); + $oldErrorHandler = set_error_handler(null); // Create a temporary database, and force the connection to use UTC for time global $databaseConfig; @@ -1118,17 +1118,16 @@ class SapphireTest extends PHPUnit_Framework_TestCase DB::connect($databaseConfig); $dbConn = DB::get_conn(); $prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_'; - $dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000, 9999999); - while (!$dbname || $dbConn->databaseExists($dbname)) { - $dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000, 9999999); - } + do { + $dbname = strtolower(sprintf('%stmpdb_%s_%s', $prefix, time(), rand(1000000, 9999999))); + } while ($dbConn->databaseExists($dbname)); $dbConn->selectDatabase($dbname, true); static::resetDBSchema(); // Reinstate PHPUnit error handling - set_error_handler(array('PHPUnit_Util_ErrorHandler', 'handleError')); + set_error_handler($oldErrorHandler); // Ensure test db is killed on exit register_shutdown_function(function () { @@ -1142,7 +1141,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase { $prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_'; foreach (DB::get_schema()->databaseList() as $dbName) { - if (preg_match(sprintf('/^%stmpdb[0-9]+$/', $prefix), $dbName)) { + if (1 === preg_match(sprintf('/^%stmpdb_[0-9]+_[0-9]+$/i', preg_quote($prefix, '/')), $dbName)) { DB::get_schema()->dropDatabase($dbName); if (Director::is_cli()) { echo "Dropped database \"$dbName\"" . PHP_EOL; @@ -1232,9 +1231,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase $group->Permissions()->add($permission); } - $member = DataObject::get_one('SilverStripe\\Security\\Member', array( - '"Member"."Email"' => "$permCode@example.org" - )); + $member = Member::get()->filter([ + 'Email' => "$permCode@example.org", + ])->first(); if (!$member) { $member = Member::create(); } diff --git a/tests/php/ORM/DBDatetimeTest.php b/tests/php/ORM/DBDatetimeTest.php index eb2c546a5..b0e6f11a2 100644 --- a/tests/php/ORM/DBDatetimeTest.php +++ b/tests/php/ORM/DBDatetimeTest.php @@ -74,7 +74,7 @@ class DBDatetimeTest extends SapphireTest { $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); // note: Some localisation packages exclude the ',' in default medium format - $this->assertRegExp('#31/12/2001(,)? 10:10:59 PM#', $date->Nice()); + $this->assertRegExp('#31/12/2001(,)? 10:10:59 PM#i', $date->Nice()); } public function testDate() @@ -86,8 +86,7 @@ class DBDatetimeTest extends SapphireTest public function testTime() { $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); - // casing depends on system ICU library - $this->assertRegexp('#10:10:59 (PM|pm)#', $date->Time()); + $this->assertRegexp('#10:10:59 PM#i', $date->Time()); } public function testTime24() diff --git a/tests/php/ORM/DBTimeTest.php b/tests/php/ORM/DBTimeTest.php index 8f68f5017..80c2f8029 100644 --- a/tests/php/ORM/DBTimeTest.php +++ b/tests/php/ORM/DBTimeTest.php @@ -48,12 +48,12 @@ class DBTimeTest extends SapphireTest public function testNice() { $time = DBTime::create_field('Time', '17:15:55'); - $this->assertEquals('5:15:55 PM', $time->Nice()); + $this->assertRegexp('#5:15:55 PM#i', $time->Nice()); } public function testShort() { $time = DBTime::create_field('Time', '17:15:55'); - $this->assertEquals('5:15 PM', $time->Short()); + $this->assertRegexp('#5:15 PM#i', $time->Short()); } }