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
62968971
Commit
62968971
authored
Nov 16, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sphinx unit test "ActiveRelationTest" created.
parent
fa086742
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
170 additions
and
24 deletions
+170
-24
config.php
tests/unit/data/config.php
+13
-0
ArticleDb.php
tests/unit/data/sphinx/ar/ArticleDb.php
+14
-0
ArticleIndex.php
tests/unit/data/sphinx/ar/ArticleIndex.php
+14
-0
ItemDb.php
tests/unit/data/sphinx/ar/ItemDb.php
+14
-0
source.sql
tests/unit/data/sphinx/source.sql
+15
-10
sphinx.conf
tests/unit/data/sphinx/sphinx.conf
+1
-1
ActiveRelationTest.php
tests/unit/extensions/sphinx/ActiveRelationTest.php
+43
-0
SphinxTestCase.php
tests/unit/extensions/sphinx/SphinxTestCase.php
+56
-13
No files found.
tests/unit/data/config.php
View file @
62968971
...
...
@@ -30,4 +30,17 @@ return [
'fixture'
=>
__DIR__
.
'/postgres.sql'
,
],
],
'sphinx'
=>
[
'sphinx'
=>
[
'dsn'
=>
'mysql:host=127.0.0.1;port=9306;'
,
'username'
=>
''
,
'password'
=>
''
,
],
'db'
=>
[
'dsn'
=>
'mysql:host=127.0.0.1;dbname=yiitest'
,
'username'
=>
'travis'
,
'password'
=>
''
,
'fixture'
=>
__DIR__
.
'/sphinx/source.sql'
,
],
]
];
tests/unit/data/sphinx/ar/ArticleDb.php
0 → 100644
View file @
62968971
<?php
namespace
yiiunit\data\sphinx\ar
;
use
yiiunit\data\ar
;
class
ArticleDb
extends
ActiveRecord
{
public
static
function
tableName
()
{
return
'yii2_test_article'
;
}
}
\ No newline at end of file
tests/unit/data/sphinx/ar/ArticleIndex.php
View file @
62968971
...
...
@@ -2,6 +2,8 @@
namespace
yiiunit\data\sphinx\ar
;
use
yii\db\ActiveRelation
;
class
ArticleIndex
extends
ActiveRecord
{
public
$custom_column
;
...
...
@@ -15,4 +17,15 @@ class ArticleIndex extends ActiveRecord
{
$query
->
andWhere
(
'author_id=1'
);
}
public
function
getSource
()
{
$config
=
[
'modelClass'
=>
ArticleDb
::
className
(),
'primaryModel'
=>
$this
,
'link'
=>
[
'id'
=>
'id'
],
'multiple'
=>
false
,
];
return
new
ActiveRelation
(
$config
);
}
}
\ No newline at end of file
tests/unit/data/sphinx/ar/ItemDb.php
0 → 100644
View file @
62968971
<?php
namespace
yiiunit\data\sphinx\ar
;
use
yiiunit\data\ar
;
class
ItemDb
extends
ActiveRecord
{
public
static
function
tableName
()
{
return
'yii2_test_item'
;
}
}
\ No newline at end of file
tests/unit/data/sphinx/s
phinx
.sql
→
tests/unit/data/sphinx/s
ource
.sql
View file @
62968971
SET
SQL_MODE
=
"NO_AUTO_VALUE_ON_ZERO"
;
SET
time_zone
=
"+00:00"
;
/**
* This is the MySQL database schema for creation of the test Sphinx index sources.
*/
DROP
TABLE
IF
EXISTS
yii2_test_article
;
DROP
TABLE
IF
EXISTS
yii2_test_item
;
DROP
TABLE
IF
EXISTS
yii2_test_article_tag
;
CREATE
TABLE
IF
NOT
EXISTS
`yii2_test_article`
(
`id`
int
(
11
)
NOT
NULL
AUTO_INCREMENT
,
...
...
@@ -10,10 +15,6 @@ CREATE TABLE IF NOT EXISTS `yii2_test_article` (
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
MyISAM
DEFAULT
CHARSET
=
utf8
AUTO_INCREMENT
=
3
;
INSERT
INTO
`yii2_test_article`
(
`id`
,
`title`
,
`content`
,
`author_id`
,
`create_date`
)
VALUES
(
1
,
'About cats'
,
'This article is about cats'
,
1
,
'2013-10-23 00:00:00'
),
(
2
,
'About dogs'
,
'This article is about dogs'
,
2
,
'2013-11-15 00:00:00'
);
CREATE
TABLE
IF
NOT
EXISTS
`yii2_test_item`
(
`id`
int
(
11
)
NOT
NULL
AUTO_INCREMENT
,
`name`
varchar
(
255
)
NOT
NULL
,
...
...
@@ -23,16 +24,20 @@ CREATE TABLE IF NOT EXISTS `yii2_test_item` (
PRIMARY
KEY
(
`id`
)
)
ENGINE
=
MyISAM
DEFAULT
CHARSET
=
utf8
AUTO_INCREMENT
=
3
;
INSERT
INTO
`yii2_test_item`
(
`id`
,
`name`
,
`description`
,
`category_id`
,
`price`
)
VALUES
(
1
,
'pencil'
,
'Simple pencil'
,
1
,
2
.
5
),
(
2
,
'table'
,
'Wooden table'
,
2
,
100
);
CREATE
TABLE
IF
NOT
EXISTS
`yii2_test_article_tag`
(
`article_id`
int
(
11
)
NOT
NULL
,
`tag_id`
int
(
11
)
NOT
NULL
,
PRIMARY
KEY
(
`article_id`
,
`tag_id`
)
)
ENGINE
=
MyISAM
DEFAULT
CHARSET
=
utf8
;
INSERT
INTO
`yii2_test_article`
(
`id`
,
`title`
,
`content`
,
`author_id`
,
`create_date`
)
VALUES
(
1
,
'About cats'
,
'This article is about cats'
,
1
,
'2013-10-23 00:00:00'
),
(
2
,
'About dogs'
,
'This article is about dogs'
,
2
,
'2013-11-15 00:00:00'
);
INSERT
INTO
`yii2_test_item`
(
`id`
,
`name`
,
`description`
,
`category_id`
,
`price`
)
VALUES
(
1
,
'pencil'
,
'Simple pencil'
,
1
,
2
.
5
),
(
2
,
'table'
,
'Wooden table'
,
2
,
100
);
INSERT
INTO
`yii2_test_article_tag`
(
`article_id`
,
`tag_id`
)
VALUES
(
1
,
1
),
(
1
,
2
),
...
...
tests/unit/data/sphinx/sphinx.conf
View file @
62968971
...
...
@@ -2,7 +2,7 @@
#
# Setup test environment:
# - initialize test database source:
# mysql -D yii2test -u test < /path/to/yii/tests/unit/data/sphinx/s
phinx
.sql
# mysql -D yii2test -u test < /path/to/yii/tests/unit/data/sphinx/s
ource
.sql
# - setup test Sphinx indexes:
# indexer --config /path/to/yii/tests/unit/data/sphinx/sphinx.conf --all [--rotate]
# - run the "searchd" daemon:
...
...
tests/unit/extensions/sphinx/ActiveRelationTest.php
0 → 100644
View file @
62968971
<?php
namespace
yiiunit\extensions\sphinx
;
use
yiiunit\data\sphinx\ar\ActiveRecord
;
use
yiiunit\data\ar\ActiveRecord
as
ActiveRecordDb
;
use
yiiunit\data\sphinx\ar\ArticleIndex
;
use
yiiunit\data\sphinx\ar\ArticleDb
;
/**
* @group sphinx
*/
class
ActiveRelationTest
extends
SphinxTestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
ActiveRecord
::
$db
=
$this
->
getConnection
();
ActiveRecordDb
::
$db
=
$this
->
getDbConnection
();
}
public
function
testFindLazy
()
{
/** @var ArticleIndex $article */
$article
=
ArticleIndex
::
find
([
'id'
=>
2
]);
$this
->
assertFalse
(
$article
->
isRelationPopulated
(
'source'
));
$source
=
$article
->
source
;
$this
->
assertTrue
(
$article
->
isRelationPopulated
(
'source'
));
$this
->
assertTrue
(
$source
instanceof
ArticleDb
);
$this
->
assertEquals
(
1
,
count
(
$article
->
populatedRelations
));
}
public
function
testFindEager
()
{
$articles
=
ArticleIndex
::
find
()
->
with
(
'source'
)
->
all
();
$this
->
assertEquals
(
2
,
count
(
$articles
));
$this
->
assertTrue
(
$articles
[
0
]
->
isRelationPopulated
(
'source'
));
$this
->
assertTrue
(
$articles
[
1
]
->
isRelationPopulated
(
'source'
));
$this
->
assertTrue
(
$articles
[
0
]
->
source
instanceof
ArticleDb
);
$this
->
assertTrue
(
$articles
[
1
]
->
source
instanceof
ArticleDb
);
}
}
\ No newline at end of file
tests/unit/extensions/sphinx/SphinxTestCase.php
View file @
62968971
...
...
@@ -21,9 +21,21 @@ class SphinxTestCase extends TestCase
'password'
=>
''
,
];
/**
* @var Connection
* @var Connection
Sphinx connection instance.
*/
protected
$sphinx
;
/**
* @var array Database connection configuration.
*/
protected
$dbConfig
=
[
'dsn'
=>
'mysql:host=127.0.0.1;'
,
'username'
=>
''
,
'password'
=>
''
,
];
/**
* @var \yii\db\Connection database connection instance.
*/
protected
$db
;
public
static
function
setUpBeforeClass
()
{
...
...
@@ -33,10 +45,14 @@ class SphinxTestCase extends TestCase
protected
function
setUp
()
{
parent
::
setUp
();
//$this->sphinxConfig = $this->getParam('sphinx');
if
(
!
extension_loaded
(
'pdo'
)
||
!
extension_loaded
(
'pdo_mysql'
))
{
$this
->
markTestSkipped
(
'pdo and pdo_mysql extension are required.'
);
}
$config
=
$this
->
getParam
(
'sphinx'
);
if
(
!
empty
(
$config
))
{
$this
->
sphinxConfig
=
$config
[
'sphinx'
];
$this
->
dbConfig
=
$config
[
'db'
];
}
$this
->
mockApplication
();
static
::
loadClassMap
();
}
...
...
@@ -67,15 +83,15 @@ class SphinxTestCase extends TestCase
/**
* @param bool $reset whether to clean up the test database
* @param bool $open whether to open
and populate
test database
* @param bool $open whether to open test database
* @return \yii\sphinx\Connection
*/
public
function
getConnection
(
$reset
=
tru
e
,
$open
=
true
)
public
function
getConnection
(
$reset
=
fals
e
,
$open
=
true
)
{
if
(
!
$reset
&&
$this
->
sphinx
)
{
return
$this
->
sphinx
;
}
$db
=
new
\yii\sphinx\
Connection
;
$db
=
new
Connection
;
$db
->
dsn
=
$this
->
sphinxConfig
[
'dsn'
];
if
(
isset
(
$this
->
sphinxConfig
[
'username'
]))
{
$db
->
username
=
$this
->
sphinxConfig
[
'username'
];
...
...
@@ -86,14 +102,6 @@ class SphinxTestCase extends TestCase
}
if
(
$open
)
{
$db
->
open
();
if
(
!
empty
(
$this
->
sphinxConfig
[
'fixture'
]))
{
$lines
=
explode
(
';'
,
file_get_contents
(
$this
->
sphinxConfig
[
'fixture'
]));
foreach
(
$lines
as
$line
)
{
if
(
trim
(
$line
)
!==
''
)
{
$db
->
pdo
->
exec
(
$line
);
}
}
}
}
$this
->
sphinx
=
$db
;
return
$db
;
...
...
@@ -109,4 +117,38 @@ class SphinxTestCase extends TestCase
$this
->
sphinx
->
createCommand
(
'TRUNCATE RTINDEX '
.
$indexName
)
->
execute
();
}
}
/**
* @param bool $reset whether to clean up the test database
* @param bool $open whether to open and populate test database
* @return \yii\db\Connection
*/
public
function
getDbConnection
(
$reset
=
true
,
$open
=
true
)
{
if
(
!
$reset
&&
$this
->
db
)
{
return
$this
->
db
;
}
$db
=
new
\yii\db\Connection
;
$db
->
dsn
=
$this
->
dbConfig
[
'dsn'
];
if
(
isset
(
$this
->
dbConfig
[
'username'
]))
{
$db
->
username
=
$this
->
dbConfig
[
'username'
];
$db
->
password
=
$this
->
dbConfig
[
'password'
];
}
if
(
isset
(
$this
->
dbConfig
[
'attributes'
]))
{
$db
->
attributes
=
$this
->
dbConfig
[
'attributes'
];
}
if
(
$open
)
{
$db
->
open
();
if
(
!
empty
(
$this
->
dbConfig
[
'fixture'
]))
{
$lines
=
explode
(
';'
,
file_get_contents
(
$this
->
dbConfig
[
'fixture'
]));
foreach
(
$lines
as
$line
)
{
if
(
trim
(
$line
)
!==
''
)
{
$db
->
pdo
->
exec
(
$line
);
}
}
}
}
$this
->
db
=
$db
;
return
$db
;
}
}
\ 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