Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
2fdfacc9
Commit
2fdfacc9
authored
Jul 06, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit test "AutoTimestampTest" has been reworked to use static model class.
parent
09d84f11
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
56 deletions
+35
-56
AutoTimestampTest.php
tests/unit/framework/behaviors/AutoTimestampTest.php
+35
-56
No files found.
tests/unit/framework/behaviors/AutoTimestampTest.php
View file @
2fdfacc9
<?php
namespace
yiiunit\framework\behaviors
;
use
Yii
;
use
yiiunit\TestCase
;
use
yii\db\Connection
;
use
yii\db\ActiveRecord
;
...
...
@@ -15,21 +18,12 @@ class AutoTimestampTest extends TestCase
* @var Connection test db connection
*/
protected
$dbConnection
;
/**
* @var string test table name.
*/
protected
static
$testTableName
=
'test_table'
;
/**
* @var string test Active Record class name.
*/
protected
static
$testActiveRecordClassName
;
public
static
function
setUpBeforeClass
()
{
if
(
!
extension_loaded
(
'pdo'
)
||
!
extension_loaded
(
'pdo_sqlite'
))
{
static
::
markTestSkipped
(
'PDO and SQLite extensions are required.'
);
}
static
::
$testActiveRecordClassName
=
get_called_class
()
.
'_TestActiveRecord_'
.
sha1
(
uniqid
());
}
public
function
setUp
()
{
...
...
@@ -49,9 +43,7 @@ class AutoTimestampTest extends TestCase
'create_time'
=>
'integer'
,
'update_time'
=>
'integer'
,
);
Yii
::
$app
->
getDb
()
->
createCommand
()
->
createTable
(
self
::
$testTableName
,
$columns
)
->
execute
();
$this
->
declareTestActiveRecordClass
();
Yii
::
$app
->
getDb
()
->
createCommand
()
->
createTable
(
'test_auto_timestamp'
,
$columns
)
->
execute
();
}
public
function
tearDown
()
...
...
@@ -60,54 +52,13 @@ class AutoTimestampTest extends TestCase
parent
::
tearDown
();
}
/**
* Declares test Active Record class with auto timestamp behavior attached.
*/
protected
function
declareTestActiveRecordClass
()
{
$className
=
static
::
$testActiveRecordClassName
;
if
(
class_exists
(
$className
,
false
))
{
return
true
;
}
$activeRecordClassName
=
ActiveRecord
::
className
();
$behaviorClassName
=
AutoTimestamp
::
className
();
$tableName
=
static
::
$testTableName
;
$classDefinitionCode
=
<<<EOL
class {$className} extends {$activeRecordClassName}
{
public function behaviors()
{
return array(
'timestamp' => array(
'class' => '{$behaviorClassName}',
'attributes' => array(
static::EVENT_BEFORE_INSERT => array('create_time', 'update_time'),
static::EVENT_BEFORE_UPDATE => 'update_time',
),
),
);
}
public static function tableName()
{
return '{$tableName}';
}
}
EOL;
eval
(
$classDefinitionCode
);
return
true
;
}
// Tests :
public
function
testNewRecord
()
{
$currentTime
=
time
();
$className
=
static
::
$testActiveRecordClassName
;
$model
=
new
$className
();
$model
=
new
ActiveRecordAutoTimestamp
();
$model
->
save
(
false
);
$this
->
assertTrue
(
$model
->
create_time
>=
$currentTime
);
...
...
@@ -121,8 +72,7 @@ EOL;
{
$currentTime
=
time
();
$className
=
static
::
$testActiveRecordClassName
;
$model
=
new
$className
();
$model
=
new
ActiveRecordAutoTimestamp
();
$model
->
save
(
false
);
$enforcedTime
=
$currentTime
-
100
;
...
...
@@ -134,4 +84,32 @@ EOL;
$this
->
assertEquals
(
$enforcedTime
,
$model
->
create_time
,
'Create time has been set on update!'
);
$this
->
assertTrue
(
$model
->
update_time
>=
$currentTime
,
'Update time has NOT been set on update!'
);
}
}
/**
* Test Active Record class with [[AutoTimestamp]] behavior attached.
*
* @property integer $id
* @property integer $create_time
* @property integer $update_time
*/
class
ActiveRecordAutoTimestamp
extends
ActiveRecord
{
public
function
behaviors
()
{
return
array
(
'timestamp'
=>
array
(
'class'
=>
AutoTimestamp
::
className
(),
'attributes'
=>
array
(
static
::
EVENT_BEFORE_INSERT
=>
array
(
'create_time'
,
'update_time'
),
static
::
EVENT_BEFORE_UPDATE
=>
'update_time'
,
),
),
);
}
public
static
function
tableName
()
{
return
'test_auto_timestamp'
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment