diff options
author | Danilo Šegan <danilo@segan.org> | 2012-05-31 15:41:49 +0800 |
---|---|---|
committer | Danilo Šegan <danilo@segan.org> | 2012-05-31 15:41:49 +0800 |
commit | cb24a3baf4641226c4238171034c2a10ebf214d9 (patch) | |
tree | 195ec17a8929697260e0790eae7b4bb6fcfd8bda | |
parent | 6e7a890c96a9e3c9f8cd33b8100a307c2ac96c9d (diff) |
Complete parseLine implementation.
-rw-r--r-- | licenses/BuildInfo.php | 18 | ||||
-rw-r--r-- | tests/BuildInfoTest.php | 34 |
2 files changed, 49 insertions, 3 deletions
diff --git a/licenses/BuildInfo.php b/licenses/BuildInfo.php index d5b8fc4..c4ea9e6 100644 --- a/licenses/BuildInfo.php +++ b/licenses/BuildInfo.php @@ -172,10 +172,24 @@ class BuildInfo public function parseLine($line) { $values = explode(":", $line, 2); - if ($values === FALSE || count($values) != 2) { + if ($values === false || count($values) != 2) { throw new InvalidArgumentException("Line is not in the correct format."); } else { - return array($values[0] => $values[1]); + $field = trim($values[0]); + $value = trim($values[1]); + if (!$this->isValidField($field)) { + throw new InvalidArgumentException("Field '$field' not allowed."); + } else { + return array($field => $value); + } + } + } + + public function isValidField($field_name) { + if (in_array($field_name, $this->fields_defined)) { + return true; + } else { + return false; } } } diff --git a/tests/BuildInfoTest.php b/tests/BuildInfoTest.php index d51705c..37c9c38 100644 --- a/tests/BuildInfoTest.php +++ b/tests/BuildInfoTest.php @@ -30,12 +30,44 @@ class BuildInfoTest extends PHPUnit_Framework_TestCase } public function test_parseLine_passes() { - $line = "field:value"; + $line = "Build-Name:value"; + $buildinfo = new BuildInfo(""); + $this->assertEquals(array("Build-Name" => "value"), + $buildinfo->parseLine($line)); + } + + public function test_parseLine_trims() { + $line = "Build-Name: value"; + $buildinfo = new BuildInfo(""); + $this->assertEquals(array("Build-Name" => "value"), + $buildinfo->parseLine($line)); + } + + /** + * @expectedException InvalidArgumentException + */ + public function test_parseLine_invalid_field() { + $line = "field: value"; $buildinfo = new BuildInfo(""); $this->assertEquals(array("field" => "value"), $buildinfo->parseLine($line)); } + public function test_isValidField_true() { + $buildinfo = new BuildInfo(""); + $fields_allowed = array("Format-Version", "Files-Pattern", + "Build-Name", "Theme", "License-Type", "OpenID-Launchpad-Teams", + "Collect-User-Data", "License-Text"); + foreach ($fields_allowed as $field) { + $this->assertTrue($buildinfo->isValidField($field)); + } + } + + public function test_isValidField_false() { + $buildinfo = new BuildInfo(""); + $this->assertFalse($buildinfo->isValidField("Some random text")); + } + /** * Running readFile on a directory returns false. */ |