Skip to content

Commit

Permalink
adds incase lookup for type
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingmedia committed Jun 30, 2016
1 parent fd66542 commit 765064b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/ORM/AssociationCollection.php
Expand Up @@ -114,11 +114,11 @@ public function keys()
*/
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);
}
Expand Down
30 changes: 23 additions & 7 deletions tests/TestCase/ORM/AssociationCollectionTest.php
Expand Up @@ -144,19 +144,35 @@ public function testKeys()
*/
public function testType()
{
$belongsToCases = ['BelongsTo', 'belongsTo', 'belongsto'];
$belongsToManyCases = ['BelongsToMany', 'belongsToMany', 'belongstomany'];

$belongsTo = new BelongsTo('');
$this->associations->add('Users', $belongsTo);

$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'])
);
foreach ($belongsToCases as $belongsToType) {
$this->assertSame(
[$belongsTo],
$this->associations->type($belongsToType)
);
foreach ($belongsToManyCases as $belongsToManyType) {
$this->assertSame(
[$belongsToMany],
$this->associations->type($belongsToManyType)
);
$this->assertSame(
[$belongsTo, $belongsToMany],
$this->associations->type([$belongsToType, $belongsToManyType])
);
}
}

foreach (['HasMany', 'hasMany', 'FooBar', 'DoesNotExist'] as $value) {
$this->assertSame([], $this->associations->type($value));
}
}

/**
Expand Down

0 comments on commit 765064b

Please sign in to comment.