Advertisement
Guest User

OmniDataManagerTest.php

a guest
Apr 16th, 2012
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.54 KB | None | 0 0
  1. <?php
  2.  
  3. namespace tests\broadnet\broadmap;
  4.  
  5. require_once 'setup.php';
  6.  
  7. use \PHPUnit_Framework_TestCase;
  8.  
  9. use \R;
  10.  
  11. use \broadnet\broadmap\OmniDataManager;
  12. use \broadnet\broadmap\OmniDataManagerException;
  13. use \broadnet\files\File;
  14.  
  15. class OmniDataManagerTest extends PHPUnit_Framework_TestCase
  16. {
  17.     public function __construct()
  18.     {
  19.         // Truncate the OmniDataManager tables
  20.         R::wipe(OmniDataManager::TABLE_GROUP);
  21.         R::wipe(OmniDataManager::TABLE_DATA);
  22.     }
  23.  
  24.     public function __destruct()
  25.     {
  26.         // Truncate the OmniDataManager tables
  27.         R::wipe(OmniDataManager::TABLE_GROUP);
  28.         R::wipe(OmniDataManager::TABLE_DATA);
  29.     }
  30.  
  31.     // For every test create a new OmniDataManager
  32.     public function setUp()
  33.     {
  34.         $this->odm = new OmniDataManager();
  35.     }
  36.  
  37.     /**
  38.      * Test the createGroup function
  39.      *
  40.      * @return void
  41.      * @author Tomas Sandven <tomas191191@gmail.com>
  42.      *
  43.      * @dataProvider provideFileImportTests_good
  44.      **/
  45.     public function testCreateGroup($file, $groupname, $group, $mapping)
  46.     {
  47.         // Create a test group
  48.         $id = $this->odm->createGroup($groupname, $group);
  49.  
  50.         // Try to load it back out
  51.         $result = R::load(OmniDataManager::TABLE_GROUP, $id);
  52.  
  53.         // Check that the result is not null
  54.         $this->assertFalse(is_null($result));
  55.  
  56.         return $id;
  57.     }
  58.  
  59.     /**
  60.      * Test the file import function
  61.      *
  62.      * @return void
  63.      * @author Tomas Sandven <tomas191191@gmail.com>
  64.      *
  65.      * @depends testCreateGroup
  66.      * @dataProvider provideFileImportTests_good
  67.      **/
  68.     public function testFileImport($t)
  69.     {
  70.         // Create a file instance of the data file path
  71.         $dataFile = new File($t['file']);
  72.  
  73.         // Import the test file into the test group
  74.         $this->odm->fileImport($dataFile, $t['mapping'], $groupid);
  75.     }
  76.  
  77.     /**
  78.      * Test importing too many data columns
  79.      *
  80.      * @return void
  81.      * @author Tomas Sandven <tomas191191@gmail.com>
  82.      *
  83.      * @depends testCreateGroup
  84.      **/
  85.     public function testFileImportTooManyColumns($groupid)
  86.     {
  87.         // Expect an OmniDataManagerException
  88.         $this->setExpectedException(
  89.             'broadnet\broadmap\OmniDataManagerException',
  90.             null,
  91.             OmniDataManager::ERROR_MAPPINGCOLUMNNOTINGROUP
  92.         );
  93.  
  94.         $mapping = $this->goodMapping;
  95.         $mapping['data']['superflousColumn'] = 'KOMMUNE_NA';
  96.  
  97.         // Create a file instance of the data file path
  98.         $dataFile = new File($this->goodTestDataFile);
  99.  
  100.         // Try importing with the erronious mapping array
  101.         $this->odm->fileImport($dataFile, $mapping, $groupid);
  102.     }
  103.  
  104.     /**
  105.      * Test importing too few data columns
  106.      *
  107.      * @return void
  108.      * @author Tomas Sandven <tomas191191@gmail.com>
  109.      *
  110.      * @depends testCreateGroup
  111.      **/
  112.     public function testFileImportTooFewColumns($groupid)
  113.     {
  114.         // Expect an OmniDataManagerException
  115.         $this->setExpectedException(
  116.             'broadnet\broadmap\OmniDataManagerException',
  117.             null,
  118.             OmniDataManager::ERROR_MAPPINGCOLUMNMISSING
  119.         );
  120.  
  121.         $mapping = $this->goodMapping;
  122.         array_pop($mapping['data']);
  123.  
  124.         // Create a file instance of the data file path
  125.         $dataFile = new File($this->goodTestDataFile);
  126.  
  127.         // Try importing with the erronious mapping array
  128.         $this->odm->fileImport($dataFile, $mapping, $groupid);
  129.     }
  130.  
  131.     /**
  132.      * Tests the deleteGroup function
  133.      *
  134.      * @return void
  135.      * @author Tomas Sandven <tomas191191@gmail.com>
  136.      *
  137.      * @depends testCreateGroup
  138.      * @depends testFileImport
  139.      **/
  140.     public function testRemoveGroup($id)
  141.     {
  142.         $group = R::load(OmniDataManager::TABLE_GROUP, $id);
  143.  
  144.         // Group must exist
  145.         $this->assertFalse($group->id === 0);
  146.  
  147.         // Delete the group
  148.         $this->odm->deleteGroup($group->name);
  149.  
  150.         // Confirm that the group is gone
  151.         $group = R::load(OmniDataManager::TABLE_GROUP, $id);
  152.         $this->assertTrue($group->id === 0);
  153.  
  154.         // Confirm that all data associated with the group is gone
  155.         $data = R::find(
  156.             OmniDataManager::TABLE_DATA,
  157.             'ISNULL(group_id) OR group_id=?',
  158.             array($id)
  159.         );
  160.  
  161.         $this->assertTrue(empty($data), sprintf(
  162.             "All data associated with test group should be gone, but there\n" .
  163.             "are %d such data rows left in the table (%d total)",
  164.             count($data),
  165.             R::count(OmniDataManager::TABLE_DATA)
  166.         ));
  167.     }
  168.  
  169.     /**
  170.      * Provider function for the file import testfiles that should succees
  171.      *
  172.      * @return array
  173.      * @author Tomas Sandven <tomas191191@gmail.com>
  174.      **/
  175.     public function provideFileImportTests_good()
  176.     {
  177.         $files = glob('tests/testfiles/OmniDataManager_fileImport_good*.csv');
  178.         $groupNames = array('Testgroup 1', 'Testgroup 2');
  179.         $groups = array(
  180.             array(
  181.                 'Fiberinfo' => OmniDataManager::TYPE_STRING,
  182.                 'Fiberpunkt id' => OmniDataManager::TYPE_STRING
  183.             ),
  184.             array(
  185.                 'Waypoint navn' => OmniDataManager::TYPE_STRING,
  186.                 'Waypoint description' => OmniDataManager::TYPE_STRING
  187.             )
  188.         );
  189.         $mappings = array(
  190.             array(
  191.                 OmniDataManager::COLUMN_TITLE =>
  192.                     array('column', 'Fib_info (eksisterende)'),
  193.                 OmniDataManager::COLUMN_UTM_ZONE => array('static', 32),
  194.                 OmniDataManager::COLUMN_UTMX =>
  195.                     array('column', 'GAB UX_KOORDIN'),
  196.                 OmniDataManager::COLUMN_UTMY =>
  197.                     array('column', 'GAB UY_KOORDIN'),
  198.                 OmniDataManager::COLUMN_DATA => array(
  199.                     'Fiberinfo' => array('column', 'Fib_info (eksisterende)'),
  200.                     'Fiberpunkt id' => array('column', 'Fiberpkt_id')
  201.                 )
  202.             ),
  203.             array(
  204.                 OmniDataManager::COLUMN_TITLE => array('column', 'NK-navn'),
  205.                 OmniDataManager::COLUMN_LAT => array('column', 'N'),
  206.                 OmniDataManager::COLUMN_LNG => array('column', 'E'),
  207.                 OmniDataManager::COLUMN_DATA => array(
  208.                     'Waypoint navn' => 'Wpt Navn',
  209.                     'Waypoint description' => 'Wpt Description:'
  210.                 )
  211.             )
  212.         );
  213.  
  214.         $output = array();
  215.  
  216.         foreach($files as $index => $file)
  217.         {
  218.             $output[] = array(
  219.                 $file, $groupNames[$index], $groups[$index], $mappings[$index]
  220.             );
  221.         }
  222.  
  223.         return $output;
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement