diff --git a/extensions/sphinx/LICENSE.md b/extensions/sphinx/LICENSE.md new file mode 100644 index 0000000..0bb1a8d --- /dev/null +++ b/extensions/sphinx/LICENSE.md @@ -0,0 +1,32 @@ +The Yii framework is free software. It is released under the terms of +the following BSD License. + +Copyright © 2008-2013 by Yii Software LLC (http://www.yiisoft.com) +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + * Neither the name of Yii Software LLC nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/extensions/sphinx/README.md b/extensions/sphinx/README.md new file mode 100644 index 0000000..fad7613 --- /dev/null +++ b/extensions/sphinx/README.md @@ -0,0 +1,42 @@ +Yii 2.0 Public Preview - Sphinx Extension +========================================= + +Thank you for choosing Yii - a high-performance component-based PHP framework. + +If you are looking for a production-ready PHP framework, please use +[Yii v1.1](https://github.com/yiisoft/yii). + +Yii 2.0 is still under heavy development. We may make significant changes +without prior notices. **Yii 2.0 is not ready for production use yet.** + +[![Build Status](https://secure.travis-ci.org/yiisoft/yii2.png)](http://travis-ci.org/yiisoft/yii2) + +This is the yii2-sphinx extension. + + +Installation +------------ + +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run +``` +php composer.phar require yiisoft/yii2-sphinx "*" +``` + +or add +``` +"yiisoft/yii2-sphinx": "*" +``` +to the require section of your composer.json. + + +*Note: You might have to run `php composer.phar selfupdate`* + + +Usage & Documentation +--------------------- + +This extension adds [Sphinx](http://sphinxsearch.com/docs) full text search engine extension for the Yii framework. +This extension interact with Sphinx search daemon using MySQL protocol and [SphinxQL](http://sphinxsearch.com/docs/current.html#sphinxql) query language. + diff --git a/extensions/sphinx/composer.json b/extensions/sphinx/composer.json new file mode 100644 index 0000000..a42358e --- /dev/null +++ b/extensions/sphinx/composer.json @@ -0,0 +1,27 @@ +{ + "name": "yiisoft/yii2-sphinx", + "description": "Sphinx full text search engine extension for the Yii framework", + "keywords": ["yii", "sphinx", "search", "fulltext"], + "type": "yii2-extension", + "license": "BSD-3-Clause", + "support": { + "issues": "https://github.com/yiisoft/yii2/issues?state=open", + "forum": "http://www.yiiframework.com/forum/", + "wiki": "http://www.yiiframework.com/wiki/", + "irc": "irc://irc.freenode.net/yii", + "source": "https://github.com/yiisoft/yii2" + }, + "authors": [ + { + "name": "Paul Klimov", + "email": "klimov.paul@gmail.com" + } + ], + "minimum-stability": "dev", + "require": { + "yiisoft/yii2": "*" + }, + "autoload": { + "psr-0": { "yii\\sphinx\\": "" } + } +} diff --git a/extensions/sphinx/yii/sphinx/Connection.php b/extensions/sphinx/yii/sphinx/Connection.php new file mode 100644 index 0000000..c63ec7d --- /dev/null +++ b/extensions/sphinx/yii/sphinx/Connection.php @@ -0,0 +1,19 @@ + + * @since 2.0 + */ +class Connection extends \yii\db\Connection +{ + // +} \ No newline at end of file diff --git a/tests/unit/extensions/sphinx/ConnectionTest.php b/tests/unit/extensions/sphinx/ConnectionTest.php new file mode 100644 index 0000000..803f627 --- /dev/null +++ b/tests/unit/extensions/sphinx/ConnectionTest.php @@ -0,0 +1,42 @@ +getConnection(false); + $params = $this->sphinxConfig; + + $this->assertEquals($params['dsn'], $connection->dsn); + $this->assertEquals($params['username'], $connection->username); + $this->assertEquals($params['password'], $connection->password); + } + + public function testOpenClose() + { + $connection = $this->getConnection(false, false); + + $this->assertFalse($connection->isActive); + $this->assertEquals(null, $connection->pdo); + + $connection->open(); + $this->assertTrue($connection->isActive); + $this->assertTrue($connection->pdo instanceof \PDO); + + $connection->close(); + $this->assertFalse($connection->isActive); + $this->assertEquals(null, $connection->pdo); + + $connection = new Connection; + $connection->dsn = 'unknown::memory:'; + $this->setExpectedException('yii\db\Exception'); + $connection->open(); + } +} \ No newline at end of file diff --git a/tests/unit/extensions/sphinx/SphinxTestCase.php b/tests/unit/extensions/sphinx/SphinxTestCase.php new file mode 100644 index 0000000..c643c65 --- /dev/null +++ b/tests/unit/extensions/sphinx/SphinxTestCase.php @@ -0,0 +1,101 @@ + 'mysql:host=127.0.0.1;port=9306;', + 'username' => '', + 'password' => '', + ]; + /** + * @var Connection + */ + protected $sphinx; + + public static function setUpBeforeClass() + { + static::loadClassMap(); + } + + 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.'); + } + $this->mockApplication(); + static::loadClassMap(); + } + + protected function tearDown() + { + if ($this->sphinx) { + $this->sphinx->close(); + } + $this->destroyApplication(); + } + + /** + * Adds sphinx extension files to [[Yii::$classPath]], + * avoiding the necessity of usage Composer autoloader. + */ + protected static function loadClassMap() + { + $baseNameSpace = 'yii/sphinx'; + $basePath = realpath(__DIR__. '/../../../../extensions/sphinx/yii/sphinx'); + $files = FileHelper::findFiles($basePath); + foreach ($files as $file) { + $classRelativePath = str_replace($basePath, '', $file); + $classFullName = str_replace(['/', '.php'], ['\\', ''], $baseNameSpace . $classRelativePath); + Yii::$classMap[$classFullName] = $file; + } + } + + /** + * @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 getConnection($reset = true, $open = true) + { + if (!$reset && $this->sphinx) { + return $this->sphinx; + } + $db = new \yii\db\Connection; + $db->dsn = $this->sphinxConfig['dsn']; + if (isset($this->sphinxConfig['username'])) { + $db->username = $this->sphinxConfig['username']; + $db->password = $this->sphinxConfig['password']; + } + if (isset($this->sphinxConfig['attributes'])) { + $db->attributes = $this->sphinxConfig['attributes']; + } + 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; + } +} \ No newline at end of file