Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace tests\broadnet\broadmap;
- require_once 'setup.php';
- use \PHPUnit_Framework_TestCase;
- use \R;
- use \broadnet\broadmap\OmniDataManager;
- use \broadnet\broadmap\OmniDataManagerException;
- use \broadnet\files\File;
- class OmniDataManagerTest extends PHPUnit_Framework_TestCase
- {
- public static function setUpBeforeClass()
- {
- self::wipeOmniDataTables();
- }
- public static function tearDownAfterClass()
- {
- self::wipeOmniDataTables();
- }
- public function setUp()
- {
- $this->odm = new OmniDataManager();
- }
- /**
- * Test the createGroup function
- *
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @dataProvider provideFileImportScenarios
- * @group omnidatamanager-group-functions
- **/
- public function testCreateGroup($file, $groupname, $group, $mapping)
- {
- // Create a test group
- $id = $this->odm->createGroup($groupname, $group);
- // Try to load it back out
- $result = R::load(OmniDataManager::TABLE_GROUP, $id);
- // Check that the result is not null
- $this->assertFalse(is_null($result));
- }
- /**
- * Test the file import function
- *
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- * @dataProvider provideFileImportScenarios
- **/
- public function testFileImport($file, $groupname, $group, $mapping)
- {
- $this->wipeOmniDataTables();
- // Create test group
- $groupid = $this->odm->createGroup($groupname, $group);
- // Create a file instance of the data file path
- $dataFile = new File($file);
- // Import the test file into the test group
- $this->odm->fileImport($dataFile, $mapping, $groupid);
- // Fetch the imported data
- $importedData = R::findAll(
- OmniDataManager::TABLE_DATA,
- 'WHERE group_id=?',
- array($groupid)
- );
- $this->assertFalse(is_null($importedData));
- }
- /**
- * Test importing a file without specifying any coordinate data
- *
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- **/
- public function testFileImportWithoutCoordinates()
- {
- $this->markTestIncomplete();
- }
- /**
- * Test importing too many data columns
- *
- * @return void
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- **/
- public function testFileImportTooManyDataColumns($groupid)
- {
- // Expect an OmniDataManagerException
- $this->setExpectedException(
- 'broadnet\broadmap\OmniDataManagerException',
- null,
- OmniDataManager::ERROR_MAPPINGCOLUMNNOTINGROUP
- );
- $mapping = $this->goodMapping;
- $mapping['data']['superflousColumn'] = 'KOMMUNE_NA';
- // Create a file instance of the data file path
- $dataFile = new File($this->goodTestDataFile);
- // Try importing with the erronious mapping array
- $this->odm->fileImport($dataFile, $mapping, $groupid);
- $this->markTestIncomplete();
- }
- /**
- * Test importing too few data columns
- *
- * @return void
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- **/
- public function testFileImportTooFewDataColumns($groupid)
- {
- // Expect an OmniDataManagerException
- $this->setExpectedException(
- 'broadnet\broadmap\OmniDataManagerException',
- null,
- OmniDataManager::ERROR_MAPPINGCOLUMNMISSING
- );
- $mapping = $this->goodMapping;
- array_pop($mapping['data']);
- // Create a file instance of the data file path
- $dataFile = new File($this->goodTestDataFile);
- // Try importing with the erronious mapping array
- $this->odm->fileImport($dataFile, $mapping, $groupid);
- $this->markTestIncomplete();
- }
- /**
- * Tests the fetchGroups function
- *
- * @group omnidatamanager-group-functions
- *
- * @author Tomas Sandven <tomas191191@gmail.com>
- **/
- public function testFetchGroups()
- {
- $groups = $this->odm->fetchGroups();
- var_dump($groups);
- }
- /**
- * Tests the deleteGroup function
- *
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- *
- * @group omnidatamanager-group-functions
- **/
- public function testRemoveGroup()
- {
- $groups = R::findAll(OmniDataManager::TABLE_GROUP);
- foreach($groups as $index => $group)
- {
- $groupid = $group->id;
- // Delete the group
- $this->odm->deleteGroup($group->name);
- // Confirm that the group is gone
- $group = R::load(OmniDataManager::TABLE_GROUP, $groupid);
- $this->assertTrue($group->id === 0);
- // Confirm that all data associated with the group is gone
- $data = R::find(
- OmniDataManager::TABLE_DATA,
- 'ISNULL(group_id) OR group_id=?',
- array($groupid)
- );
- $this->assertTrue(empty($data), sprintf(
- 'All data associated with group "%s" should be gone, but "%d"' .
- ' rows remain',
- $group->name,
- R::count(OmniDataManager::TABLE_DATA, 'group_id=?',
- array($groupid))
- ));
- }
- }
- /**
- * Removes all groups and data
- *
- * @author Tomas Sandven <tomas191191@gmail.com>
- **/
- public static function wipeOmniDataTables()
- {
- // Truncate the OmniDataManager tables
- R::wipe(OmniDataManager::TABLE_GROUP);
- R::wipe(OmniDataManager::TABLE_DATA);
- }
- /**
- * Provider function for the file import testfiles that should succees
- *
- * @return array
- * @author Tomas Sandven <tomas191191@gmail.com>
- **/
- public function provideFileImportScenarios()
- {
- return array(
- array(
- // Filename
- 'tests/testfiles/OmniDataManager_fileImport_utmcoords.csv',
- // Group name
- 'UTMCoords testgroup',
- // Group structure
- array(
- 'Fiberinfo' => OmniDataManager::TYPE_STRING,
- 'Fiberpunkt id' => OmniDataManager::TYPE_STRING
- ),
- // Mapping
- array(
- OmniDataManager::COLUMN_TITLE =>
- array('column', 'Fib_info (eksisterende)'),
- OmniDataManager::COLUMN_UTM_ZONE => array('static', 32),
- OmniDataManager::COLUMN_UTMX =>
- array('column', 'GAB UX_KOORDIN'),
- OmniDataManager::COLUMN_UTMY =>
- array('column', 'GAB UY_KOORDIN'),
- OmniDataManager::COLUMN_DATA => array(
- 'Fiberinfo' =>
- array('column', 'Fib_info (eksisterende)'),
- 'Fiberpunkt id' => array('column', 'Fiberpkt_id')
- )
- )
- ),
- array(
- // Filename
- 'tests/testfiles/OmniDataManager_fileImport_wgscoords.csv',
- // Group name
- 'WGSCoords testgroup',
- // Group structure
- array(
- 'Waypoint navn' => OmniDataManager::TYPE_STRING,
- 'Waypoint description' => OmniDataManager::TYPE_STRING
- ),
- // Mapping
- array(
- OmniDataManager::COLUMN_TITLE => array('column', 'NK-navn'),
- OmniDataManager::COLUMN_LAT => array('column', 'N'),
- OmniDataManager::COLUMN_LNG => array('column', 'E'),
- OmniDataManager::COLUMN_DATA => array(
- 'Waypoint navn' =>
- array('column', 'Wpt Navn'),
- 'Waypoint description' =>
- array('column', 'Wpt Description:')
- )
- )
- )
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement