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
376dd0d4
Commit
376dd0d4
authored
Jan 30, 2012
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
..
parent
1978a4ef
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
250 additions
and
11 deletions
+250
-11
ActiveQuery.php
framework/db/ar/ActiveQuery.php
+15
-7
MysqlTestCase.php
tests/unit/MysqlTestCase.php
+4
-1
ActiveRecord.php
tests/unit/data/ar/ActiveRecord.php
+29
-0
Customer.php
tests/unit/data/ar/Customer.php
+12
-0
Order.php
tests/unit/data/ar/Order.php
+12
-0
ActiveRecordTest.php
tests/unit/framework/db/ar/ActiveRecordTest.php
+174
-0
ConnectionTest.php
tests/unit/framework/db/dao/ConnectionTest.php
+2
-1
QueryTest.php
tests/unit/framework/db/dao/QueryTest.php
+2
-2
No files found.
framework/db/ar/ActiveQuery.php
View file @
376dd0d4
...
@@ -683,16 +683,24 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
...
@@ -683,16 +683,24 @@ class ActiveQuery extends \yii\base\Object implements \IteratorAggregate, \Array
* public $with;
* public $with;
*/
*/
if
(
$this
->
query
->
from
===
null
)
{
if
(
$this
->
sql
===
null
)
{
$modelClass
=
$this
->
modelClass
;
if
(
$this
->
query
->
from
===
null
)
{
$this
->
query
->
from
=
$modelClass
::
tableName
();
$modelClass
=
$this
->
modelClass
;
if
(
$this
->
tableAlias
!==
null
)
{
$tableName
=
$modelClass
::
tableName
();
$this
->
query
->
from
.=
$this
->
tableAlias
;
if
(
$this
->
tableAlias
!==
null
)
{
$tableName
.=
' '
.
$this
->
tableAlias
;
}
$this
->
query
->
from
=
array
(
$tableName
);
}
}
$command
=
$this
->
query
->
createCommand
(
$this
->
getDbConnection
());
$this
->
sql
=
$command
->
getSql
();
}
else
{
$command
=
$this
->
getDbConnection
()
->
createCommand
(
$this
->
sql
);
$command
->
bindValues
(
$this
->
query
->
params
);
}
}
$command
=
$this
->
query
->
createCommand
(
$this
->
getDbConnection
());
$this
->
sql
=
$command
->
getSql
();
$rows
=
$command
->
queryAll
();
$rows
=
$command
->
queryAll
();
if
(
$this
->
asArray
)
{
if
(
$this
->
asArray
)
{
if
(
$this
->
indexBy
===
null
)
{
if
(
$this
->
indexBy
===
null
)
{
return
$rows
;
return
$rows
;
...
...
tests/unit/MysqlTestCase.php
View file @
376dd0d4
...
@@ -18,7 +18,10 @@ class MysqlTestCase extends TestCase
...
@@ -18,7 +18,10 @@ class MysqlTestCase extends TestCase
function
getConnection
(
$reset
=
true
)
function
getConnection
(
$reset
=
true
)
{
{
$params
=
$this
->
getParam
(
'mysql'
);
$params
=
$this
->
getParam
(
'mysql'
);
$db
=
new
\yii\db\dao\Connection
(
$params
[
'dsn'
],
$params
[
'username'
],
$params
[
'password'
]);
$db
=
new
\yii\db\dao\Connection
;
$db
->
dsn
=
$params
[
'dsn'
];
$db
->
username
=
$params
[
'username'
];
$db
->
password
=
$params
[
'password'
];
if
(
$reset
)
{
if
(
$reset
)
{
$db
->
active
=
true
;
$db
->
active
=
true
;
$lines
=
explode
(
';'
,
file_get_contents
(
$params
[
'fixture'
]));
$lines
=
explode
(
';'
,
file_get_contents
(
$params
[
'fixture'
]));
...
...
tests/unit/data/ar/ActiveRecord.php
0 → 100644
View file @
376dd0d4
<?php
/**
* ActiveRecord class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yiiunit\data\ar
;
use
yii\db\dao\Connection
;
/**
* ActiveRecord is ...
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
ActiveRecord
extends
\yii\db\ar\ActiveRecord
{
public
static
$db
;
public
static
function
getDbConnection
()
{
return
self
::
$db
;
}
}
\ No newline at end of file
tests/unit/data/ar/Customer.php
0 → 100644
View file @
376dd0d4
<?php
namespace
yiiunit\data\ar
;
class
Customer
extends
ActiveRecord
{
public
static
function
tableName
()
{
return
'tbl_customer'
;
}
}
\ No newline at end of file
tests/unit/data/ar/Order.php
0 → 100644
View file @
376dd0d4
<?php
namespace
yiiunit\data\ar
;
class
Order
extends
ActiveRecord
{
public
static
function
tableName
()
{
return
'tbl_order'
;
}
}
\ No newline at end of file
tests/unit/framework/db/ar/ActiveRecordTest.php
0 → 100644
View file @
376dd0d4
<?php
namespace
yiiunit\framework\db\ar
;
use
yii\db\dao\Query
;
use
yii\db\ar\ActiveQuery
;
use
yiiunit\data\ar\ActiveRecord
;
use
yiiunit\data\ar\Customer
;
class
ActiveRecordTest
extends
\yiiunit\MysqlTestCase
{
public
function
setUp
()
{
ActiveRecord
::
$db
=
$this
->
getConnection
();
}
public
function
testFind
()
{
// find one
$result
=
Customer
::
find
();
$this
->
assertTrue
(
$result
instanceof
ActiveQuery
);
$customer
=
$result
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
1
,
$result
->
count
);
// find all
$result
=
Customer
::
find
();
$customers
=
$result
->
all
();
$this
->
assertTrue
(
is_array
(
$customers
));
$this
->
assertEquals
(
3
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
0
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
1
]
instanceof
Customer
);
$this
->
assertTrue
(
$customers
[
2
]
instanceof
Customer
);
$this
->
assertEquals
(
3
,
$result
->
count
);
// check count first
$result
=
Customer
::
find
();
$this
->
assertEquals
(
3
,
$result
->
count
);
$customer
=
$result
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
1
,
$result
->
count
);
// iterator
$result
=
Customer
::
find
();
$count
=
0
;
foreach
(
$result
as
$customer
)
{
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$count
++
;
}
$this
->
assertEquals
(
$count
,
$result
->
count
);
// array access
$result
=
Customer
::
find
();
$this
->
assertTrue
(
$result
[
0
]
instanceof
Customer
);
$this
->
assertTrue
(
$result
[
1
]
instanceof
Customer
);
$this
->
assertTrue
(
$result
[
2
]
instanceof
Customer
);
// find by a single primary key
$customer
=
Customer
::
find
(
2
)
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
// find by attributes
$customer
=
Customer
::
find
(
array
(
'name'
=>
'user2'
))
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
2
,
$customer
->
id
);
// find by Query
$query
=
new
ActiveQuery
;
$query
->
where
(
'id=:id'
,
array
(
':id'
=>
2
));
$customer
=
Customer
::
find
(
$query
)
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
}
public
function
testFindBySql
()
{
// find one
$customer
=
Customer
::
findBySql
(
'SELECT * FROM tbl_customer ORDER BY id DESC'
)
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
// find all
$customers
=
Customer
::
findBySql
(
'SELECT * FROM tbl_customer'
)
->
all
();
$this
->
assertEquals
(
3
,
count
(
$customers
));
// find with parameter binding
$customer
=
Customer
::
findBySql
(
'SELECT * FROM tbl_customer WHERE id=:id'
,
array
(
':id'
=>
2
))
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
// count
$finder
=
Customer
::
findBySql
(
'SELECT * FROM tbl_customer ORDER BY id DESC'
);
$finder
->
one
();
$this
->
assertEquals
(
1
,
$finder
->
count
);
$finder
=
Customer
::
findBySql
(
'SELECT * FROM tbl_customer ORDER BY id DESC'
);
// todo
// $this->assertEquals(3, $finder->count);
}
public
function
testQueryMethods
()
{
$customer
=
Customer
::
find
()
->
where
(
'id=?'
,
2
)
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user2'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
where
(
array
(
'name'
=>
'user3'
))
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user3'
,
$customer
->
name
);
$customer
=
Customer
::
find
()
->
select
(
'id'
)
->
orderBy
(
'id DESC'
)
->
one
();
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
3
,
$customer
->
id
);
$this
->
assertEquals
(
null
,
$customer
->
name
);
}
public
function
testGetSql
()
{
// sql for all
$sql
=
Customer
::
find
()
->
sql
;
$this
->
assertEquals
(
'SELECT * FROM tbl_customer'
,
$sql
);
// sql for one row
$sql
=
Customer
::
find
()
->
oneSql
;
$this
->
assertEquals
(
'SELECT * FROM tbl_customer LIMIT 1'
,
$sql
);
// sql for count
$sql
=
Customer
::
find
()
->
countSql
;
$this
->
assertEquals
(
'SELECT COUNT(*) FROM tbl_customer'
,
$sql
);
}
public
function
testArrayResult
()
{
Customer
::
find
()
->
asArray
()
->
one
();
Customer
::
find
()
->
asArray
()
->
all
();
}
public
function
testMisc
()
{
/*
* Customer::exists()
* Customer::updateAll()
* Customer::updateCounters()
* Customer::deleteAll()
*/
}
public
function
testInsert
()
{
}
public
function
testUpdate
()
{
}
public
function
testDelete
()
{
}
public
function
testLazyLoading
()
{
}
public
function
testEagerLoading
()
{
}
}
\ No newline at end of file
tests/unit/framework/db/dao/ConnectionTest.php
View file @
376dd0d4
...
@@ -31,7 +31,8 @@ class ConnectionTest extends \yiiunit\MysqlTestCase
...
@@ -31,7 +31,8 @@ class ConnectionTest extends \yiiunit\MysqlTestCase
$this
->
assertFalse
(
$connection
->
active
);
$this
->
assertFalse
(
$connection
->
active
);
$this
->
assertEquals
(
null
,
$connection
->
pdo
);
$this
->
assertEquals
(
null
,
$connection
->
pdo
);
$connection
=
new
Connection
(
'unknown::memory:'
);
$connection
=
new
Connection
;
$connection
->
dsn
=
'unknown::memory:'
;
$this
->
setExpectedException
(
'yii\db\Exception'
);
$this
->
setExpectedException
(
'yii\db\Exception'
);
$connection
->
open
();
$connection
->
open
();
}
}
...
...
tests/unit/framework/db/dao/QueryTest.php
View file @
376dd0d4
...
@@ -15,11 +15,11 @@ class QueryTest extends \yiiunit\MysqlTestCase
...
@@ -15,11 +15,11 @@ class QueryTest extends \yiiunit\MysqlTestCase
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
select
();
$query
->
select
();
$this
->
assertEquals
(
'*'
,
$query
->
select
);
$this
->
assertEquals
(
'*'
,
$query
->
select
);
$this
->
assert
False
(
$query
->
distinct
);
$this
->
assert
Null
(
$query
->
distinct
);
$this
->
assertEquals
(
null
,
$query
->
selectOption
);
$this
->
assertEquals
(
null
,
$query
->
selectOption
);
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
select
(
'id, name'
,
true
,
'something'
);
$query
->
select
(
'id, name'
,
'something'
)
->
distinct
(
true
);
$this
->
assertEquals
(
'id, name'
,
$query
->
select
);
$this
->
assertEquals
(
'id, name'
,
$query
->
select
);
$this
->
assertTrue
(
$query
->
distinct
);
$this
->
assertTrue
(
$query
->
distinct
);
$this
->
assertEquals
(
'something'
,
$query
->
selectOption
);
$this
->
assertEquals
(
'something'
,
$query
->
selectOption
);
...
...
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