Browse Source

Merge pull request #9058 from thinkingmedia/thinkingmedia/AssociationCollection

adds case insensitive lookup for AssociaationCollection::type()
Mark Story 9 years ago
parent
commit
62033662b1
2 changed files with 37 additions and 11 deletions
  1. 2 2
      src/ORM/AssociationCollection.php
  2. 35 9
      tests/TestCase/ORM/AssociationCollectionTest.php

+ 2 - 2
src/ORM/AssociationCollection.php

@@ -114,11 +114,11 @@ class AssociationCollection implements IteratorAggregate
      */
     public function type($class)
     {
-        $class = (array)$class;
+        $class = array_map('strtolower', (array)$class);
 
         $out = array_filter($this->_items, function ($assoc) use ($class) {
             list(, $name) = namespaceSplit(get_class($assoc));
-            return in_array($name, $class, true);
+            return in_array(strtolower($name), $class, true);
         });
         return array_values($out);
     }

+ 35 - 9
tests/TestCase/ORM/AssociationCollectionTest.php

@@ -25,6 +25,10 @@ use Cake\TestSuite\TestCase;
  */
 class AssociationCollectionTest extends TestCase
 {
+    /**
+     * @var AssociationCollection
+     */
+    public $associations;
 
     /**
      * setup
@@ -138,11 +142,25 @@ class AssociationCollectionTest extends TestCase
     }
 
     /**
+     *  Data provider for AssociationCollection::type
+     */
+    public function associationCollectionType()
+    {
+        return [
+            ['BelongsTo', 'BelongsToMany'],
+            ['belongsTo', 'belongsToMany'],
+            ['belongsto', 'belongstomany']
+        ];
+    }
+
+    /**
      * Test getting association names by type.
      *
-     * @return void
+     * @param string $belongsToStr
+     * @param string $belongsToManyStr
+     * @dataProvider associationCollectionType
      */
-    public function testType()
+    public function testType($belongsToStr, $belongsToManyStr)
     {
         $belongsTo = new BelongsTo('');
         $this->associations->add('Users', $belongsTo);
@@ -150,13 +168,21 @@ class AssociationCollectionTest extends TestCase
         $belongsToMany = new BelongsToMany('');
         $this->associations->add('Tags', $belongsToMany);
 
-        $this->assertSame([$belongsTo], $this->associations->type('BelongsTo'));
-        $this->assertSame([$belongsToMany], $this->associations->type('BelongsToMany'));
-        $this->assertSame([], $this->associations->type('HasMany'));
-        $this->assertSame(
-            [$belongsTo, $belongsToMany],
-            $this->associations->type(['BelongsTo', 'BelongsToMany'])
-        );
+        $this->assertSame([$belongsTo], $this->associations->type($belongsToStr));
+        $this->assertSame([$belongsToMany], $this->associations->type($belongsToManyStr));
+        $this->assertSame([$belongsTo, $belongsToMany], $this->associations->type([$belongsToStr, $belongsToManyStr]));
+    }
+
+    /**
+     * Type should return empty array.
+     *
+     * @return void
+     */
+    public function hasTypeReturnsEmptyArray()
+    {
+        foreach (['HasMany', 'hasMany', 'FooBar', 'DoesNotExist'] as $value) {
+            $this->assertSame([], $this->associations->type($value));
+        }
     }
 
     /**