Skip to content

Commit

Permalink
Merge pull request #8 from tattersoftware/named
Browse files Browse the repository at this point in the history
Named lookup
  • Loading branch information
MGatner authored Sep 17, 2020
2 parents 3bf7893 + 2a38578 commit 975ac0e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/Commands/HandlersList.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class HandlersList extends BaseCommand
{
protected $group = 'Handlers';
protected $group = 'Housekeeping';
protected $name = 'handlers:list';
protected $description = 'List all discovered handlers';
protected $usage = 'handlers:list';
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/HandlersRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class HandlersRegister extends BaseCommand
{
protected $group = 'Handlers';
protected $group = 'Housekeeping';
protected $name = 'handlers:register';
protected $description = 'Regsiter all discovered handlers';
protected $usage = 'handlers:register';
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/HandlersReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class HandlersReset extends BaseCommand
{
protected $group = 'Handlers';
protected $group = 'Housekeeping';
protected $name = 'handlers:reset';
protected $description = 'Clear cached versions of discovered handlers';
protected $usage = 'handlers:reset';
Expand Down
49 changes: 49 additions & 0 deletions src/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public function setPath(string $path): self
return $this;
}

//--------------------------------------------------------------------

/**
* Adds attribute criteria.
*
Expand Down Expand Up @@ -147,6 +149,53 @@ public function all(): array
return $classes;
}

/**
* Returns a handler with a given name. Ignores filters.
* Searches: attribute "name" or "uid", namespaced class, and short class name.
*
* @param string $name The name of the handler
*
* @return string|null The full class name, or null if none found
*/
public function named(string $name): ?string
{
$this->discoverHandlers();

$name = trim($name, '\\ ');

foreach ($this->discovered as $class => $attributes)
{
// Check the namespaced class
if ($class === $name)
{
return $class;
}

// Check the attributes
if (isset($attributes['name']) && $attributes['name'] === $name)
{
return $class;
}
if (isset($attributes['uid']) && $attributes['uid'] === $name)
{
return $class;
}

// Check the class shortname
if ($pos = strrpos($class, '\\'))
{
if (substr($class, $pos + 1) === $name)
{
return $class;
}
}
}

return null;
}

//--------------------------------------------------------------------

/**
* Iterates through discovered handlers and attempts to register them.
*
Expand Down
61 changes: 0 additions & 61 deletions tests/unit/LibraryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,67 +79,6 @@ public function testFilterHandlersRespectsLimit()
$this->assertCount($limit, $result);
}

public function testAllDiscoversAll()
{
$expected = [
'Tests\Support\Factories\PopFactory',
'Tests\Support\Factories\WidgetFactory',
];

$result = $this->handlers->all();

$this->assertEquals($expected, $result);
}

public function testAllRespectsCriteria()
{
$expected = [
'Tests\Support\Factories\WidgetFactory',
];

$result = $this->handlers->where(['uid' => 'widget'])->all();

$this->assertEquals($expected, $result);
}

public function testAllResetsCriteria()
{
$this->handlers->where(['uid' => 'widget'])->all();

$result = $this->getPrivateProperty($this->handlers, 'criteria');

$this->assertEquals([], $result);
}

public function testFirstReturnsSingleton()
{
$expected = 'Tests\Support\Factories\PopFactory';

$result = $this->handlers->first();

$this->assertEquals($expected, $result);
}

public function testFirstRespectsCriteria()
{
$expected = [
'Tests\Support\Factories\WidgetFactory',
];

$result = $this->handlers->where(['uid' => 'widget'])->all();

$this->assertEquals($expected, $result);
}

public function testFirstResetsCriteria()
{
$this->handlers->where(['uid' => 'widget'])->first();

$result = $this->getPrivateProperty($this->handlers, 'criteria');

$this->assertEquals([], $result);
}

public function testRegisterCallsHandlerRegister()
{
$this->handlers->register();
Expand Down
98 changes: 98 additions & 0 deletions tests/unit/SearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

use Tatter\Handlers\BaseHandler;
use Tatter\Handlers\Handlers;
use Tatter\Handlers\Config\Handlers as HandlersConfig;
use Tests\Support\HandlerTestCase;

class SearchTest extends HandlerTestCase
{
public function testAllDiscoversAll()
{
$expected = [
'Tests\Support\Factories\PopFactory',
'Tests\Support\Factories\WidgetFactory',
];

$result = $this->handlers->all();

$this->assertEquals($expected, $result);
}

public function testAllRespectsCriteria()
{
$expected = [
'Tests\Support\Factories\WidgetFactory',
];

$result = $this->handlers->where(['uid' => 'widget'])->all();

$this->assertEquals($expected, $result);
}

public function testAllResetsCriteria()
{
$this->handlers->where(['uid' => 'widget'])->all();

$result = $this->getPrivateProperty($this->handlers, 'criteria');

$this->assertEquals([], $result);
}

public function testFirstReturnsSingleton()
{
$expected = 'Tests\Support\Factories\PopFactory';

$result = $this->handlers->first();

$this->assertEquals($expected, $result);
}

public function testFirstRespectsCriteria()
{
$expected = [
'Tests\Support\Factories\WidgetFactory',
];

$result = $this->handlers->where(['uid' => 'widget'])->all();

$this->assertEquals($expected, $result);
}

public function testFirstResetsCriteria()
{
$this->handlers->where(['uid' => 'widget'])->first();

$result = $this->getPrivateProperty($this->handlers, 'criteria');

$this->assertEquals([], $result);
}

//--------------------------------------------------------------------

/**
* @dataProvider provideNames
*/
public function testNamedFindsMatch($name, $success)
{
$result = $this->handlers->named($name);

$this->assertEquals($success, (bool) $result);
}

public function provideNames()
{
return [
['', false],
[' ', false],
['pop', true],
['PopFactory', true],
['Pop Factory', true],
['Bad Factory', false],
['Not A Factory', false],
['Tests\\Support\\Factories', false],
['Tests\\Support\Factories\\PopFactory', true],
['\\Tests\\Support\\Factories\\PopFactory', true],
];
}
}

0 comments on commit 975ac0e

Please sign in to comment.