Skip to content

Commit

Permalink
adding all the ProjectConfig tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alagroy-42 committed Aug 27, 2020
1 parent 98b1407 commit 0bf6486
Show file tree
Hide file tree
Showing 4 changed files with 1,195 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lizmap/modules/lizmap/lib/Project/ProjectConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function unsetProperty($propName, $propName2 = '')
public function findLayerByAnyName($name)
{
// name null or empty string
if ($name == null || empty($name)) {
if ($name == null || empty($name) || !isset($this->cfgContent->layers)) {
return null;
}

Expand Down Expand Up @@ -305,7 +305,7 @@ public function findLayerByTypeName($typeName)

public function getEditionLayerByName($name)
{
$editionLayers = $this->cfgContent->editionLayers;
$editionLayers = $this->editionLayers;
if ($editionLayers && property_exists($editionLayers, $name)) {
return $editionLayers->{$name};
}
Expand All @@ -320,7 +320,10 @@ public function getEditionLayerByName($name)
*/
public function getEditionLayerByLayerId($layerId)
{
$editionLayers = $this->cfgContent->editionLayers;
$editionLayers = $this->editionLayers;
if (!$editionLayers) {
return null;
}
foreach ($editionLayers as $layer) {
if (!property_exists($layer, 'layerId')) {
continue;
Expand Down
129 changes: 126 additions & 3 deletions tests/units/classes/Project/ProjectConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,149 @@

use Lizmap\Project;

/**
* @internal
* @coversNothing
*/
class projectConfigTest extends PHPUnit_Framework_TestCase
{

public function getConstructData()
{
$file = __DIR__.'/Ressources/events.qgs.cfg';

return array(
array(null, json_decode(file_get_contents($file))),
array(array('cfg' => json_decode(file_get_contents($file))), json_decode(file_get_contents($file)))
array(array('cfgContent' => json_decode(file_get_contents($file))), json_decode(file_get_contents($file))),
);
}

/**
* @dataProvider getConstructData
*
* @param mixed $data
* @param mixed $expectedData
*/
public function testConstruct($data, $expectedData)
{
$file = __DIR__.'/Ressources/events.qgs.cfg';
$testCfg = new Project\ProjectConfig($file, $data);
$this->assertEquals($expectedData, $testCfg->getData());
}
}

public function testConstructCache()
{
$file = __DIR__.'/Ressources/events.qgs.cfg';
$json = json_decode(file_get_contents($file));
$data = array('cfgContent' => $json);
foreach ($json as $key => $prop) {
$data[$key] = $json->{$key};
}
$cachedProperties = array('layersOrder', 'locateByLayer', 'formFilterLayers', 'editionLayers',
'attributeLayers', 'cfgContent', 'options', 'layers', );
$testCfg = new Project\ProjectConfig($file, $data);
foreach ($cachedProperties as $prop) {
if (array_key_exists($prop, $data)) {
$this->assertEquals($data[$prop], $testCfg->getProperty($prop), 'failed Prop = '.$prop);
}
}
}

public function getFindLayerData()
{
$file = __DIR__.'/Ressources/events.qgs.cfg';
$json = json_decode(file_get_contents($file));
$layers = array('cfgContent' => (object) array('layers' => $json->layers));
$layersNull = array('cfgContent' => (object) array('layers' => null));

return array(
array($layers, 'events_4c3b47b8_3939_4c8c_8e91_55bdb13a2101', 'montpellier_events'),
array($layers, 'test_shortname', 'Hidden'),
array($layers, 'Hidden', 'Hidden'),
array($layers, 'osm-test', 'osm-stamen-toner'),
array($layers, 'test', null),
array($layers, null, null),
array($layersNull, 'test', null),
array($layersNull, null, null),
);
}

/**
* @dataProvider getFindLayerData
*
* @param mixed $layers
* @param mixed $key
* @param mixed $layerName
*/
public function testFindLayer($layers, $key, $layerName)
{
$testCfg = new Project\ProjectConfig(null, $layers);
if ($layerName) {
$this->assertSame($testCfg->getProperty('layers')->{$layerName}, $testCfg->findLayerByAnyName($key));
} else {
$this->assertNull($testCfg->findLayerByAnyName($key));
}
}

public function getEditionLayerByNameData()
{
$file = __DIR__.'/Ressources/montpellier.qgs.cfg';
$json = json_decode(file_get_contents($file));
$eLayer = array('editionLayers' => $json->editionLayers);
$eLayerNull = array('editionLayers' => null);

return array(
array($eLayer, 'tramstop'),
array($eLayer, 'tram_stop_work'),
array($eLayerNull, null),
);
}

/**
* @dataProvider getEditionLayerByNameData
*
* @param mixed $eLayers
* @param mixed $name
*/
public function testGetEditionLayerByName($eLayers, $name)
{
$testCfg = new Project\ProjectConfig(null, $eLayers);
if ($name) {
$this->assertSame($testCfg->getProperty('editionLayers')->{$name}, $testCfg->getEditionLayerByName($name));
} else {
$this->assertNull($testCfg->getEditionLayerByName($name));
}
}

public function getEditionLayerByLayerIdData()
{
$file = __DIR__.'/Ressources/montpellier.qgs.cfg';
$json = json_decode(file_get_contents($file));
$eLayer = array('editionLayers' => $json->editionLayers);
$eLayerNull = array('editionLayers' => null);

return array(
array($eLayer, 'edition_line20130409161630329', 'edition_line'),
array($eLayer, 'edition_polygon20130409114333776', 'areas_of_interest'),
array($eLayer, 'null', null),
array($eLayerNull, 'null', null),
array($eLayerNull, null, null),
);
}

/**
* @dataProvider getEditionLayerByLayerIdData
*
* @param mixed $eLayers
* @param mixed $id
* @param mixed $eLayerName
*/
public function testGetEditionLayerByLayerId($eLayers, $id, $eLayerName)
{
$testCfg = new Project\ProjectConfig(null, $eLayers);
if ($eLayerName) {
$this->assertSame($testCfg->getProperty('editionLayers')->{$eLayerName}, $testCfg->getEditionLayerByLayerId($id));
} else {
$this->assertNull($testCfg->getEditionLayerByLayerId($id));
}
}
}
3 changes: 2 additions & 1 deletion tests/units/classes/Project/Ressources/events.qgs.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"Hidden": {
"id": "Hidden",
"name": "Hidden",
"shortname": "test_shortname",
"type": "group",
"title": "Hidden",
"abstract": "",
Expand Down Expand Up @@ -113,7 +114,7 @@
20037508.342789244
],
"crs": "EPSG:3857",
"title": "osm-stamen-toner",
"title": "osm-test",
"abstract": "",
"link": "",
"minScale": 1,
Expand Down
Loading

0 comments on commit 0bf6486

Please sign in to comment.