Advertisement
Guest User

LocationSearchTest

a guest
Oct 4th, 2010
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.31 KB | None | 0 0
  1. <?php
  2. /**
  3.  * =============================================================================
  4.  * 2010/09/29
  5.  * $Id: RecordMapperTest.php 357 2010-09-29 11:07:56Z whirlwind $
  6.  * =============================================================================
  7.  */
  8.  
  9. require_once dirname(__FILE__).'/config.php';
  10. require_once 'Location/RecordMapper.php';
  11. require_once 'Location/CityMapper.php';
  12. require_once 'fixture/LocationRecordFixture.php';
  13.  
  14. class Location_RecordMapperTest extends PHPUnit_Framework_TestCase {
  15.     private static $dbh,$validator,$fixture,$countries,$states,$cities;
  16.     private $object,$mapper;
  17.    
  18.     static function setUpBeforeClass() {
  19.         self::$dbh = ConnectionManager::get();
  20.         self::$validator = new Validator();
  21.         self::$fixture = new LocationRecordFixture(self::$dbh);
  22.         self::$fixture->prepareFirst();
  23.         self::$countries = Country::getMapper();
  24.         self::$states = State::getMapper();
  25.         self::$cities = new Location_CityMapper(self::$dbh, self::$validator,
  26.             'aquarius_city');
  27.     }
  28.    
  29.     function setUp() {
  30.         $this->mapper = new Location_RecordMapper(self::$dbh, self::$validator,
  31.             self::$countries, self::$states, self::$cities,
  32.             'aquarius_location');
  33.         $this->object = new Location_Record(self::$validator, $this->mapper,
  34.             self::$countries, self::$states, self::$cities);
  35.         self::$fixture->prepare();
  36.     }
  37.    
  38.     function tearDown() {
  39.         self::$fixture->clear();
  40.     }
  41.    
  42.     static function tearDownAfterClass() {
  43.         self::$fixture->clearLast();
  44.     }
  45.  
  46.     function testDefaultMetadata() {
  47.         $mapper = new Location_RecordMapper(self::$dbh, self::$validator,
  48.             self::$countries, self::$states, self::$cities);
  49.         $this->assertEquals('location', $mapper->getMetadata()->getTableName());
  50.     }
  51.  
  52.     function testMetadata() {
  53.         $meta = $this->mapper->getMetadata();
  54.         $this->assertEquals('aquarius_location', $meta->getTableName());
  55.         $this->assertEquals('id', $meta->getKey());
  56.     }
  57.  
  58.     function testCreateInstance() {
  59.         $object = $this->mapper->createInstance();
  60.         $this->assertNotNull($object);
  61.         $this->assertType('Location_Record', $object);
  62.         $this->assertNull($object->getCountry());
  63.         $this->assertNull($object->getState());
  64.         $this->assertNull($object->getCity());
  65.         $this->assertNull($object->getId());
  66.         $this->assertTrue($object->isNew());
  67.         $this->assertTrue($object->isModified());
  68.     }
  69.  
  70.     function testToArray() {
  71.         $this->object->setId(12345);
  72.         $this->object->setCountry(self::$countries->find(5));
  73.         $this->object->setState(self::$states->find(2));
  74.         $this->object->setCity(self::$cities->find(4));
  75.         $this->assertEquals(array(
  76.             'id' => 12345,
  77.             'country' => 5,
  78.             'state' => 2,
  79.             'city' => 4), $this->mapper->toArray($this->object));
  80.     }
  81.    
  82.     function testToArrayNullReferences() {
  83.         $this->object->setId(12345);
  84.         $this->object->setCountry(self::$countries->find(5));
  85.         $this->object->setState(null);
  86.         $this->object->setCity(null);
  87.         $this->assertEquals(array(
  88.             'id' => 12345,
  89.             'country' => 5,
  90.             'state' => null,
  91.             'city' => null), $this->mapper->toArray($this->object));
  92.     }
  93.    
  94.     function testFromArray() {
  95.         $object = $this->mapper->fromArray(array(
  96.             'id' => 54321,
  97.             'country' => 2,
  98.             'state' => 4,
  99.             'city' => 1));
  100.         $this->assertNotNull($object);
  101.         $this->assertType('Location_Record', $object);
  102.         $this->assertEquals(54321, $object->getId());
  103.         self::$fixture->getCountryFixture()->
  104.             assertObject(2, $object->getCountry());
  105.         self::$fixture->getStateFixture()->assertObject(4, $object->getState());
  106.         self::$fixture->getCityFixture()->assertObject(1, $object->getCity());
  107.         $this->assertFalse($object->isModified());
  108.     }
  109.    
  110.     function testFromArrayNullValues() {
  111.         $object = $this->mapper->fromArray(array(
  112.             'id' => 54321,
  113.             'country' => null,
  114.             'state' => null,
  115.             'city' => null));
  116.         $this->assertNotNull($object);
  117.         $this->assertType('Location_Record', $object);
  118.         $this->assertEquals(54321, $object->getId());
  119.         $this->assertNull($object->getCountry());
  120.         $this->assertNull($object->getState());
  121.         $this->assertNull($object->getCity());
  122.         $this->assertFalse($object->isModified());
  123.     }
  124.  
  125.     function testCreateSearch() {
  126.         $criteria = $this->mapper->createSearch();
  127.         $this->assertNotNull($criteria);
  128.         $this->assertType('Location_RecordSearchCriteria', $criteria);
  129.         $this->assertEquals('Id', $criteria->getOrderBy());
  130.         $this->assertFalse($criteria->isOrderDesc());
  131.     }
  132.  
  133.     function testFind() {
  134.         $object = $this->mapper->find(1);
  135.         $this->assertNotNull($object);
  136.         $this->assertType('Location_Record', $object);
  137.         $this->assertFalse($object->isNew());
  138.         $this->assertFalse($object->isModified());
  139.         self::$fixture->getCountryFixture()->
  140.             assertObject(1, $object->getCountry());
  141.         self::$fixture->getStateFixture()->assertObject(1, $object->getState());
  142.         self::$fixture->getCityFixture()->assertObject(1, $object->getCity());
  143.         $this->assertEquals(1, $object->getId());
  144.     }
  145.        
  146.     function testFindNotFound() {
  147.         $this->assertNull($this->mapper->find(100500));
  148.     }
  149.    
  150.     function testDelete() {
  151.         $object = $this->mapper->find(1);
  152.         $this->assertNotNull($object);
  153.         $this->mapper->delete($object);
  154.         $this->assertTrue($object->isNew());
  155.         $rs = self::$dbh->executeQuery('SELECT COUNT(*) FROM ' .
  156.             $this->mapper->getMetadata()->getTableName() .
  157.             ' WHERE id=1', ResultSet::FETCHMODE_NUM);
  158.         $this->assertTrue($rs->next());
  159.         $this->assertEquals(0, $rs->get(1));
  160.     }
  161.  
  162.     function testSaveDoNothingIfNotModified() {
  163.         $object = $this->mapper->find(1);
  164.         $object->setCountry(2);
  165.         $object->setState(5);
  166.         $object->setCity(3);
  167.         $object->resetModified();
  168.         $this->mapper->save($object);
  169.         $rs = self::$dbh->executeQuery('SELECT * FROM ' .
  170.             $this->mapper->getMetadata()->getTableName() .
  171.             ' WHERE id=1');
  172.         $this->assertTrue($rs->next(), 'Record not found');
  173.         $this->assertNotEquals(2, $rs->getInt('country'));
  174.         $this->assertNotEquals(5, $rs->getInt('state'));
  175.         $this->assertNotEquals(3, $rs->getInt('city'));
  176.     }
  177.  
  178.     function testSaveNew() {
  179.         $object = $this->mapper->createInstance();
  180.         $object->setCountry(self::$countries->find(3));
  181.         $object->setState(self::$states->find(4));
  182.         $object->setCity(self::$cities->find(2));
  183.         $object->save();
  184.         self::$fixture->toDelete($object);
  185.         $this->assertFalse($object->isNew());
  186.         $this->assertFalse($object->isModified());
  187.         $this->assertNotNull($object->getId());
  188.         $sth = self::$dbh->prepareStatement('SELECT * FROM ' .
  189.             $this->mapper->getMetadata()->getTableName() .
  190.             ' WHERE id=?');
  191.         $sth->setInt(1, $object->getId());
  192.         $rs = $sth->executeQuery();
  193.         $this->assertTrue($rs->next(), 'Record not found');
  194.         $this->assertEquals(3, $rs->getInt('country'));
  195.         $this->assertEquals(4, $rs->getInt('state'));
  196.         $this->assertEquals(2, $rs->getInt('city'));
  197.     }
  198.  
  199.     function testSaveUpdate() {
  200.         $object = $this->mapper->find(1);
  201.         $object->setCountry(2);
  202.         $object->setState(4);
  203.         $object->setCity(3);
  204.         $object->save();
  205.         $rs = self::$dbh->executeQuery('SELECT * FROM ' .
  206.             $this->mapper->getMetadata()->getTableName() .
  207.             ' WHERE id=1');
  208.         $this->assertTrue($rs->next(), 'Record not found');
  209.         $this->assertEquals(2, $rs->getInt('country'));
  210.         $this->assertEquals(4, $rs->getInt('state'));
  211.         $this->assertEquals(3, $rs->getInt('city'));
  212.     }
  213.  
  214.     function testFetch() {
  215.         $result = $this->mapper->fetch($this->mapper->createSearch());
  216.         $this->assertNotNull($result);
  217.         $this->assertType('array', $result);
  218.         self::$fixture->assertResult(array(1,2,3,4,5,6), $result);
  219.     }
  220.  
  221.     function testFetchLimited() {
  222.         $result = $this->mapper->
  223.             fetch($this->mapper->createSearch()->limit(2,1));
  224.         $this->assertNotNull($result);
  225.         $this->assertType('array', $result);
  226.         self::$fixture->assertResult(array(2,3), $result);
  227.     }
  228.    
  229.     function testFetchOrderByIdDesc() {
  230.         $result = $this->mapper->
  231.             fetch($this->mapper->createSearch()->orderById(true));
  232.         $this->assertNotNull($result);
  233.         $this->assertType('array', $result);
  234.         self::$fixture->assertResult(array(6,5,4,3,2,1), $result);
  235.     }
  236.    
  237.     function testFetchOrderByRand() {
  238.         $criteria = $this->mapper->createSearch()->orderByRand();
  239.         $result = $this->mapper->fetch($criteria);
  240.         $pass = array();
  241.         $pass[] = $this->mapper->fetch($criteria);
  242.         $pass[] = $this->mapper->fetch($criteria);
  243.         $pass[] = $this->mapper->fetch($criteria);
  244.         $matches = 0;
  245.         $mismatches = 0;
  246.         foreach ( $pass as $p ) {
  247.             for ( $i = 0; $i < sizeof($result); $i ++ ) {
  248.                 if ( $result[$i]->getId() == $p[$i]->getId() ) {
  249.                     $matches ++;
  250.                 } else {
  251.                     $mismatches ++;
  252.                 }
  253.             }
  254.         }
  255.         $this->assertTrue($matches < $mismatches);
  256.     }
  257.  
  258.     function testFetchFilterById() {
  259.         $result = $this->mapper->fetch($this->mapper->createSearch()->
  260.             orderById()->setId(array(1,5)));
  261.         $this->assertNotNull($result);
  262.         $this->assertType('array', $result);
  263.         self::$fixture->assertResult(array(1,5), $result);
  264.     }
  265.  
  266.     function testFetchFilterByCountry() {
  267.         $result = $this->mapper->fetch($this->mapper->createSearch()->
  268.             orderById()->setCountry(array(self::$countries->find(2), 3)));
  269.         $this->assertNotNull($result);
  270.         $this->assertType('array', $result);
  271.         self::$fixture->assertResult(array(2,4), $result);
  272.     }
  273.    
  274.     function testFetchFilterByState() {
  275.         $result = $this->mapper->fetch($this->mapper->createSearch()->
  276.             orderById()->setState(array(self::$states->find(1), 3)));
  277.         $this->assertNotNull($result);
  278.         $this->assertType('array', $result);
  279.         self::$fixture->assertResult(array(1,4,6), $result);
  280.     }
  281.    
  282.     function testFetchFilterByNullState() {
  283.         $result = $this->mapper->fetch($this->mapper->createSearch()->
  284.             orderById()->setState(array(null,2)));
  285.         $this->assertNotNull($result);
  286.         $this->assertType('array', $result);
  287.         self::$fixture->assertResult(array(2,3,5), $result);
  288.     }
  289.    
  290.     function testFetchFilterByCity() {
  291.         $result = $this->mapper->fetch($this->mapper->createSearch()->
  292.             orderById()->setCity(array(self::$cities->find(2), 5)));
  293.         $this->assertNotNull($result);
  294.         $this->assertType('array', $result);
  295.         self::$fixture->assertResult(array(3,5,6), $result);
  296.     }
  297.    
  298.     function testFetchFilterByNullCity() {
  299.         $result = $this->mapper->fetch($this->mapper->createSearch()->
  300.             orderById()->setCity(array(null,2)));
  301.         $this->assertNotNull($result);
  302.         $this->assertType('array', $result);
  303.         self::$fixture->assertResult(array(2,3,4), $result);
  304.     }
  305.    
  306.     function testFetchCount() {
  307.         $this->assertEquals(6, $this->mapper->fetchCount($this->mapper
  308.             ->createSearch()));
  309.     }
  310.    
  311.     function testFetchCountFilterByNullState() {
  312.         $this->assertEquals(3, $this->mapper->fetchCount($this->mapper->
  313.             createSearch()->setState(array(null,2))));
  314.     }
  315.    
  316.     function testFetchCountFilterByNullCity() {
  317.         $this->assertEquals(3, $this->mapper->fetchCount($this->mapper->
  318.             createSearch()->setCity(array(null,2))));
  319.     }
  320.    
  321.     function testFetchCountFilterById() {
  322.         $this->assertEquals(2, $this->mapper->fetchCount($this->mapper->
  323.             createSearch()->setId(array(1,5))));
  324.     }
  325.  
  326.     function testFetchCountFilterByCountry() {
  327.         $this->assertEquals(2, $this->mapper->fetchCount($this->mapper->
  328.             createSearch()->setCountry(array(self::$countries->find(2), 3))));
  329.     }
  330.    
  331.     function testFetchCountFilterByState() {
  332.         $this->assertEquals(3, $this->mapper->fetchCount($this->mapper->
  333.             createSearch()->setState(array(self::$states->find(1), 3))));
  334.     }
  335.    
  336.     function testFetchCountFilterByCity() {
  337.         $this->assertEquals(3, $this->mapper->fetchCount($this->mapper->
  338.             createSearch()->setCity(array(self::$cities->find(2), 5))));
  339.     }
  340.    
  341.     function testSaveFieldCombinationMutBeUnique() {
  342.         $object = $this->mapper->find(1);
  343.         $object->setId(null);
  344.         $this->setExpectedException('SQLException');
  345.         $object->save();
  346.     }
  347.    
  348.     function testFindNearestByCountry() {
  349.         $this->assertNearest(array(1,3,6), self::$countries->find(1));
  350.     }
  351.    
  352.     function testFindNearestByCountryAndState() {
  353.         $this->assertNearest(array(1,6), self::$countries->find(1),
  354.             self::$states->find(1));
  355.     }
  356.    
  357.     function testFindNearestByCountryAndNullState() {
  358.         $this->assertNearest(array(3), self::$countries->find(1), null);
  359.     }
  360.    
  361.     function testFindNearestByCountryAndStateAndCity() {
  362.         $this->assertNearest(array(1), self::$countries->find(1),
  363.             self::$states->find(1), self::$cities->find(1));
  364.     }
  365.    
  366.     function testFindNearestByCountryAndNullStateAndCity() {
  367.         $this->assertNearest(array(3), self::$countries->find(1), null,
  368.             self::$cities->find(2));
  369.     }
  370.    
  371.     function testFindNearestByCountryAndStateAndNullCity() {
  372.         $this->assertNearest(array(4), self::$countries->find(3),
  373.             self::$states->find(3), null);
  374.     }
  375.    
  376.     function testFindNearestByCountryAndNullStateAndNullCity() {
  377.         $this->assertNearest(array(2), self::$countries->find(2), null, null);
  378.     }
  379.    
  380.     function testFindNearestLaxFoundByAll() {
  381.         $this->assertNearestLax(array(1), self::$countries->find(1), 1, 1);
  382.     }
  383.    
  384.     function testFindNearestLaxFoundByCountryAndState() {
  385.         $this->assertNearestLax(array(1,6), self::$countries->find(1), 1, 4);
  386.     }
  387.  
  388.     function testFindNearestLaxFoundByCountryAndCity() {
  389.         $this->assertNearestLax(array(1), self::$countries->find(1), 5, 1);
  390.     }
  391.    
  392.     function testFindNearestLaxFoundByCountry() {
  393.         $this->assertNearestLax(array(1,3,6), self::$countries->find(1), 5, 4);
  394.     }
  395.    
  396.     private function assertNearestLax(array $expectedId,
  397.         Location_Country $country, $state=Location_RecordSearchCriteria::NONE,
  398.         $city=Location_RecordSearchCriteria::NONE)
  399.     {
  400.         $this->assertNearestSearch('findNearestLax', $expectedId, $country,
  401.             $state, $city);
  402.     }
  403.    
  404.     private function assertNearest(array $expectedId,
  405.         Location_Country $country, $state=Location_RecordSearchCriteria::NONE,
  406.         $city=Location_RecordSearchCriteria::NONE)
  407.     {
  408.         $this->assertNearestSearch('findNearest', $expectedId, $country,
  409.             $state, $city);
  410.     }
  411.    
  412.     private function assertNearestSearch($method, array $expectedId,
  413.         Location_Country $country,
  414.         $state=Location_RecordSearchCriteria::NONE,
  415.         $city=Location_RecordSearchCriteria::NONE)
  416.     {
  417.         $matches = array();
  418.         for ( $i = 0; $i < 12; $i ++ ) {
  419.             $object = $this->mapper->$method($country, $state, $city);
  420.             $this->assertNotNull($object);
  421.             $this->assertType('Location_Record', $object);
  422.             $this->assertEquals($country, $object->getCountry());
  423.             if ( in_array($object->getId(), $expectedId) ) {
  424.                 $matches[$object->getId()] = true;
  425.             } else {
  426.                 $this->fail("Unexpected object found: " . $object->getId());
  427.             }
  428.         }
  429.         foreach ( $expectedId as $id ) {
  430.             if ( ! isset($matches[$id]) ) {
  431.                 $this->fail("Object $id not found");
  432.             }
  433.         }
  434.     }
  435.    
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement