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
a082fd91
Commit
a082fd91
authored
Jul 12, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added test.
parent
65987f62
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
2 deletions
+90
-2
DatabaseTestCase.php
tests/unit/framework/db/DatabaseTestCase.php
+5
-2
SqliteConnectionTest.php
tests/unit/framework/db/sqlite/SqliteConnectionTest.php
+85
-0
No files found.
tests/unit/framework/db/DatabaseTestCase.php
View file @
a082fd91
...
...
@@ -51,16 +51,19 @@ abstract class DatabaseTestCase extends TestCase
}
else
{
$fixture
=
null
;
}
return
$this
->
db
=
$this
->
prepareDatabase
(
$config
,
$fixture
);
return
$this
->
db
=
$this
->
prepareDatabase
(
$config
,
$fixture
,
$open
);
}
public
function
prepareDatabase
(
$config
,
$fixture
)
public
function
prepareDatabase
(
$config
,
$fixture
,
$open
=
true
)
{
if
(
!
isset
(
$config
[
'class'
]))
{
$config
[
'class'
]
=
'yii\db\Connection'
;
}
/* @var $db \yii\db\Connection */
$db
=
\Yii
::
createObject
(
$config
);
if
(
!
$open
)
{
return
$db
;
}
$db
->
open
();
if
(
$fixture
!==
null
)
{
$lines
=
explode
(
';'
,
file_get_contents
(
$fixture
));
...
...
tests/unit/framework/db/sqlite/SqliteConnectionTest.php
View file @
a082fd91
<?php
namespace
yiiunit\framework\db\sqlite
;
use
yii\db\Connection
;
use
yii\db\Transaction
;
use
yiiunit\framework\db\ConnectionTest
;
use
yiiunit\data\ar\ActiveRecord
;
use
yiiunit\data\ar\Customer
;
/**
* @group db
...
...
@@ -57,4 +60,86 @@ class SqliteConnectionTest extends ConnectionTest
$transaction
=
$connection
->
beginTransaction
(
Transaction
::
SERIALIZABLE
);
$transaction
->
rollBack
();
}
public
function
testMasterSlave
()
{
$counts
=
[[
0
,
2
],
[
1
,
2
],
[
2
,
2
]];
foreach
(
$counts
as
$count
)
{
list
(
$masterCount
,
$slaveCount
)
=
$count
;
$db
=
$this
->
prepareMasterSlave
(
$masterCount
,
$slaveCount
);
$this
->
assertTrue
(
$db
->
getSlave
()
instanceof
Connection
);
$this
->
assertTrue
(
$db
->
getSlave
()
->
isActive
);
$this
->
assertFalse
(
$db
->
isActive
);
// test SELECT uses slave
$this
->
assertEquals
(
2
,
$db
->
createCommand
(
'SELECT COUNT(*) FROM profile'
)
->
queryScalar
());
$this
->
assertFalse
(
$db
->
isActive
);
// test UPDATE uses master
$db
->
createCommand
(
"UPDATE profile SET description='test' WHERE id=1"
)
->
execute
();
$this
->
assertTrue
(
$db
->
isActive
);
$this
->
assertNotEquals
(
'test'
,
$db
->
createCommand
(
"SELECT description FROM profile WHERE id=1"
)
->
queryScalar
());
$result
=
$db
->
useMaster
(
function
(
Connection
$db
)
{
return
$db
->
createCommand
(
"SELECT description FROM profile WHERE id=1"
)
->
queryScalar
();
});
$this
->
assertEquals
(
'test'
,
$result
);
// test ActiveRecord read/write split
ActiveRecord
::
$db
=
$db
=
$this
->
prepareMasterSlave
(
$masterCount
,
$slaveCount
);
$this
->
assertFalse
(
$db
->
isActive
);
$customer
=
Customer
::
findOne
(
1
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$this
->
assertFalse
(
$db
->
isActive
);
$customer
->
name
=
'test'
;
$customer
->
save
();
$this
->
assertTrue
(
$db
->
isActive
);
$customer
=
Customer
::
findOne
(
1
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
(
'user1'
,
$customer
->
name
);
$result
=
$db
->
useMaster
(
function
()
{
return
Customer
::
findOne
(
1
)
->
name
;
});
$this
->
assertEquals
(
'test'
,
$result
);
}
}
/**
* @param integer $masterCount
* @param integer $slaveCount
* @return Connection
*/
protected
function
prepareMasterSlave
(
$masterCount
,
$slaveCount
)
{
$databases
=
self
::
getParam
(
'databases'
);
$fixture
=
$databases
[
$this
->
driverName
][
'fixture'
];
$basePath
=
\Yii
::
getAlias
(
'@yiiunit/runtime'
);
$config
=
[
'class'
=>
'yii\db\Connection'
,
'dsn'
=>
'sqlite:memory:'
,
];
$this
->
prepareDatabase
(
$config
,
$fixture
)
->
close
();
for
(
$i
=
0
;
$i
<
$masterCount
;
++
$i
)
{
$master
=
[
'dsn'
=>
"sqlite:
$basePath
/yii2test_master
{
$i
}
.sq3"
];
$db
=
$this
->
prepareDatabase
(
$master
,
$fixture
);
$db
->
close
();
$config
[
'masters'
][]
=
$master
;
}
for
(
$i
=
0
;
$i
<
$slaveCount
;
++
$i
)
{
$slave
=
[
'dsn'
=>
"sqlite:
$basePath
/yii2test_slave
{
$i
}
.sq3"
];
$db
=
$this
->
prepareDatabase
(
$slave
,
$fixture
);
$db
->
close
();
$config
[
'slaves'
][]
=
$slave
;
}
return
\Yii
::
createObject
(
$config
);
}
}
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