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
0f94ed39
Commit
0f94ed39
authored
Nov 11, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
"Sphinx" unit tests updated to expose runtime indexes.
parent
fe3f7f2d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
169 additions
and
3 deletions
+169
-3
Schema.php
extensions/sphinx/Schema.php
+89
-0
sphinx.conf
tests/unit/data/sphinx/sphinx.conf
+12
-2
CommandTest.php
tests/unit/extensions/sphinx/CommandTest.php
+55
-0
SphinxTestCase.php
tests/unit/extensions/sphinx/SphinxTestCase.php
+13
-1
No files found.
extensions/sphinx/Schema.php
View file @
0f94ed39
...
...
@@ -7,6 +7,9 @@
namespace
yii\sphinx
;
use
yii\db\ColumnSchema
;
use
yii\db\TableSchema
;
/**
* Class Schema
*
...
...
@@ -24,4 +27,89 @@ class Schema extends \yii\db\mysql\Schema
{
return
new
QueryBuilder
(
$this
->
db
);
}
/**
* Loads the metadata for the specified table.
* @param string $name table name
* @return TableSchema driver dependent table metadata. Null if the table does not exist.
*/
protected
function
loadTableSchema
(
$name
)
{
$table
=
new
TableSchema
;
$this
->
resolveTableNames
(
$table
,
$name
);
if
(
$this
->
findColumns
(
$table
))
{
return
$table
;
}
else
{
return
null
;
}
}
/**
* Collects the metadata of table columns.
* @param TableSchema $table the table metadata
* @return boolean whether the table exists in the database
* @throws \Exception if DB query fails
*/
protected
function
findColumns
(
$table
)
{
$sql
=
'DESCRIBE '
.
$this
->
quoteSimpleTableName
(
$table
->
name
);
try
{
$columns
=
$this
->
db
->
createCommand
(
$sql
)
->
queryAll
();
}
catch
(
\Exception
$e
)
{
$previous
=
$e
->
getPrevious
();
if
(
$previous
instanceof
\PDOException
&&
$previous
->
getCode
()
==
'42S02'
)
{
// index does not exist
return
false
;
}
throw
$e
;
}
foreach
(
$columns
as
$info
)
{
$column
=
$this
->
loadColumnSchema
(
$info
);
$table
->
columns
[
$column
->
name
]
=
$column
;
if
(
$column
->
isPrimaryKey
)
{
$table
->
primaryKey
[]
=
$column
->
name
;
if
(
$column
->
autoIncrement
)
{
$table
->
sequenceName
=
''
;
}
}
}
return
true
;
}
/**
* Loads the column information into a [[ColumnSchema]] object.
* @param array $info column information
* @return ColumnSchema the column schema object
*/
protected
function
loadColumnSchema
(
$info
)
{
$column
=
new
ColumnSchema
;
$column
->
name
=
$info
[
'Field'
];
// Not supported :
//$column->allowNull = $info['Null'] === 'YES';
//$column->isPrimaryKey = strpos($info['Key'], 'PRI') !== false;
//$column->autoIncrement = stripos($info['Extra'], 'auto_increment') !== false;
//$column->comment = $info['Comment'];
$column
->
dbType
=
$info
[
'Type'
];
//$column->unsigned = strpos($column->dbType, 'unsigned') !== false;
$type
=
$info
[
'Type'
];
if
(
isset
(
$this
->
typeMap
[
$type
]))
{
$column
->
type
=
$this
->
typeMap
[
$type
];
}
else
{
$column
->
type
=
self
::
TYPE_STRING
;
}
$column
->
phpType
=
$this
->
getColumnPhpType
(
$column
);
/*if ($column->type !== 'timestamp' || $info['Default'] !== 'CURRENT_TIMESTAMP') {
$column->defaultValue = $column->typecast($info['Default']);
}*/
return
$column
;
}
}
\ No newline at end of file
tests/unit/data/sphinx/sphinx.conf
View file @
0f94ed39
...
...
@@ -50,7 +50,7 @@ source yii2_test_item_src
index
yii2_test_article_index
{
source
=
yii2_test_article_src
path
= /
var
/
lib
/
sphinx
/
yii2_test_article
_src
path
= /
var
/
lib
/
sphinx
/
yii2_test_article
docinfo
=
extern
charset_type
=
sbcs
}
...
...
@@ -59,12 +59,22 @@ index yii2_test_article_index
index
yii2_test_item_index
{
source
=
yii2_test_item_src
path
= /
var
/
lib
/
sphinx
/
yii2_test_item
_src
path
= /
var
/
lib
/
sphinx
/
yii2_test_item
docinfo
=
extern
charset_type
=
sbcs
}
index
yii2_test_rt_index
{
type
=
rt
path
= /
var
/
lib
/
sphinx
/
yii2_test_rt
rt_field
=
title
rt_field
=
content
rt_attr_uint
=
type_id
}
indexer
{
mem_limit
=
32
M
...
...
tests/unit/extensions/sphinx/CommandTest.php
View file @
0f94ed39
...
...
@@ -9,6 +9,14 @@ use yii\db\DataReader;
*/
class
CommandTest
extends
SphinxTestCase
{
protected
function
tearDown
()
{
$this
->
truncateRuntimeIndex
(
'yii2_test_rt_index'
);
parent
::
tearDown
();
}
// Tests :
public
function
testExecute
()
{
$db
=
$this
->
getConnection
();
...
...
@@ -82,4 +90,50 @@ class CommandTest extends SphinxTestCase
$this
->
setExpectedException
(
'\yii\db\Exception'
);
$command
->
query
();
}
/**
* @depends testQuery
*/
public
function
testInsert
()
{
$db
=
$this
->
getConnection
();
$command
=
$db
->
createCommand
()
->
insert
(
'yii2_test_rt_index'
,
[
'title'
=>
'Test title'
,
'content'
=>
'Test content'
,
'type_id'
=>
2
,
'id'
=>
1
,
]);
$this
->
assertEquals
(
1
,
$command
->
execute
(),
'Unable to execute insert!'
);
$rows
=
$db
->
createCommand
(
'SELECT * FROM yii2_test_rt_index'
)
->
queryAll
();
$this
->
assertEquals
(
1
,
count
(
$rows
),
'No row inserted!'
);
}
/**
* @depends testInsert
*/
public
function
testUpdate
()
{
$db
=
$this
->
getConnection
();
$db
->
createCommand
()
->
insert
(
'yii2_test_rt_index'
,
[
'title'
=>
'Test title'
,
'content'
=>
'Test content'
,
'type_id'
=>
2
,
'id'
=>
1
,
])
->
execute
();
$newTypeId
=
5
;
$command
=
$db
->
createCommand
()
->
update
(
'yii2_test_rt_index'
,
[
'title'
=>
'Test title'
,
'content'
=>
'Test content'
,
'type_id'
=>
$newTypeId
,
],
'id = 1'
);
$this
->
assertEquals
(
1
,
$command
->
execute
(),
'Unable to execute update!'
);
}
}
\ No newline at end of file
tests/unit/extensions/sphinx/SphinxTestCase.php
View file @
0f94ed39
...
...
@@ -75,7 +75,7 @@ class SphinxTestCase extends TestCase
if
(
!
$reset
&&
$this
->
sphinx
)
{
return
$this
->
sphinx
;
}
$db
=
new
\yii\
db
\Connection
;
$db
=
new
\yii\
sphinx
\Connection
;
$db
->
dsn
=
$this
->
sphinxConfig
[
'dsn'
];
if
(
isset
(
$this
->
sphinxConfig
[
'username'
]))
{
$db
->
username
=
$this
->
sphinxConfig
[
'username'
];
...
...
@@ -98,4 +98,15 @@ class SphinxTestCase extends TestCase
$this
->
sphinx
=
$db
;
return
$db
;
}
/**
* Truncates the runtime index.
* @param string $indexName index name.
*/
protected
function
truncateRuntimeIndex
(
$indexName
)
{
if
(
$this
->
sphinx
)
{
$this
->
sphinx
->
createCommand
(
'TRUNCATE RTINDEX '
.
$indexName
)
->
execute
();
}
}
}
\ 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