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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
5c8927af
Commit
5c8927af
authored
Aug 17, 2013
by
Suralc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ExistValidatorTest and added new option to DatabaseTestCase
parent
01330696
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
261 additions
and
3 deletions
+261
-3
mysql.sql
tests/unit/data/mysql.sql
+32
-0
postgres.sql
tests/unit/data/postgres.sql
+30
-0
sqlite.sql
tests/unit/data/sqlite.sql
+33
-2
ExistValidatorMainModel.php
...s/unit/data/validators/models/ExistValidatorMainModel.php
+21
-0
ExistValidatorRefModel.php
tests/unit/data/validators/models/ExistValidatorRefModel.php
+23
-0
DatabaseTestCase.php
tests/unit/framework/db/DatabaseTestCase.php
+5
-1
ExistValidatorPostgresTest.php
.../ExistValidatorDriverTests/ExistValidatorPostgresTest.php
+11
-0
ExistValidatorSQliteTest.php
...rs/ExistValidatorDriverTests/ExistValidatorSQliteTest.php
+11
-0
ExistValidatorTest.php
tests/unit/framework/validators/ExistValidatorTest.php
+95
-0
No files found.
tests/unit/data/mysql.sql
View file @
5c8927af
...
...
@@ -100,3 +100,35 @@ INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (2, 4,
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
2
,
5
,
1
,
15
.
0
);
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
2
,
3
,
1
,
8
.
0
);
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
3
,
2
,
1
,
40
.
0
);
/**
* (MySQL-)Database Schema for ExistValidatorTest
*/
DROP
TABLE
IF
EXISTS
tbl_validator_exist_main
CASCADE
;
DROP
TABLE
IF
EXISTS
tbl_validator_exist_ref
CASCADE
;
CREATE
TABLE
`tbl_validator_exist_main`
(
`id`
INT
(
11
)
NOT
NULL
AUTO_INCREMENT
,
`field1`
VARCHAR
(
255
),
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
CREATE
TABLE
`tbl_validator_exist_ref`
(
`id`
INT
(
11
)
NOT
NULL
AUTO_INCREMENT
,
`a_field`
VARCHAR
(
255
),
`ref`
INT
(
11
),
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8
;
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
1
,
'just a string1'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
2
,
'just a string2'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
3
,
'just a string3'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
4
,
'just a string4'
);
INSERT
INTO
tbl_validator_exist_ref
(
a_field
,
ref
)
VALUES
(
'ref_to_2'
,
2
);
INSERT
INTO
tbl_validator_exist_ref
(
a_field
,
ref
)
VALUES
(
'ref_to_2'
,
2
);
INSERT
INTO
tbl_validator_exist_ref
(
a_field
,
ref
)
VALUES
(
'ref_to_3'
,
3
);
INSERT
INTO
tbl_validator_exist_ref
(
a_field
,
ref
)
VALUES
(
'ref_to_4'
,
4
);
INSERT
INTO
tbl_validator_exist_ref
(
a_field
,
ref
)
VALUES
(
'ref_to_4'
,
4
);
INSERT
INTO
tbl_validator_exist_ref
(
a_field
,
ref
)
VALUES
(
'ref_to_5'
,
5
);
tests/unit/data/postgres.sql
View file @
5c8927af
...
...
@@ -92,3 +92,32 @@ INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (2, 4,
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
2
,
5
,
1
,
15
.
0
);
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
2
,
3
,
1
,
8
.
0
);
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
3
,
2
,
1
,
40
.
0
);
/**
* (Postgres-)Database Schema for ExistValidatorTest
*/
DROP
TABLE
IF
EXISTS
tbl_validator_exist_main
CASCADE
;
DROP
TABLE
IF
EXISTS
tbl_validator_exist_ref
CASCADE
;
CREATE
TABLE
tbl_validator_exist_main
(
id
integer
not
null
primary
key
,
field1
VARCHAR
(
255
)
);
CREATE
TABLE
tbl_validator_exist_ref
(
id
integer
not
null
primary
key
,
a_field
VARCHAR
(
255
),
ref
integer
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
1
,
'just a string1'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
2
,
'just a string2'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
3
,
'just a string3'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
4
,
'just a string4'
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
1
,
'ref_to_2'
,
2
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
2
,
'ref_to_2'
,
2
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
3
,
'ref_to_3'
,
3
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
4
,
'ref_to_4'
,
4
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
5
,
'ref_to_4'
,
4
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
6
,
'ref_to_5'
,
5
);
\ No newline at end of file
tests/unit/data/sqlite.sql
View file @
5c8927af
...
...
@@ -85,4 +85,35 @@ INSERT INTO tbl_order_item (order_id, item_id, quantity, subtotal) VALUES (1, 2,
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
2
,
4
,
1
,
10
.
0
);
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
2
,
5
,
1
,
15
.
0
);
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
2
,
3
,
1
,
8
.
0
);
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
3
,
2
,
1
,
40
.
0
);
\ No newline at end of file
INSERT
INTO
tbl_order_item
(
order_id
,
item_id
,
quantity
,
subtotal
)
VALUES
(
3
,
2
,
1
,
40
.
0
);
/**
* (SqLite-)Database Schema for ExistValidatorTest
*/
DROP
TABLE
IF
EXISTS
tbl_validator_exist_main
;
DROP
TABLE
IF
EXISTS
tbl_validator_exist_ref
;
CREATE
TABLE
tbl_validator_exist_main
(
id
INT
(
11
)
NOT
NULL
,
field1
VARCHAR
(
255
),
PRIMARY
KEY
(
id
)
);
CREATE
TABLE
tbl_validator_exist_ref
(
id
INT
(
11
)
NOT
NULL
,
a_field
VARCHAR
(
255
),
ref
INT
(
11
),
PRIMARY
KEY
(
id
)
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
1
,
'just a string1'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
2
,
'just a string2'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
3
,
'just a string3'
);
INSERT
INTO
tbl_validator_exist_main
(
id
,
field1
)
VALUES
(
4
,
'just a string4'
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
1
,
'ref_to_2'
,
2
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
2
,
'ref_to_2'
,
2
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
3
,
'ref_to_3'
,
3
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
4
,
'ref_to_4'
,
4
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
5
,
'ref_to_4'
,
4
);
INSERT
INTO
tbl_validator_exist_ref
(
id
,
a_field
,
ref
)
VALUES
(
6
,
'ref_to_5'
,
5
);
\ No newline at end of file
tests/unit/data/validators/models/ExistValidatorMainModel.php
0 → 100644
View file @
5c8927af
<?php
namespace
yiiunit\data\validators\models
;
use
yii\db\ActiveRecord
;
class
ExistValidatorMainModel
extends
ActiveRecord
{
public
$testMainVal
=
1
;
public
static
function
tableName
()
{
return
'tbl_validator_exist_main'
;
}
public
function
getReferences
()
{
return
$this
->
hasMany
(
ExistValidatorRefModel
::
className
(),
array
(
'ref'
=>
'id'
));
}
}
\ No newline at end of file
tests/unit/data/validators/models/ExistValidatorRefModel.php
0 → 100644
View file @
5c8927af
<?php
namespace
yiiunit\data\validators\models
;
use
yii\db\ActiveRecord
;
class
ExistValidatorRefModel
extends
ActiveRecord
{
public
$test_val
=
2
;
public
$test_val_fail
=
99
;
public
static
function
tableName
()
{
return
'tbl_validator_exist_ref'
;
}
public
function
getMain
()
{
return
$this
->
hasOne
(
ExistValidatorMainModel
::
className
(),
array
(
'id'
=>
'ref'
));
}
}
\ No newline at end of file
tests/unit/framework/db/DatabaseTestCase.php
View file @
5c8927af
...
...
@@ -8,6 +8,7 @@ abstract class DatabaseTestCase extends TestCase
protected
$database
;
protected
$driverName
=
'mysql'
;
protected
$db
;
protected
$initializeAppWithDb
=
false
;
protected
function
setUp
()
{
...
...
@@ -18,7 +19,10 @@ abstract class DatabaseTestCase extends TestCase
$pdo_database
=
'pdo_'
.
$this
->
driverName
;
if
(
!
extension_loaded
(
'pdo'
)
||
!
extension_loaded
(
$pdo_database
))
{
$this
->
markTestSkipped
(
'pdo and pdo_'
.
$pdo_database
.
' extension are required.'
);
$this
->
markTestSkipped
(
'pdo and '
.
$pdo_database
.
' extension are required.'
);
}
if
(
$this
->
initializeAppWithDb
===
true
)
{
\Yii
::
$app
->
setComponent
(
'db'
,
$this
->
getConnection
());
}
}
...
...
tests/unit/framework/validators/ExistValidatorDriverTests/ExistValidatorPostgresTest.php
0 → 100644
View file @
5c8927af
<?php
namespace
yiiunit\framework\validators\ExistValidatorDriverTests
;
use
yiiunit\framework\validators\ExistValidatorTest
;
class
ExistValidatorPostgresTest
extends
ExistValidatorTest
{
protected
$driverName
=
'pgsql'
;
}
\ No newline at end of file
tests/unit/framework/validators/ExistValidatorDriverTests/ExistValidatorSQliteTest.php
0 → 100644
View file @
5c8927af
<?php
namespace
yiiunit\framework\validators\ExistValidatorDriverTests
;
use
yiiunit\framework\validators\ExistValidatorTest
;
class
ExistValidatorSQliteTest
extends
ExistValidatorTest
{
protected
$driverName
=
'sqlite'
;
}
\ No newline at end of file
tests/unit/framework/validators/ExistValidatorTest.php
0 → 100644
View file @
5c8927af
<?php
namespace
yiiunit\framework\validators
;
use
Yii
;
use
yii\base\Exception
;
use
yii\validators\ExistValidator
;
use
yiiunit\data\ar\ActiveRecord
;
use
yiiunit\data\validators\models\ExistValidatorMainModel
;
use
yiiunit\data\validators\models\ExistValidatorRefModel
;
use
yiiunit\framework\db\DatabaseTestCase
;
class
ExistValidatorTest
extends
DatabaseTestCase
{
protected
$initializeAppWithDb
=
true
;
protected
$driverName
=
'mysql'
;
public
function
setUp
()
{
parent
::
setUp
();
ActiveRecord
::
$db
=
Yii
::
$app
->
getComponent
(
'db'
);
}
public
function
tearDown
()
{
parent
::
tearDown
();
}
public
function
testValidateValueExpectedException
()
{
try
{
$val
=
new
ExistValidator
();
$result
=
$val
->
validateValue
(
'ref'
);
$this
->
fail
(
'Exception should have been thrown at this time'
);
}
catch
(
Exception
$e
)
{
$this
->
assertInstanceOf
(
'yii\base\InvalidConfigException'
,
$e
);
$this
->
assertEquals
(
'The "className" property must be set.'
,
$e
->
getMessage
());
}
// combine to save the time creating a new db-fixture set (likely ~5 sec)
try
{
$val
=
new
ExistValidator
(
array
(
'className'
=>
ExistValidatorMainModel
::
className
()));
$val
->
validateValue
(
'ref'
);
$this
->
fail
(
'Exception should have been thrown at this time'
);
}
catch
(
Exception
$e
)
{
$this
->
assertInstanceOf
(
'yii\base\InvalidConfigException'
,
$e
);
$this
->
assertEquals
(
'The "attributeName" property must be set.'
,
$e
->
getMessage
());
}
}
public
function
testValidateValue
()
{
$val
=
new
ExistValidator
(
array
(
'className'
=>
ExistValidatorRefModel
::
className
(),
'attributeName'
=>
'id'
));
$this
->
assertTrue
(
$val
->
validateValue
(
2
));
$this
->
assertTrue
(
$val
->
validateValue
(
5
));
$this
->
assertFalse
(
$val
->
validateValue
(
99
));
$this
->
assertFalse
(
$val
->
validateValue
(
array
(
'1'
)));
}
public
function
testValidateAttribute
()
{
// existing value on different table
$val
=
new
ExistValidator
(
array
(
'className'
=>
ExistValidatorMainModel
::
className
(),
'attributeName'
=>
'id'
));
$m
=
ExistValidatorRefModel
::
find
(
array
(
'id'
=>
1
));
$val
->
validateAttribute
(
$m
,
'ref'
);
$this
->
assertFalse
(
$m
->
hasErrors
());
// non-existing value on different table
$val
=
new
ExistValidator
(
array
(
'className'
=>
ExistValidatorMainModel
::
className
(),
'attributeName'
=>
'id'
));
$m
=
ExistValidatorRefModel
::
find
(
array
(
'id'
=>
6
));
$val
->
validateAttribute
(
$m
,
'ref'
);
$this
->
assertTrue
(
$m
->
hasErrors
(
'ref'
));
// existing value on same table
$val
=
new
ExistValidator
(
array
(
'attributeName'
=>
'ref'
));
$m
=
ExistValidatorRefModel
::
find
(
array
(
'id'
=>
2
));
$val
->
validateAttribute
(
$m
,
'test_val'
);
$this
->
assertFalse
(
$m
->
hasErrors
());
// non-existing value on same table
$val
=
new
ExistValidator
(
array
(
'attributeName'
=>
'ref'
));
$m
=
ExistValidatorRefModel
::
find
(
array
(
'id'
=>
5
));
$val
->
validateAttribute
(
$m
,
'test_val_fail'
);
$this
->
assertTrue
(
$m
->
hasErrors
(
'test_val_fail'
));
// check for given value (true)
$val
=
new
ExistValidator
();
$m
=
ExistValidatorRefModel
::
find
(
array
(
'id'
=>
3
));
$val
->
validateAttribute
(
$m
,
'ref'
);
$this
->
assertFalse
(
$m
->
hasErrors
());
// check for given defaults (false)
$val
=
new
ExistValidator
();
$m
=
ExistValidatorRefModel
::
find
(
array
(
'id'
=>
4
));
$m
->
a_field
=
'some new value'
;
$val
->
validateAttribute
(
$m
,
'a_field'
);
$this
->
assertTrue
(
$m
->
hasErrors
(
'a_field'
));
}
}
\ 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