Skip to content

Instantly share code, notes, and snippets.

@jamband
Created April 23, 2012 15:37
Show Gist options
  • Save jamband/2471725 to your computer and use it in GitHub Desktop.
Save jamband/2471725 to your computer and use it in GitHub Desktop.
<?php
class Word extends ActiveRecord
{
...
/**
* Gets all models.
* @param mixed $sort null sorting strings
* @param mixed $q null searching strings
* @return array pagination object and all models
*/
public function getAll($sort, $q=null)
{
$sort = substr($sort, 0, 3);
$c = $this->getAllModelsCriteria($sort, $q);
$pages = $this->getPages($c, param('wordPerPage'), $q);
return array($pages, $this->findAll($c));
}
/**
* Gets all models criteria
* @param $sort null sorting strings
* @param $q null searching strings
* @return object criteria
*/
protected function getAllModelsCriteria($sort, $q)
{
$c = new CDbCriteria;
$c->addSearchCondition('t.en', $q, true, 'OR');
$c->addSearchCondition('t.ja', $q, true, 'OR');
if (strlen($sort) === 1)
$c->addSearchCondition('t.en', $sort.'%', false);
$c->order = $this->getOrderBySortOrQ($sort, $q);
return $c;
}
/**
* Gets the pagination object.
* @param object $c criteria
* @param integer $pageSize per page
* @param mixed $q null or searching strings
* @return object pagination
*/
protected function getPages($c, $pageSize, $q)
{
$pages = new CPagination($this->count($c));
$pages->pageSize = $pageSize;
$pages->applyLimit($c);
$pages->params = $q ? compact('q') : null;
return $pages;
}
/**
* Gets order by sort or q
* @param mixed $sort null or sorting strings
* @param mixed $q null or searching strings
* @return string order by
*/
protected function getOrderBySortOrQ($sort, $q)
{
if ($sort === 'az' || strlen($sort) === 1 || !empty($q))
return 't.en';
if ($sort === 'za')
return 't.en DESC';
if ($sort === 'old')
return 't.id';
if ($sort === 'rnd')
return 'RAND()';
return 't.id DESC';
}
}
<?php
class WordTest extends CDbTestCase
{
public $fixtures = array(
'words' => 'Word',
);
protected function setUp()
{
parent::setUp();
user()->setId(1);
}
protected static function getMethod($name)
{
$class = new ReflectionClass('Word');
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}
public function test_getAllModelsCriteria()
{
$word = new Word;
$method = self::getMethod('getAllModelsCriteria');
$c = $method->invokeArgs($word, array(null, null));
$this->assertEmpty($c->condition);
$this->assertEquals('t.id DESC', $c->order);
$c = $method->invokeArgs($word, array('a', null));
$this->assertNotEmpty($c->condition);
$this->assertEquals('t.en', $c->order);
$c = $method->invokeArgs($word, array(null, 'abc'));
$this->assertNotEmpty($c->condition);
$this->assertEquals('t.en', $c->order);
}
public function test_getPages()
{
$word = new Word;
$method = self::getMethod('getPages');
$pages = $method->invokeArgs($word, array(new CDbCriteria, param('wordPerPage'), null));
$this->assertNull($pages->params);
$this->assertEquals(param('wordPerPage'), $pages->pageSize);
$pages = $method->invokeArgs($word, array(new CDbCriteria, param('wordPerPage'), 'abc'));
$this->assertEquals(array('q' => 'abc'), $pages->params);
}
/**
* @dataProvider orderByDataProvider
*/
public function test_getOrderBySortOrQ($expected, $sort, $q)
{
$word = new Word;
$method = self::getMethod('getOrderBySortOrQ');
$this->assertEquals($expected, $method->invokeArgs($word, array($sort, $q)));
}
public function orderByDataProvider()
{
return array(
array('t.en', 'az', null),
array('t.en', 'a', null),
array('t.en', null, 'abc'),
array('t.en DESC', 'za', null),
array('t.id', 'old', null),
array('RAND()', 'rnd', null),
array('t.id DESC', null, null),
array('t.id DESC', 'abcdef', null),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment