-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from vlucas/alias_mapping
Fix #97 with support for field/column aliases
- Loading branch information
Showing
6 changed files
with
240 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
namespace SpotTest\Entity; | ||
|
||
use Spot\Entity; | ||
use Spot\EntityInterface; | ||
use Spot\MapperInterface; | ||
use Spot\EventEmitter; | ||
|
||
/** | ||
* Legacy - A legacy database table with custom column mappings | ||
* | ||
* @package Spot | ||
*/ | ||
class Legacy extends Entity | ||
{ | ||
protected static $table = 'test_legacy'; | ||
|
||
public static function fields() | ||
{ | ||
return [ | ||
'id' => ['type' => 'integer', 'autoincrement' => true, 'primary' => true, 'column' => self::getIdFieldColumnName()], | ||
'name' => ['type' => 'string', 'required' => true, 'column' => self::getNameFieldColumnName()], | ||
'number' => ['type' => 'integer', 'required' => true, 'column' => self::getNumberFieldColumnName()], | ||
'date_created' => ['type' => 'datetime', 'value' => new \DateTime(), 'column' => self::getDateCreatedColumnName()], | ||
]; | ||
} | ||
|
||
public static function relations(MapperInterface $mapper, EntityInterface $entity) | ||
{ | ||
return [ | ||
'polymorphic_comments' => $mapper->hasMany($entity, 'SpotTest\Entity\PolymorphicComment', 'item_id')->where(['item_type' => 'legacy']) | ||
]; | ||
} | ||
|
||
/** | ||
* Helpers for field/column names - methods with public access so we can avoid duplication in tests | ||
*/ | ||
public static function getIdFieldColumnName() | ||
{ | ||
return 'obnoxiouslyObtuse_IdentityColumn'; | ||
} | ||
|
||
public static function getNameFieldColumnName() | ||
{ | ||
return 'string_54_LegacyDB_x8'; | ||
} | ||
|
||
public static function getNumberFieldColumnName() | ||
{ | ||
return 'xbf86_haikusInTheDark'; | ||
} | ||
|
||
public static function getDateCreatedColumnName() | ||
{ | ||
return 'dtCreatedAt'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
namespace SpotTest; | ||
use SpotTest\Entity\Legacy; | ||
|
||
/** | ||
* @package Spot | ||
*/ | ||
class FieldAlias extends \PHPUnit_Framework_TestCase | ||
{ | ||
public static $legacyTable; | ||
|
||
public static function setupBeforeClass() | ||
{ | ||
self::$legacyTable = new \SpotTest\Entity\Legacy(); | ||
foreach (['Legacy', 'PolymorphicComment'] as $entity) { | ||
test_spot_mapper('SpotTest\Entity\\' . $entity)->migrate(); | ||
} | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
foreach (['Legacy', 'PolymorphicComment'] as $entity) { | ||
test_spot_mapper('\SpotTest\Entity\\' . $entity)->dropTable(); | ||
} | ||
} | ||
|
||
public function testLegacySelectFieldsAreAliases() | ||
{ | ||
$mapper = test_spot_mapper('SpotTest\Entity\Legacy'); | ||
$query = $mapper->select()->noQuote()->where(['number' => 2, 'name' => 'legacy_crud']); | ||
$this->assertEquals("SELECT * FROM test_legacy test_legacy WHERE test_legacy." . self::$legacyTable->getNumberFieldColumnName() ." = ? AND test_legacy." . self::$legacyTable->getNameFieldColumnName() . " = ?", $query->toSql()); | ||
} | ||
|
||
// Ordering | ||
public function testLegacyOrderBy() | ||
{ | ||
$mapper = test_spot_mapper('SpotTest\Entity\Legacy'); | ||
$query = $mapper->select()->noQuote()->where(['number' => 2])->order(['date_created' => 'ASC']); | ||
$this->assertContains("ORDER BY test_legacy." . self::$legacyTable->getDateCreatedColumnName() . " ASC", $query->toSql()); | ||
} | ||
|
||
// Grouping | ||
public function testLegacyGroupBy() | ||
{ | ||
$mapper = test_spot_mapper('SpotTest\Entity\Legacy'); | ||
$query = $mapper->select()->noQuote()->where(['name' => 'test_group'])->group(['id']); | ||
$this->assertEquals("SELECT * FROM test_legacy test_legacy WHERE test_legacy." . self::$legacyTable->getNameFieldColumnName() . " = ? GROUP BY test_legacy." . self::$legacyTable->getIdFieldColumnName(), $query->toSql()); | ||
} | ||
|
||
// Insert | ||
public function testLegacyInsert() | ||
{ | ||
$legacy = new Legacy(); | ||
$legacy->name = 'Something Here'; | ||
$legacy->number = 5; | ||
|
||
$mapper = test_spot_mapper('SpotTest\Entity\Legacy'); | ||
$mapper->save($legacy); | ||
return $legacy; | ||
} | ||
|
||
/** | ||
* @depends testLegacyInsert | ||
*/ | ||
public function testLegacyUpdate(Legacy $legacy) | ||
{ | ||
$legacy->name = 'Something ELSE Here'; | ||
$legacy->number = 6; | ||
|
||
$mapper = test_spot_mapper('SpotTest\Entity\Legacy'); | ||
$mapper->save($legacy); | ||
} | ||
|
||
/** | ||
* @depends testLegacyInsert | ||
*/ | ||
public function testLegacyEntityFieldMapping(Legacy $legacy) | ||
{ | ||
$mapper = test_spot_mapper('SpotTest\Entity\Legacy'); | ||
$savedLegacyItem = $mapper->first(); | ||
|
||
$this->assertEquals($legacy->name, $savedLegacyItem->name); | ||
$this->assertEquals($legacy->number, $savedLegacyItem->number); | ||
} | ||
|
||
/** | ||
* @depends testLegacyInsert | ||
*/ | ||
public function testLegacyRelations(Legacy $legacy) | ||
{ | ||
// New Comment | ||
$commentMapper = test_spot_mapper('SpotTest\Entity\PolymorphicComment'); | ||
$comment = new \SpotTest\Entity\PolymorphicComment([ | ||
'item_id' => $legacy->id, | ||
'item_type' => 'legacy', | ||
'name' => 'Testy McTesterpants', | ||
'email' => '[email protected]', | ||
'body' => '<p>Comment Text</p>' | ||
]); | ||
$commentMapper->save($comment); | ||
|
||
$this->assertInstanceOf('Spot\Relation\HasMany', $legacy->polymorphic_comments); | ||
$this->assertEquals(count($legacy->polymorphic_comments), 1); | ||
} | ||
} |