Browse Source

Only accept strings in parseDsn

Jose Lorenzo Rodriguez 11 years ago
parent
commit
a1079c23d1
3 changed files with 19 additions and 17 deletions
  1. 2 1
      src/Cache/Cache.php
  2. 7 12
      src/Core/StaticConfigTrait.php
  3. 10 4
      tests/TestCase/Core/StaticConfigTraitTest.php

+ 2 - 1
src/Cache/Cache.php

@@ -479,7 +479,8 @@ class Cache {
 	}
 
 /**
- * Returns an array mapping url schemes to fully qualified class names
+ * Returns an array mapping url schemes to fully qualified caching engine
+ * class names.
  *
  * @return array
  */

+ 7 - 12
src/Core/StaticConfigTrait.php

@@ -15,6 +15,7 @@
 namespace Cake\Core;
 
 use BadMethodCallException;
+use InvalidArgumentException;
 use UnexpectedValueException;
 
 /**
@@ -140,22 +141,16 @@ trait StaticConfigTrait {
  * The following is an example of its usage:
  *
  * {{{
- *   $dsn = 'Cake\Database\Driver\Mysql://localhost/database?className=Cake\Database\Connection';
- *   $config = ConnectionManager::parseDsn($dsn);
- *
- *   $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
- *   $config = ConnectionManager::parseDsn($dsn);
- *
- *   $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
+ *   $dsn = 'mysql://user:pass@localhost/database?';
  *   $config = ConnectionManager::parseDsn($dsn);
  *
  *   $dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS';
  * 	 $config = Log::parseDsn($dsn);
  *
- *   $dsn = 'Mail://user:secret@localhost:25?timeout=30&client=null&tls=null';
+ *   $dsn = 'smtp://user:secret@localhost:25?timeout=30&client=null&tls=null';
  * 	 $config = Email::parseDsn($dsn);
  *
- *   $dsn = 'File:///';
+ *   $dsn = 'file:///?className=\My\Cache\Engine\FileEngine';
  * 	 $config = Cache::parseDsn($dsn);
  *
  *   $dsn = 'File://?prefix=myapp_cake_core_&serialize=true&duration=+2 minutes&path=/tmp/persistent/';
@@ -163,13 +158,13 @@ trait StaticConfigTrait {
  *
  * }}
  *
- * For all classes, the value of `scheme` is set as the value of both the `className` and `driver`
+ * For all classes, the value of `scheme` is set as the value of both the `className`
  * unless they have been otherwise specified.
  *
  * Note that querystring arguments are also parsed and set as values in the returned configuration.
  *
  * @param string $dsn The DSN string to convert to a configuration array
- * @return mixed null when adding configuration and an array of configuration data when reading.
+ * @return array The configuration array to be stored after parsing the DSN
  */
 	public static function parseDsn($dsn) {
 		if (empty($dsn)) {
@@ -177,7 +172,7 @@ trait StaticConfigTrait {
 		}
 
 		if (!is_string($dsn)) {
-			return $dsn;
+			throw new InvalidArgumentException('Only strings can be passed to parseDsn');
 		}
 
 		if (preg_match("/^([\w\\\]+)/", $dsn, $matches)) {

+ 10 - 4
tests/TestCase/Core/StaticConfigTraitTest.php

@@ -133,11 +133,17 @@ class StaticConfigTraitTest extends TestCase {
 		$klassName = get_class($this->subject);
 
 		$this->assertSame([], $klassName::parseDsn(''));
-		$this->assertInternalType('array', $klassName::parseDsn(['url' => 'http://:80']));
-		$this->assertEquals(['url' => 'http://:80'], $klassName::parseDsn(['url' => 'http://:80']));
+	}
 
-		$this->assertInternalType('array', $klassName::parseDsn(['url' => 'http://user@:80']));
-		$this->assertEquals(['url' => 'http://user@:80'], $klassName::parseDsn(['url' => 'http://user@:80']));
+/**
+ * Tests that failing to pass a string to parseDsn will throw an exception
+ *
+ * @expectedException InvalidArgumentException
+ * @return void
+ */
+	public function testParseBadType() {
+		$klassName = get_class($this->subject);
+		$klassName::parseDsn(['url' => 'http://:80']);
 	}
 
 	public function testCustomParseDsn() {