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 function __construct()
- {
- // Truncate the OmniDataManager tables
- R::wipe(OmniDataManager::TABLE_GROUP);
- R::wipe(OmniDataManager::TABLE_DATA);
- }
- public function __destruct()
- {
- // Truncate the OmniDataManager tables
- R::wipe(OmniDataManager::TABLE_GROUP);
- R::wipe(OmniDataManager::TABLE_DATA);
- }
- // For every test create a new OmniDataManager
- public function setUp()
- {
- $this->odm = new OmniDataManager();
- }
- /**
- * Test the createGroup function
- *
- * @return void
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @dataProvider provideFileImportTests_good
- **/
- 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));
- return $id;
- }
- /**
- * Test the file import function
- *
- * @return void
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- * @dataProvider provideFileImportTests_good
- **/
- public function testFileImport($t)
- {
- // Create a file instance of the data file path
- $dataFile = new File($t['file']);
- // Import the test file into the test group
- $this->odm->fileImport($dataFile, $t['mapping'], $groupid);
- }
- /**
- * Test importing too many data columns
- *
- * @return void
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- **/
- public function testFileImportTooManyColumns($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);
- }
- /**
- * Test importing too few data columns
- *
- * @return void
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- **/
- public function testFileImportTooFewColumns($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);
- }
- /**
- * Tests the deleteGroup function
- *
- * @return void
- * @author Tomas Sandven <tomas191191@gmail.com>
- *
- * @depends testCreateGroup
- * @depends testFileImport
- **/
- public function testRemoveGroup($id)
- {
- $group = R::load(OmniDataManager::TABLE_GROUP, $id);
- // Group must exist
- $this->assertFalse($group->id === 0);
- // Delete the group
- $this->odm->deleteGroup($group->name);
- // Confirm that the group is gone
- $group = R::load(OmniDataManager::TABLE_GROUP, $id);
- $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($id)
- );
- $this->assertTrue(empty($data), sprintf(
- "All data associated with test group should be gone, but there\n" .
- "are %d such data rows left in the table (%d total)",
- count($data),
- R::count(OmniDataManager::TABLE_DATA)
- ));
- }
- /**
- * Provider function for the file import testfiles that should succees
- *
- * @return array
- * @author Tomas Sandven <tomas191191@gmail.com>
- **/
- public function provideFileImportTests_good()
- {
- $files = glob('tests/testfiles/OmniDataManager_fileImport_good*.csv');
- $groupNames = array('Testgroup 1', 'Testgroup 2');
- $groups = array(
- array(
- 'Fiberinfo' => OmniDataManager::TYPE_STRING,
- 'Fiberpunkt id' => OmniDataManager::TYPE_STRING
- ),
- array(
- 'Waypoint navn' => OmniDataManager::TYPE_STRING,
- 'Waypoint description' => OmniDataManager::TYPE_STRING
- )
- );
- $mappings = array(
- 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(
- 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' => 'Wpt Navn',
- 'Waypoint description' => 'Wpt Description:'
- )
- )
- );
- $output = array();
- foreach($files as $index => $file)
- {
- $output[] = array(
- $file, $groupNames[$index], $groups[$index], $mappings[$index]
- );
- }
- return $output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement