Advertisement
codemonkey

Untitled

Jun 15th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.11 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 static function setUpBeforeClass()
  18.     {
  19.         self::wipeOmniDataTables();
  20.     }
  21.  
  22.     public static function tearDownAfterClass()
  23.     {
  24.         self::wipeOmniDataTables();
  25.     }
  26.  
  27.     public function setUp()
  28.     {
  29.         $this->odm = new OmniDataManager();
  30.     }
  31.  
  32.     /**
  33.      * Test the createGroup function
  34.      *
  35.      * @author Tomas Sandven <tomas191191@gmail.com>
  36.      *
  37.      * @dataProvider provideFileImportScenarios
  38.      * @group omnidatamanager-group-functions
  39.      **/
  40.     public function testCreateGroup($file, $groupname, $group, $mapping)
  41.     {
  42.         // Create a test group
  43.         $id = $this->odm->createGroup($groupname, $group);
  44.  
  45.         // Try to load it back out
  46.         $result = R::load(OmniDataManager::TABLE_GROUP, $id);
  47.  
  48.         // Check that the result is not null
  49.         $this->assertFalse(is_null($result));
  50.     }
  51.  
  52.     /**
  53.      * Test the file import function
  54.      *
  55.      * @author Tomas Sandven <tomas191191@gmail.com>
  56.      *
  57.      * @depends testCreateGroup
  58.      * @dataProvider provideFileImportScenarios
  59.      **/
  60.     public function testFileImport($file, $groupname, $group, $mapping)
  61.     {
  62.         $this->wipeOmniDataTables();
  63.  
  64.         // Create test group
  65.         $groupid = $this->odm->createGroup($groupname, $group);
  66.  
  67.         // Create a file instance of the data file path
  68.         $dataFile = new File($file);
  69.  
  70.         // Import the test file into the test group
  71.         $this->odm->fileImport($dataFile, $mapping, $groupid);
  72.  
  73.         // Fetch the imported data
  74.         $importedData = R::findAll(
  75.             OmniDataManager::TABLE_DATA,
  76.             'WHERE group_id=?',
  77.             array($groupid)
  78.         );
  79.  
  80.         $this->assertFalse(is_null($importedData));
  81.     }
  82.  
  83.     /**
  84.      * Test importing a file without specifying any coordinate data
  85.      *
  86.      * @author Tomas Sandven <tomas191191@gmail.com>
  87.      *
  88.      * @depends testCreateGroup
  89.      **/
  90.     public function testFileImportWithoutCoordinates()
  91.     {
  92.         $this->markTestIncomplete();
  93.     }
  94.  
  95.     /**
  96.      * Test importing too many data columns
  97.      *
  98.      * @return void
  99.      * @author Tomas Sandven <tomas191191@gmail.com>
  100.      *
  101.      * @depends testCreateGroup
  102.      **/
  103.     public function testFileImportTooManyDataColumns($groupid)
  104.     {
  105.         // Expect an OmniDataManagerException
  106.         $this->setExpectedException(
  107.             'broadnet\broadmap\OmniDataManagerException',
  108.             null,
  109.             OmniDataManager::ERROR_MAPPINGCOLUMNNOTINGROUP
  110.         );
  111.  
  112.         $mapping = $this->goodMapping;
  113.         $mapping['data']['superflousColumn'] = 'KOMMUNE_NA';
  114.  
  115.         // Create a file instance of the data file path
  116.         $dataFile = new File($this->goodTestDataFile);
  117.  
  118.         // Try importing with the erronious mapping array
  119.         $this->odm->fileImport($dataFile, $mapping, $groupid);
  120.  
  121.         $this->markTestIncomplete();
  122.     }
  123.  
  124.     /**
  125.      * Test importing too few data columns
  126.      *
  127.      * @return void
  128.      * @author Tomas Sandven <tomas191191@gmail.com>
  129.      *
  130.      * @depends testCreateGroup
  131.      **/
  132.     public function testFileImportTooFewDataColumns($groupid)
  133.     {
  134.         // Expect an OmniDataManagerException
  135.         $this->setExpectedException(
  136.             'broadnet\broadmap\OmniDataManagerException',
  137.             null,
  138.             OmniDataManager::ERROR_MAPPINGCOLUMNMISSING
  139.         );
  140.  
  141.         $mapping = $this->goodMapping;
  142.         array_pop($mapping['data']);
  143.  
  144.         // Create a file instance of the data file path
  145.         $dataFile = new File($this->goodTestDataFile);
  146.  
  147.         // Try importing with the erronious mapping array
  148.         $this->odm->fileImport($dataFile, $mapping, $groupid);
  149.  
  150.         $this->markTestIncomplete();
  151.     }
  152.  
  153.     /**
  154.      * Tests the fetchGroups function
  155.      *
  156.      * @group omnidatamanager-group-functions
  157.      *
  158.      * @author Tomas Sandven <tomas191191@gmail.com>
  159.      **/
  160.     public function testFetchGroups()
  161.     {
  162.         $groups = $this->odm->fetchGroups();
  163.  
  164.         var_dump($groups);
  165.     }
  166.  
  167.     /**
  168.      * Tests the deleteGroup function
  169.      *
  170.      * @author Tomas Sandven <tomas191191@gmail.com>
  171.      *
  172.      * @depends testCreateGroup
  173.      *
  174.      * @group omnidatamanager-group-functions
  175.      **/
  176.     public function testRemoveGroup()
  177.     {
  178.         $groups = R::findAll(OmniDataManager::TABLE_GROUP);
  179.  
  180.         foreach($groups as $index => $group)
  181.         {
  182.             $groupid = $group->id;
  183.  
  184.             // Delete the group
  185.             $this->odm->deleteGroup($group->name);
  186.  
  187.             // Confirm that the group is gone
  188.             $group = R::load(OmniDataManager::TABLE_GROUP, $groupid);
  189.             $this->assertTrue($group->id === 0);
  190.  
  191.             // Confirm that all data associated with the group is gone
  192.             $data = R::find(
  193.                 OmniDataManager::TABLE_DATA,
  194.                 'ISNULL(group_id) OR group_id=?',
  195.                 array($groupid)
  196.             );
  197.  
  198.             $this->assertTrue(empty($data), sprintf(
  199.                 'All data associated with group "%s" should be gone, but "%d"' .
  200.                     ' rows remain',
  201.                 $group->name,
  202.                 R::count(OmniDataManager::TABLE_DATA, 'group_id=?',
  203.                          array($groupid))
  204.             ));
  205.         }
  206.     }
  207.  
  208.     /**
  209.      * Removes all groups and data
  210.      *
  211.      * @author Tomas Sandven <tomas191191@gmail.com>
  212.      **/
  213.     public static function wipeOmniDataTables()
  214.     {
  215.         // Truncate the OmniDataManager tables
  216.         R::wipe(OmniDataManager::TABLE_GROUP);
  217.         R::wipe(OmniDataManager::TABLE_DATA);
  218.     }
  219.  
  220.     /**
  221.      * Provider function for the file import testfiles that should succees
  222.      *
  223.      * @return array
  224.      * @author Tomas Sandven <tomas191191@gmail.com>
  225.      **/
  226.     public function provideFileImportScenarios()
  227.     {
  228.         return array(
  229.             array(
  230.                 // Filename
  231.                 'tests/testfiles/OmniDataManager_fileImport_utmcoords.csv',
  232.  
  233.                 // Group name
  234.                 'UTMCoords testgroup',
  235.  
  236.                 // Group structure
  237.                 array(
  238.                     'Fiberinfo' => OmniDataManager::TYPE_STRING,
  239.                     'Fiberpunkt id' => OmniDataManager::TYPE_STRING
  240.                 ),
  241.  
  242.                 // Mapping
  243.                 array(
  244.                     OmniDataManager::COLUMN_TITLE =>
  245.                         array('column', 'Fib_info (eksisterende)'),
  246.                     OmniDataManager::COLUMN_UTM_ZONE => array('static', 32),
  247.                     OmniDataManager::COLUMN_UTMX =>
  248.                         array('column', 'GAB UX_KOORDIN'),
  249.                     OmniDataManager::COLUMN_UTMY =>
  250.                         array('column', 'GAB UY_KOORDIN'),
  251.                     OmniDataManager::COLUMN_DATA => array(
  252.                         'Fiberinfo' =>
  253.                             array('column', 'Fib_info (eksisterende)'),
  254.                         'Fiberpunkt id' => array('column', 'Fiberpkt_id')
  255.                     )
  256.                 )
  257.             ),
  258.             array(
  259.                 // Filename
  260.                 'tests/testfiles/OmniDataManager_fileImport_wgscoords.csv',
  261.  
  262.                 // Group name
  263.                 'WGSCoords testgroup',
  264.  
  265.                 // Group structure
  266.                 array(
  267.                     'Waypoint navn' => OmniDataManager::TYPE_STRING,
  268.                     'Waypoint description' => OmniDataManager::TYPE_STRING
  269.                 ),
  270.  
  271.                 // Mapping
  272.                 array(
  273.                     OmniDataManager::COLUMN_TITLE => array('column', 'NK-navn'),
  274.                     OmniDataManager::COLUMN_LAT => array('column', 'N'),
  275.                     OmniDataManager::COLUMN_LNG => array('column', 'E'),
  276.                     OmniDataManager::COLUMN_DATA => array(
  277.                         'Waypoint navn' =>
  278.                             array('column', 'Wpt Navn'),
  279.                         'Waypoint description' =>
  280.                             array('column', 'Wpt Description:')
  281.                     )
  282.                 )
  283.             )
  284.         );
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement