From 120823e83c1c2d5885419c84fa32692a559dd546 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Sat, 9 Jul 2016 20:22:55 +0200 Subject: [PATCH] CamelCase and more tests. --- lib/Frs/FieldDefinition.php | 22 +++++++++++----------- tests/FieldDefinitionTest.php | 24 ++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/Frs/FieldDefinition.php b/lib/Frs/FieldDefinition.php index 011efdc..0641da6 100644 --- a/lib/Frs/FieldDefinition.php +++ b/lib/Frs/FieldDefinition.php @@ -4,7 +4,7 @@ namespace Frs; class FieldDefinition { - private $field_data = array(); + private $fieldData = array(); /** * @param string $type Type of FieldDefinition (hotel, car, etc.) @@ -12,28 +12,28 @@ class FieldDefinition */ public function __construct($type) { - $definition_file = 'definitions/' . $type . '.json'; - if (!file_exists($definition_file)) { - throw new \Exception('File ' . $definition_file . ' not found!'); + $definitionFile = 'definitions/' . $type . '.json'; + if (!file_exists($definitionFile)) { + throw new \Exception('File ' . $definitionFile . ' not found!'); } - $field_data_json = file_get_contents($definition_file); - $this->field_data = json_decode($field_data_json, true); + $fieldDataJson = file_get_contents($definitionFile); + $this->fieldData = json_decode($fieldDataJson, true); } public function getFieldData() { - return $this->field_data; + return $this->fieldData; } public function getGroups() { - $by_group = array(); - foreach ($this->field_data['groups'] as $id=>$group) { - $by_group[$group] = array( + $byGroup = array(); + foreach ($this->fieldData['groups'] as $id=>$group) { + $byGroup[$group] = array( 'group_name' => $group, 'fields' => array(), ); } - return $by_group; + return $byGroup; } } diff --git a/tests/FieldDefinitionTest.php b/tests/FieldDefinitionTest.php index 17730cf..ed378d6 100644 --- a/tests/FieldDefinitionTest.php +++ b/tests/FieldDefinitionTest.php @@ -2,12 +2,32 @@ namespace Frs\Tests; +use \Frs\FieldDefinition; #use \PHPUnit\Framework\TestCase; class FieldDefinitionTest extends \PHPUnit_Framework_TestCase { - public function testTrue() + public function testLoading() { - $this->assertTrue(true); + $fdo = new FieldDefinition('hotel'); + $fd = $fdo->getFieldData(); + $this->assertArraySubset(array('groups'=>array(), 'fields'=>array()), $fd); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage File definitions/doesnotexist.json not found! + */ + public function testLoadingFailure() + { + $fdo = new FieldDefinition('doesnotexist'); + } + + public function testGroups() + { + $fdo = new FieldDefinition('hotel'); + $byGroup = $fdo->getGroups(); + $this->assertArrayHasKey('Hotel Information', $byGroup); + $this->assertArrayHasKey('Metadata', $byGroup); } }