From 58a8ff80e311a9e4579e09e46c0b9eedc6404aca Mon Sep 17 00:00:00 2001 From: Tom Klingenberg Date: Sun, 13 May 2012 00:41:15 +0200 Subject: [PATCH] Cont. code simplification & better tests. --- src/Composer/Util/SpdxLicenseIdentifier.php | 11 ++++++----- .../Test/Util/SpdxLicenseIdentifierTest.php | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Composer/Util/SpdxLicenseIdentifier.php b/src/Composer/Util/SpdxLicenseIdentifier.php index 1ef896460..02525bb24 100644 --- a/src/Composer/Util/SpdxLicenseIdentifier.php +++ b/src/Composer/Util/SpdxLicenseIdentifier.php @@ -40,18 +40,19 @@ class SpdxLicenseIdentifier public function validate($license) { if (is_array($license)) { - $license = count($license) > 1 ? '('.implode(' or ', $license).')' : reset($license); + $count = count($license); + if ($count !== count(array_filter($license, 'is_string'))) { + throw new \InvalidArgumentException('Array of strings expected.'); + } + $license = $count > 1 ? '('.implode(' or ', $license).')' : (string) reset($license); } if (!is_string($license)) { throw new \InvalidArgumentException(sprintf( 'Array or String expected, %s given.', gettype($license) )); } - if (!$this->isValidLicenseString($license)) { - return false; - } - return true; + return $this->isValidLicenseString($license); } /** diff --git a/tests/Composer/Test/Util/SpdxLicenseIdentifierTest.php b/tests/Composer/Test/Util/SpdxLicenseIdentifierTest.php index 9e4d1dd3b..4b461d31f 100644 --- a/tests/Composer/Test/Util/SpdxLicenseIdentifierTest.php +++ b/tests/Composer/Test/Util/SpdxLicenseIdentifierTest.php @@ -32,6 +32,7 @@ class SpdxLicenseIdentifierTest extends TestCase { return array( array(""), + array(array()), array("The system pwns you"), array("()"), array("(MIT)"), @@ -50,6 +51,17 @@ class SpdxLicenseIdentifierTest extends TestCase ); } + public static function provideInvalidArgument() + { + return array( + array(null), + array(new \stdClass), + array(array(new \stdClass)), + array(array("mixed", new \stdClass)), + array(array(new \stdClass, new \stdClass)), + ); + } + /** * @dataProvider provideValidLicenses * @param $license @@ -71,11 +83,12 @@ class SpdxLicenseIdentifierTest extends TestCase } /** + * @dataProvider provideInvalidArgument * @expectedException InvalidArgumentException */ - public function testInvalidArgument() + public function testInvalidArgument($invalidArgument) { $validator = new SpdxLicenseIdentifier(); - $validator->validate(null); + $validator->validate($invalidArgument); } } \ No newline at end of file