mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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
This commit is contained in:
parent
8ed675d29b
commit
3495c0826e
@ -23,6 +23,7 @@ use SilverStripe\Core\Manifest\ClassManifest;
|
|||||||
use SilverStripe\Core\Manifest\ClassLoader;
|
use SilverStripe\Core\Manifest\ClassLoader;
|
||||||
use SilverStripe\Core\Resettable;
|
use SilverStripe\Core\Resettable;
|
||||||
use SilverStripe\i18n\i18n;
|
use SilverStripe\i18n\i18n;
|
||||||
|
use SilverStripe\ORM\DataExtension;
|
||||||
use SilverStripe\ORM\SS_List;
|
use SilverStripe\ORM\SS_List;
|
||||||
use SilverStripe\Versioned\Versioned;
|
use SilverStripe\Versioned\Versioned;
|
||||||
use SilverStripe\ORM\DataObject;
|
use SilverStripe\ORM\DataObject;
|
||||||
@ -461,7 +462,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
|
|||||||
public function getFixtureFactory()
|
public function getFixtureFactory()
|
||||||
{
|
{
|
||||||
if (!$this->fixtureFactory) {
|
if (!$this->fixtureFactory) {
|
||||||
$this->fixtureFactory = Injector::inst()->create('SilverStripe\\Dev\\FixtureFactory');
|
$this->fixtureFactory = Injector::inst()->create(FixtureFactory::class);
|
||||||
}
|
}
|
||||||
return $this->fixtureFactory;
|
return $this->fixtureFactory;
|
||||||
}
|
}
|
||||||
@ -538,7 +539,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
|
|||||||
*/
|
*/
|
||||||
public function loadFixture($fixtureFile)
|
public function loadFixture($fixtureFile)
|
||||||
{
|
{
|
||||||
$fixture = Injector::inst()->create('SilverStripe\\Dev\\YamlFixture', $fixtureFile);
|
$fixture = Injector::inst()->create(YamlFixture::class, $fixtureFile);
|
||||||
$fixture->writeInto($this->getFixtureFactory());
|
$fixture->writeInto($this->getFixtureFactory());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1061,8 +1062,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$dbConn = DB::get_conn();
|
$dbConn = DB::get_conn();
|
||||||
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
|
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
|
||||||
return $dbConn && (substr($dbConn->getSelectedDatabase(), 0, strlen($prefix) + 5)
|
return 1 === preg_match(sprintf('/^%stmpdb_[0-9]+_[0-9]+$/i', preg_quote($prefix, '/')), $dbConn->getSelectedDatabase());
|
||||||
== strtolower(sprintf('%stmpdb', $prefix)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function kill_temp_db()
|
public static function kill_temp_db()
|
||||||
@ -1074,7 +1074,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
|
|||||||
if ($dbName && DB::get_conn()->databaseExists($dbName)) {
|
if ($dbName && DB::get_conn()->databaseExists($dbName)) {
|
||||||
// Some DataExtensions keep a static cache of information that needs to
|
// Some DataExtensions keep a static cache of information that needs to
|
||||||
// be reset whenever the database is killed
|
// 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');
|
$toCall = array($class, 'on_db_reset');
|
||||||
if (is_callable($toCall)) {
|
if (is_callable($toCall)) {
|
||||||
call_user_func($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
|
// Some DataExtensions keep a static cache of information that needs to
|
||||||
// be reset whenever the database is cleaned out
|
// 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) {
|
foreach ($classes as $class) {
|
||||||
$toCall = array($class, 'on_db_reset');
|
$toCall = array($class, 'on_db_reset');
|
||||||
if (is_callable($toCall)) {
|
if (is_callable($toCall)) {
|
||||||
@ -1110,7 +1110,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
|
|||||||
public static function create_temp_db()
|
public static function create_temp_db()
|
||||||
{
|
{
|
||||||
// Disable PHPUnit error handling
|
// 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
|
// Create a temporary database, and force the connection to use UTC for time
|
||||||
global $databaseConfig;
|
global $databaseConfig;
|
||||||
@ -1118,17 +1118,16 @@ class SapphireTest extends PHPUnit_Framework_TestCase
|
|||||||
DB::connect($databaseConfig);
|
DB::connect($databaseConfig);
|
||||||
$dbConn = DB::get_conn();
|
$dbConn = DB::get_conn();
|
||||||
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
|
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
|
||||||
$dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000, 9999999);
|
do {
|
||||||
while (!$dbname || $dbConn->databaseExists($dbname)) {
|
$dbname = strtolower(sprintf('%stmpdb_%s_%s', $prefix, time(), rand(1000000, 9999999)));
|
||||||
$dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000, 9999999);
|
} while ($dbConn->databaseExists($dbname));
|
||||||
}
|
|
||||||
|
|
||||||
$dbConn->selectDatabase($dbname, true);
|
$dbConn->selectDatabase($dbname, true);
|
||||||
|
|
||||||
static::resetDBSchema();
|
static::resetDBSchema();
|
||||||
|
|
||||||
// Reinstate PHPUnit error handling
|
// Reinstate PHPUnit error handling
|
||||||
set_error_handler(array('PHPUnit_Util_ErrorHandler', 'handleError'));
|
set_error_handler($oldErrorHandler);
|
||||||
|
|
||||||
// Ensure test db is killed on exit
|
// Ensure test db is killed on exit
|
||||||
register_shutdown_function(function () {
|
register_shutdown_function(function () {
|
||||||
@ -1142,7 +1141,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
|
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'ss_';
|
||||||
foreach (DB::get_schema()->databaseList() as $dbName) {
|
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);
|
DB::get_schema()->dropDatabase($dbName);
|
||||||
if (Director::is_cli()) {
|
if (Director::is_cli()) {
|
||||||
echo "Dropped database \"$dbName\"" . PHP_EOL;
|
echo "Dropped database \"$dbName\"" . PHP_EOL;
|
||||||
@ -1232,9 +1231,9 @@ class SapphireTest extends PHPUnit_Framework_TestCase
|
|||||||
$group->Permissions()->add($permission);
|
$group->Permissions()->add($permission);
|
||||||
}
|
}
|
||||||
|
|
||||||
$member = DataObject::get_one('SilverStripe\\Security\\Member', array(
|
$member = Member::get()->filter([
|
||||||
'"Member"."Email"' => "$permCode@example.org"
|
'Email' => "$permCode@example.org",
|
||||||
));
|
])->first();
|
||||||
if (!$member) {
|
if (!$member) {
|
||||||
$member = Member::create();
|
$member = Member::create();
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ class DBDatetimeTest extends SapphireTest
|
|||||||
{
|
{
|
||||||
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
|
||||||
// note: Some localisation packages exclude the ',' in default medium format
|
// 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()
|
public function testDate()
|
||||||
@ -86,8 +86,7 @@ class DBDatetimeTest extends SapphireTest
|
|||||||
public function testTime()
|
public function testTime()
|
||||||
{
|
{
|
||||||
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
|
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
|
||||||
// casing depends on system ICU library
|
$this->assertRegexp('#10:10:59 PM#i', $date->Time());
|
||||||
$this->assertRegexp('#10:10:59 (PM|pm)#', $date->Time());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTime24()
|
public function testTime24()
|
||||||
|
@ -48,12 +48,12 @@ class DBTimeTest extends SapphireTest
|
|||||||
public function testNice()
|
public function testNice()
|
||||||
{
|
{
|
||||||
$time = DBTime::create_field('Time', '17:15:55');
|
$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()
|
public function testShort()
|
||||||
{
|
{
|
||||||
$time = DBTime::create_field('Time', '17:15:55');
|
$time = DBTime::create_field('Time', '17:15:55');
|
||||||
$this->assertEquals('5:15 PM', $time->Short());
|
$this->assertRegexp('#5:15 PM#i', $time->Short());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user