Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * =============================================================================
- * 2010/09/29
- * $Id: RecordMapperTest.php 357 2010-09-29 11:07:56Z whirlwind $
- * =============================================================================
- */
- require_once dirname(__FILE__).'/config.php';
- require_once 'Location/RecordMapper.php';
- require_once 'Location/CityMapper.php';
- require_once 'fixture/LocationRecordFixture.php';
- class Location_RecordMapperTest extends PHPUnit_Framework_TestCase {
- private static $dbh,$validator,$fixture,$countries,$states,$cities;
- private $object,$mapper;
- static function setUpBeforeClass() {
- self::$dbh = ConnectionManager::get();
- self::$validator = new Validator();
- self::$fixture = new LocationRecordFixture(self::$dbh);
- self::$fixture->prepareFirst();
- self::$countries = Country::getMapper();
- self::$states = State::getMapper();
- self::$cities = new Location_CityMapper(self::$dbh, self::$validator,
- 'aquarius_city');
- }
- function setUp() {
- $this->mapper = new Location_RecordMapper(self::$dbh, self::$validator,
- self::$countries, self::$states, self::$cities,
- 'aquarius_location');
- $this->object = new Location_Record(self::$validator, $this->mapper,
- self::$countries, self::$states, self::$cities);
- self::$fixture->prepare();
- }
- function tearDown() {
- self::$fixture->clear();
- }
- static function tearDownAfterClass() {
- self::$fixture->clearLast();
- }
- function testDefaultMetadata() {
- $mapper = new Location_RecordMapper(self::$dbh, self::$validator,
- self::$countries, self::$states, self::$cities);
- $this->assertEquals('location', $mapper->getMetadata()->getTableName());
- }
- function testMetadata() {
- $meta = $this->mapper->getMetadata();
- $this->assertEquals('aquarius_location', $meta->getTableName());
- $this->assertEquals('id', $meta->getKey());
- }
- function testCreateInstance() {
- $object = $this->mapper->createInstance();
- $this->assertNotNull($object);
- $this->assertType('Location_Record', $object);
- $this->assertNull($object->getCountry());
- $this->assertNull($object->getState());
- $this->assertNull($object->getCity());
- $this->assertNull($object->getId());
- $this->assertTrue($object->isNew());
- $this->assertTrue($object->isModified());
- }
- function testToArray() {
- $this->object->setId(12345);
- $this->object->setCountry(self::$countries->find(5));
- $this->object->setState(self::$states->find(2));
- $this->object->setCity(self::$cities->find(4));
- $this->assertEquals(array(
- 'id' => 12345,
- 'country' => 5,
- 'state' => 2,
- 'city' => 4), $this->mapper->toArray($this->object));
- }
- function testToArrayNullReferences() {
- $this->object->setId(12345);
- $this->object->setCountry(self::$countries->find(5));
- $this->object->setState(null);
- $this->object->setCity(null);
- $this->assertEquals(array(
- 'id' => 12345,
- 'country' => 5,
- 'state' => null,
- 'city' => null), $this->mapper->toArray($this->object));
- }
- function testFromArray() {
- $object = $this->mapper->fromArray(array(
- 'id' => 54321,
- 'country' => 2,
- 'state' => 4,
- 'city' => 1));
- $this->assertNotNull($object);
- $this->assertType('Location_Record', $object);
- $this->assertEquals(54321, $object->getId());
- self::$fixture->getCountryFixture()->
- assertObject(2, $object->getCountry());
- self::$fixture->getStateFixture()->assertObject(4, $object->getState());
- self::$fixture->getCityFixture()->assertObject(1, $object->getCity());
- $this->assertFalse($object->isModified());
- }
- function testFromArrayNullValues() {
- $object = $this->mapper->fromArray(array(
- 'id' => 54321,
- 'country' => null,
- 'state' => null,
- 'city' => null));
- $this->assertNotNull($object);
- $this->assertType('Location_Record', $object);
- $this->assertEquals(54321, $object->getId());
- $this->assertNull($object->getCountry());
- $this->assertNull($object->getState());
- $this->assertNull($object->getCity());
- $this->assertFalse($object->isModified());
- }
- function testCreateSearch() {
- $criteria = $this->mapper->createSearch();
- $this->assertNotNull($criteria);
- $this->assertType('Location_RecordSearchCriteria', $criteria);
- $this->assertEquals('Id', $criteria->getOrderBy());
- $this->assertFalse($criteria->isOrderDesc());
- }
- function testFind() {
- $object = $this->mapper->find(1);
- $this->assertNotNull($object);
- $this->assertType('Location_Record', $object);
- $this->assertFalse($object->isNew());
- $this->assertFalse($object->isModified());
- self::$fixture->getCountryFixture()->
- assertObject(1, $object->getCountry());
- self::$fixture->getStateFixture()->assertObject(1, $object->getState());
- self::$fixture->getCityFixture()->assertObject(1, $object->getCity());
- $this->assertEquals(1, $object->getId());
- }
- function testFindNotFound() {
- $this->assertNull($this->mapper->find(100500));
- }
- function testDelete() {
- $object = $this->mapper->find(1);
- $this->assertNotNull($object);
- $this->mapper->delete($object);
- $this->assertTrue($object->isNew());
- $rs = self::$dbh->executeQuery('SELECT COUNT(*) FROM ' .
- $this->mapper->getMetadata()->getTableName() .
- ' WHERE id=1', ResultSet::FETCHMODE_NUM);
- $this->assertTrue($rs->next());
- $this->assertEquals(0, $rs->get(1));
- }
- function testSaveDoNothingIfNotModified() {
- $object = $this->mapper->find(1);
- $object->setCountry(2);
- $object->setState(5);
- $object->setCity(3);
- $object->resetModified();
- $this->mapper->save($object);
- $rs = self::$dbh->executeQuery('SELECT * FROM ' .
- $this->mapper->getMetadata()->getTableName() .
- ' WHERE id=1');
- $this->assertTrue($rs->next(), 'Record not found');
- $this->assertNotEquals(2, $rs->getInt('country'));
- $this->assertNotEquals(5, $rs->getInt('state'));
- $this->assertNotEquals(3, $rs->getInt('city'));
- }
- function testSaveNew() {
- $object = $this->mapper->createInstance();
- $object->setCountry(self::$countries->find(3));
- $object->setState(self::$states->find(4));
- $object->setCity(self::$cities->find(2));
- $object->save();
- self::$fixture->toDelete($object);
- $this->assertFalse($object->isNew());
- $this->assertFalse($object->isModified());
- $this->assertNotNull($object->getId());
- $sth = self::$dbh->prepareStatement('SELECT * FROM ' .
- $this->mapper->getMetadata()->getTableName() .
- ' WHERE id=?');
- $sth->setInt(1, $object->getId());
- $rs = $sth->executeQuery();
- $this->assertTrue($rs->next(), 'Record not found');
- $this->assertEquals(3, $rs->getInt('country'));
- $this->assertEquals(4, $rs->getInt('state'));
- $this->assertEquals(2, $rs->getInt('city'));
- }
- function testSaveUpdate() {
- $object = $this->mapper->find(1);
- $object->setCountry(2);
- $object->setState(4);
- $object->setCity(3);
- $object->save();
- $rs = self::$dbh->executeQuery('SELECT * FROM ' .
- $this->mapper->getMetadata()->getTableName() .
- ' WHERE id=1');
- $this->assertTrue($rs->next(), 'Record not found');
- $this->assertEquals(2, $rs->getInt('country'));
- $this->assertEquals(4, $rs->getInt('state'));
- $this->assertEquals(3, $rs->getInt('city'));
- }
- function testFetch() {
- $result = $this->mapper->fetch($this->mapper->createSearch());
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(1,2,3,4,5,6), $result);
- }
- function testFetchLimited() {
- $result = $this->mapper->
- fetch($this->mapper->createSearch()->limit(2,1));
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(2,3), $result);
- }
- function testFetchOrderByIdDesc() {
- $result = $this->mapper->
- fetch($this->mapper->createSearch()->orderById(true));
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(6,5,4,3,2,1), $result);
- }
- function testFetchOrderByRand() {
- $criteria = $this->mapper->createSearch()->orderByRand();
- $result = $this->mapper->fetch($criteria);
- $pass = array();
- $pass[] = $this->mapper->fetch($criteria);
- $pass[] = $this->mapper->fetch($criteria);
- $pass[] = $this->mapper->fetch($criteria);
- $matches = 0;
- $mismatches = 0;
- foreach ( $pass as $p ) {
- for ( $i = 0; $i < sizeof($result); $i ++ ) {
- if ( $result[$i]->getId() == $p[$i]->getId() ) {
- $matches ++;
- } else {
- $mismatches ++;
- }
- }
- }
- $this->assertTrue($matches < $mismatches);
- }
- function testFetchFilterById() {
- $result = $this->mapper->fetch($this->mapper->createSearch()->
- orderById()->setId(array(1,5)));
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(1,5), $result);
- }
- function testFetchFilterByCountry() {
- $result = $this->mapper->fetch($this->mapper->createSearch()->
- orderById()->setCountry(array(self::$countries->find(2), 3)));
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(2,4), $result);
- }
- function testFetchFilterByState() {
- $result = $this->mapper->fetch($this->mapper->createSearch()->
- orderById()->setState(array(self::$states->find(1), 3)));
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(1,4,6), $result);
- }
- function testFetchFilterByNullState() {
- $result = $this->mapper->fetch($this->mapper->createSearch()->
- orderById()->setState(array(null,2)));
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(2,3,5), $result);
- }
- function testFetchFilterByCity() {
- $result = $this->mapper->fetch($this->mapper->createSearch()->
- orderById()->setCity(array(self::$cities->find(2), 5)));
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(3,5,6), $result);
- }
- function testFetchFilterByNullCity() {
- $result = $this->mapper->fetch($this->mapper->createSearch()->
- orderById()->setCity(array(null,2)));
- $this->assertNotNull($result);
- $this->assertType('array', $result);
- self::$fixture->assertResult(array(2,3,4), $result);
- }
- function testFetchCount() {
- $this->assertEquals(6, $this->mapper->fetchCount($this->mapper
- ->createSearch()));
- }
- function testFetchCountFilterByNullState() {
- $this->assertEquals(3, $this->mapper->fetchCount($this->mapper->
- createSearch()->setState(array(null,2))));
- }
- function testFetchCountFilterByNullCity() {
- $this->assertEquals(3, $this->mapper->fetchCount($this->mapper->
- createSearch()->setCity(array(null,2))));
- }
- function testFetchCountFilterById() {
- $this->assertEquals(2, $this->mapper->fetchCount($this->mapper->
- createSearch()->setId(array(1,5))));
- }
- function testFetchCountFilterByCountry() {
- $this->assertEquals(2, $this->mapper->fetchCount($this->mapper->
- createSearch()->setCountry(array(self::$countries->find(2), 3))));
- }
- function testFetchCountFilterByState() {
- $this->assertEquals(3, $this->mapper->fetchCount($this->mapper->
- createSearch()->setState(array(self::$states->find(1), 3))));
- }
- function testFetchCountFilterByCity() {
- $this->assertEquals(3, $this->mapper->fetchCount($this->mapper->
- createSearch()->setCity(array(self::$cities->find(2), 5))));
- }
- function testSaveFieldCombinationMutBeUnique() {
- $object = $this->mapper->find(1);
- $object->setId(null);
- $this->setExpectedException('SQLException');
- $object->save();
- }
- function testFindNearestByCountry() {
- $this->assertNearest(array(1,3,6), self::$countries->find(1));
- }
- function testFindNearestByCountryAndState() {
- $this->assertNearest(array(1,6), self::$countries->find(1),
- self::$states->find(1));
- }
- function testFindNearestByCountryAndNullState() {
- $this->assertNearest(array(3), self::$countries->find(1), null);
- }
- function testFindNearestByCountryAndStateAndCity() {
- $this->assertNearest(array(1), self::$countries->find(1),
- self::$states->find(1), self::$cities->find(1));
- }
- function testFindNearestByCountryAndNullStateAndCity() {
- $this->assertNearest(array(3), self::$countries->find(1), null,
- self::$cities->find(2));
- }
- function testFindNearestByCountryAndStateAndNullCity() {
- $this->assertNearest(array(4), self::$countries->find(3),
- self::$states->find(3), null);
- }
- function testFindNearestByCountryAndNullStateAndNullCity() {
- $this->assertNearest(array(2), self::$countries->find(2), null, null);
- }
- function testFindNearestLaxFoundByAll() {
- $this->assertNearestLax(array(1), self::$countries->find(1), 1, 1);
- }
- function testFindNearestLaxFoundByCountryAndState() {
- $this->assertNearestLax(array(1,6), self::$countries->find(1), 1, 4);
- }
- function testFindNearestLaxFoundByCountryAndCity() {
- $this->assertNearestLax(array(1), self::$countries->find(1), 5, 1);
- }
- function testFindNearestLaxFoundByCountry() {
- $this->assertNearestLax(array(1,3,6), self::$countries->find(1), 5, 4);
- }
- private function assertNearestLax(array $expectedId,
- Location_Country $country, $state=Location_RecordSearchCriteria::NONE,
- $city=Location_RecordSearchCriteria::NONE)
- {
- $this->assertNearestSearch('findNearestLax', $expectedId, $country,
- $state, $city);
- }
- private function assertNearest(array $expectedId,
- Location_Country $country, $state=Location_RecordSearchCriteria::NONE,
- $city=Location_RecordSearchCriteria::NONE)
- {
- $this->assertNearestSearch('findNearest', $expectedId, $country,
- $state, $city);
- }
- private function assertNearestSearch($method, array $expectedId,
- Location_Country $country,
- $state=Location_RecordSearchCriteria::NONE,
- $city=Location_RecordSearchCriteria::NONE)
- {
- $matches = array();
- for ( $i = 0; $i < 12; $i ++ ) {
- $object = $this->mapper->$method($country, $state, $city);
- $this->assertNotNull($object);
- $this->assertType('Location_Record', $object);
- $this->assertEquals($country, $object->getCountry());
- if ( in_array($object->getId(), $expectedId) ) {
- $matches[$object->getId()] = true;
- } else {
- $this->fail("Unexpected object found: " . $object->getId());
- }
- }
- foreach ( $expectedId as $id ) {
- if ( ! isset($matches[$id]) ) {
- $this->fail("Object $id not found");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement