Skip to content

Commit

Permalink
Add unit tests for Horde_Rdo_Query.
Browse files Browse the repository at this point in the history
Contains a test for bug 14511 too.
  • Loading branch information
yunosh committed Nov 15, 2016
1 parent 26c26fe commit b44e672
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 2 deletions.
8 changes: 6 additions & 2 deletions framework/Rdo/package.xml
Expand Up @@ -11,7 +11,7 @@
<email>chuck@horde.org</email>
<active>yes</active>
</lead>
<date>2016-09-02</date>
<date>2016-11-15</date>
<version>
<release>2.1.1</release>
<api>1.3.0</api>
Expand Down Expand Up @@ -79,6 +79,7 @@
<file name="ManyToManyBMapper.php" role="test" />
<file name="RelatedThing.php" role="test" />
<file name="RelatedThingMapper.php" role="test" />
<file name="SimpleMapper.php" role="test" />
<file name="SomeEagerBaseObject.php" role="test" />
<file name="SomeEagerBaseObjectMapper.php" role="test" />
<file name="SomeLazyBaseObject.php" role="test" />
Expand All @@ -94,6 +95,7 @@
<file name="Autoload.php" role="test" />
<file name="bootstrap.php" role="test" />
<file name="phpunit.xml" role="test" />
<file name="QueryTest.php" role="test" />
</dir> <!-- /test/Horde/Rdo -->
</dir> <!-- /test/Horde -->
</dir> <!-- /test -->
Expand Down Expand Up @@ -171,13 +173,15 @@
<install as="Horde/Rdo/Autoload.php" name="test/Horde/Rdo/Autoload.php" />
<install as="Horde/Rdo/bootstrap.php" name="test/Horde/Rdo/bootstrap.php" />
<install as="Horde/Rdo/phpunit.xml" name="test/Horde/Rdo/phpunit.xml" />
<install as="Horde/Rdo/QueryTest.php" name="test/Horde/Rdo/QueryTest.php" />
<install as="Horde/Rdo/fixtures/unit_tests.sql" name="test/Horde/Rdo/fixtures/unit_tests.sql" />
<install as="Horde/Rdo/Objects/ManyToManyA.php" name="test/Horde/Rdo/Objects/ManyToManyA.php" />
<install as="Horde/Rdo/Objects/ManyToManyAMapper.php" name="test/Horde/Rdo/Objects/ManyToManyAMapper.php" />
<install as="Horde/Rdo/Objects/ManyToManyB.php" name="test/Horde/Rdo/Objects/ManyToManyB.php" />
<install as="Horde/Rdo/Objects/ManyToManyBMapper.php" name="test/Horde/Rdo/Objects/ManyToManyBMapper.php" />
<install as="Horde/Rdo/Objects/RelatedThing.php" name="test/Horde/Rdo/Objects/RelatedThing.php" />
<install as="Horde/Rdo/Objects/RelatedThingMapper.php" name="test/Horde/Rdo/Objects/RelatedThingMapper.php" />
<install as="Horde/Rdo/Objects/SimpleMapper.php" name="test/Horde/Rdo/Objects/SimpleMapper.php" />
<install as="Horde/Rdo/Objects/SomeEagerBaseObject.php" name="test/Horde/Rdo/Objects/SomeEagerBaseObject.php" />
<install as="Horde/Rdo/Objects/SomeEagerBaseObjectMapper.php" name="test/Horde/Rdo/Objects/SomeEagerBaseObjectMapper.php" />
<install as="Horde/Rdo/Objects/SomeLazyBaseObject.php" name="test/Horde/Rdo/Objects/SomeLazyBaseObject.php" />
Expand Down Expand Up @@ -425,7 +429,7 @@ Major improvements including inflection, eager or lazy loading of relationships
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2016-09-02</date>
<date>2016-11-15</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
Expand Down
1 change: 1 addition & 0 deletions framework/Rdo/test/Horde/Rdo/Autoload.php
Expand Up @@ -27,3 +27,4 @@
require_once __DIR__ . '/Objects/ManyToManyB.php';
require_once __DIR__ . '/Objects/ManyToManyAMapper.php';
require_once __DIR__ . '/Objects/ManyToManyBMapper.php';
require_once __DIR__ . '/Objects/SimpleMapper.php';
5 changes: 5 additions & 0 deletions framework/Rdo/test/Horde/Rdo/Objects/SimpleMapper.php
@@ -0,0 +1,5 @@
<?php
class Horde_Rdo_Test_Objects_SimpleMapper extends Horde_Rdo_Mapper
{
protected $_table = 'horde_rdo_test';
}
257 changes: 257 additions & 0 deletions framework/Rdo/test/Horde/Rdo/QueryTest.php
@@ -0,0 +1,257 @@
<?php
/**
* Copyright 2016 Horde LLC (http://www.horde.org/)
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @package Rdo
* @subpackage UnitTests
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
*/
class Horde_Rdo_QueryTest extends Horde_Test_Case
{
protected $db;
protected $mapper;

public function setUp()
{
$factory_db = new Horde_Test_Factory_Db();
$this->db = $factory_db->create();
$this->mapper = new Horde_Rdo_Test_Objects_SimpleMapper($this->db);
$migration = new Horde_Db_Migration_Base($this->db);
try {
$migration->dropTable('horde_rdo_test');
} catch (Horde_Db_Exception $e) {
}
$t = $migration->createTable(
'horde_rdo_test', array('autoincrementKey' => 'id')
);
$t->column('intprop', 'integer');
$t->column('textprop', 'string');
$t->end();
$migration->migrate('up');
}

public function testConstructor()
{
$query = new Horde_Rdo_Query();
$this->assertNull($query->mapper);
$query = new Horde_Rdo_Query($this->mapper);
$this->assertSame($this->mapper, $query->mapper);
}

public function testCreate()
{
$query1 = new Horde_Rdo_Query();
$query2 = Horde_Rdo_Query::create($query1);
$this->assertInstanceOf('Horde_Rdo_Query', $query2);
$this->assertNotSame($query1, $query2);

$query = Horde_Rdo_Query::create(4, $this->mapper);
$this->assertInstanceOf('Horde_Rdo_Query', $query);
$this->assertEquals(
array(
array(
'field' => $this->mapper->tableDefinition->getPrimaryKey(),
'test' => '=',
'value' => 4
),
),
$query->tests
);

$query = Horde_Rdo_Query::create(
array('textprop' => 'bar', 'intprop' => 2),
$this->mapper
);
$this->assertInstanceOf('Horde_Rdo_Query', $query);
$this->assertEquals(
array(
array('field' => 'textprop', 'test' => '=', 'value' => 'bar'),
array('field' => 'intprop', 'test' => '=', 'value' => 2),
),
$query->tests
);
$this->assertEquals(
array(
'horde_rdo_test.id',
'horde_rdo_test.intprop',
'horde_rdo_test.textprop'
),
$query->fields
);
$this->assertEquals('AND', $query->conjunction);
}

public function testGetQuery()
{
$query = Horde_Rdo_Query::create(
array('textprop' => 'bar', 'intprop' => 2),
$this->mapper
);
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."textprop" = ? AND horde_rdo_test."intprop" = ?',
array('bar', 2)
),
$query->getQuery()
);
}

public function testDistinct()
{
$query = Horde_Rdo_Query::create(4, $this->mapper);
$query->distinct(true);
$this->assertEquals(
array(
'SELECT DISTINCT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ?',
array(4)
),
$query->getQuery()
);
}

public function testSetFields()
{
$query = Horde_Rdo_Query::create(4, $this->mapper);
$query1 = $query->setFields(array('intprop'));
$this->assertSame($query, $query1);
$this->assertEquals(
array(
'SELECT intprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ?',
array(4)
),
$query->getQuery()
);

$query->setFields(array('intprop'), 'prefix.');
$this->assertEquals(
array(
'SELECT prefix.intprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ?',
array(4)
),
$query->getQuery()
);
}

public function testAddFields()
{
$query = Horde_Rdo_Query::create(4, $this->mapper);
$query->setFields(array('intprop'));
$query->addFields(array('id'));
$this->assertEquals(
array(
'SELECT intprop, id FROM horde_rdo_test WHERE horde_rdo_test."id" = ?',
array(4)
),
$query->getQuery()
);

$query->addFields(array('textprop'), 'prefix.');
$this->assertEquals(
array(
'SELECT intprop, id, prefix.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ?',
array(4)
),
$query->getQuery()
);
}

public function testCombineWith()
{
$query = Horde_Rdo_Query::create(
array('textprop' => 'bar', 'intprop' => 2),
$this->mapper
);
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."textprop" = ? AND horde_rdo_test."intprop" = ?',
array('bar', 2)
),
$query->getQuery()
);
$query->combineWith('OR');
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."textprop" = ? OR horde_rdo_test."intprop" = ?',
array('bar', 2)
),
$query->getQuery()
);
}

public function testSortBy()
{
$query = Horde_Rdo_Query::create(4, $this->mapper);
$query->sortBy('intprop');
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ? ORDER BY intprop',
array(4)
),
$query->getQuery()
);
$query->sortBy('textprop');
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ? ORDER BY intprop, textprop',
array(4)
),
$query->getQuery()
);
}

public function testSortByProperty()
{
$query = Horde_Rdo_Query::create(4, $this->mapper);
$query->sortBy('intprop');
$this->assertEquals(array('intprop'), $query->sortby);

$this->mapper->sortBy('textprop');
$query = Horde_Rdo_Query::create(4, $this->mapper);
$this->assertEquals(array('textprop'), $query->sortby);
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ? ORDER BY textprop',
array(4)
),
$query->getQuery()
);
}

public function testClearSort()
{
$query = Horde_Rdo_Query::create(4, $this->mapper);
$query->sortBy('intprop');
$query->clearSort();
$query->sortBy('textprop');
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ? ORDER BY textprop',
array(4)
),
$query->getQuery()
);
}

public function testLimit()
{
$query = Horde_Rdo_Query::create(4, $this->mapper);
$query->limit(10);
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ? LIMIT 10',
array(4)
),
$query->getQuery()
);
$query->limit(10, 20);
$this->assertEquals(
array(
'SELECT horde_rdo_test.id, horde_rdo_test.intprop, horde_rdo_test.textprop FROM horde_rdo_test WHERE horde_rdo_test."id" = ? LIMIT 20, 10',
array(4)
),
$query->getQuery()
);
}
}

0 comments on commit b44e672

Please sign in to comment.