Skip to content

Commit

Permalink
Cleanup SapphireTest and time related tests (#6898)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dhensby authored and Damian Mooyman committed May 18, 2017
1 parent 8ed675d commit 3495c08
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
31 changes: 15 additions & 16 deletions src/Dev/SapphireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -461,7 +462,7 @@ public static function tearDownAfterClass()
public function getFixtureFactory()
{
if (!$this->fixtureFactory) {
$this->fixtureFactory = Injector::inst()->create('SilverStripe\\Dev\\FixtureFactory');
$this->fixtureFactory = Injector::inst()->create(FixtureFactory::class);
}
return $this->fixtureFactory;
}
Expand Down Expand Up @@ -538,7 +539,7 @@ protected function objFromFixture($className, $identifier)
*/
public function loadFixture($fixtureFile)
{
$fixture = Injector::inst()->create('SilverStripe\\Dev\\YamlFixture', $fixtureFile);
$fixture = Injector::inst()->create(YamlFixture::class, $fixtureFile);
$fixture->writeInto($this->getFixtureFactory());
}

Expand Down Expand Up @@ -1061,8 +1062,7 @@ public static function using_temp_db()
{
$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()
Expand All @@ -1074,7 +1074,7 @@ public static function kill_temp_db()
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);
Expand All @@ -1097,7 +1097,7 @@ public static function empty_temp_db()

// 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)) {
Expand All @@ -1110,25 +1110,24 @@ public static function empty_temp_db()
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;
$databaseConfig['timezone'] = '+0:00';
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 () {
Expand All @@ -1142,7 +1141,7 @@ public static function delete_all_temp_dbs()
{
$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;
Expand Down Expand Up @@ -1232,9 +1231,9 @@ public function logInWithPermission($permCode = "ADMIN")
$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();
}
Expand Down
5 changes: 2 additions & 3 deletions tests/php/ORM/DBDatetimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testNice()
{
$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()
Expand All @@ -86,8 +86,7 @@ public function testDate()
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()
Expand Down
4 changes: 2 additions & 2 deletions tests/php/ORM/DBTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public function testParse($input, $expected)
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());
}
}

0 comments on commit 3495c08

Please sign in to comment.