Skip to content

Commit

Permalink
Rebrand Factories
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Jan 7, 2022
1 parent f4612e0 commit 9e5055d
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 142 deletions.
4 changes: 2 additions & 2 deletions src/BaseManager.php → src/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Throwable;
use UnexpectedValueException;

abstract class BaseManager
abstract class BaseFactory
{
/**
* The configuration.
Expand Down Expand Up @@ -334,7 +334,7 @@ private function discoverHandlers(): void
}
}

ksort($this->discovered, SORT_STRING);
ksort($this->discovered, SORT_STRING);

// Cache the results
$this->commitCache();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php

namespace Tatter\Handlers\Managers;
namespace Tatter\Handlers\Factories;

use Tatter\Handlers\BaseManager;
use Tatter\Handlers\BaseFactory;
use Tatter\Handlers\Interfaces\HandlerInterface;

/**
* Manager Manager Class
* Factory Factory Class
*
* Used to discover other Manager classes
* Used to discover other Factory classes
* which are set up as handlers themselves.
*/
class ManagerManager extends BaseManager implements HandlerInterface
class FactoryFactory extends BaseFactory implements HandlerInterface
{
public static function handlerId(): string
{
return 'managers';
return 'factories';
}

public static function attributes(): array
{
return [
'name' => 'Manager of Managers',
'name' => 'Factory of Factories',
];
}

Expand All @@ -30,6 +30,6 @@ public static function attributes(): array
*/
public function getPath(): string
{
return 'Managers';
return 'Factories';
}
}
62 changes: 31 additions & 31 deletions tests/BaseManagerTest.php → tests/BaseFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

use Tatter\Handlers\BaseManager;
use Tatter\Handlers\BaseFactory;
use Tatter\Handlers\Config\Handlers as HandlersConfig;
use Tests\Support\Managers\FactoryManager;
use Tests\Support\Factories\CarFactory;
use Tests\Support\TestCase;

/**
* @internal
*/
final class BaseManagerTest extends TestCase
final class BaseFactoryTest extends TestCase
{
public function testInvalidPathThrows()
{
$this->expectException('UnexpectedValueException');
$this->expectExceptionMessage('Invalid path provided: ');

new class () extends BaseManager {
new class () extends BaseFactory {
public function getPath(): string
{
return '';
Expand All @@ -25,43 +25,43 @@ public function getPath(): string

public function testNoDiscoveryReturnsEmptyArray()
{
$manager = new class () extends BaseManager {
$factory = new class () extends BaseFactory {
public function getPath(): string
{
return 'Bananas';
}
};

$this->assertSame([], $manager->findAll());
$this->assertSame([], $factory->findAll());
}

public function testGetConfigReturnsConfig()
{
$result = $this->manager->getConfig();
$result = $this->factory->getConfig();

$this->assertInstanceOf(HandlersConfig::class, $result);
$this->assertNull($result->cacheDuration);
}

public function testGetPathReturnsPath()
{
$result = $this->manager->getPath();
$result = $this->factory->getPath();

$this->assertSame('Factories', $result);
$this->assertSame('Cars', $result);
}

public function testWhereCombinesFilters()
{
$this->manager->where(['group' => 'East']);
$this->factory->where(['group' => 'East']);

$result = $this->getPrivateProperty($this->manager, 'filters');
$result = $this->getPrivateProperty($this->factory, 'filters');
$this->assertSame($result, [
['group', '==', 'East', true],
]);

$this->manager->where(['uid' => 'pop']);
$this->factory->where(['uid' => 'pop']);

$result = $this->getPrivateProperty($this->manager, 'filters');
$result = $this->getPrivateProperty($this->factory, 'filters');
$this->assertSame($result, [
['group', '==', 'East', true],
['uid', '==', 'pop', true],
Expand All @@ -70,48 +70,48 @@ public function testWhereCombinesFilters()

public function testResetClearsFilters()
{
$this->manager->where(['group' => 'East']);
$this->manager->reset();
$this->factory->where(['group' => 'East']);
$this->factory->reset();

$result = $this->getPrivateProperty($this->manager, 'filters');
$result = $this->getPrivateProperty($this->factory, 'filters');
$this->assertSame($result, []);
}

public function testGetHandlerClassReturnsClass()
{
$expected = 'Tests\Support\Factories\WidgetFactory';
$expected = 'Tests\Support\Cars\WidgetCar';

$file = realpath(SUPPORTPATH . 'Factories/WidgetFactory.php');
$result = $this->manager->getHandlerClass($file, 'Tests\Support');
$file = realpath(SUPPORTPATH . 'Cars/WidgetCar.php');
$result = $this->factory->getHandlerClass($file, 'Tests\Support');

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

public function testGetHandlerClassRequiresPhpExtension()
{
$result = $this->manager->getHandlerClass('foo', 'bar');
$result = $this->factory->getHandlerClass('foo', 'bar');

$this->assertNull($result);
}

public function testGetHandlerClassRequiresInterfaces()
{
$result = $this->manager->getHandlerClass(SUPPORTPATH . 'Factories/NotFactory.php', 'Tests\Support');
$result = $this->factory->getHandlerClass(SUPPORTPATH . 'Cars/NotCar.php', 'Tests\Support');

$this->assertNull($result);
}

public function testGetHandlerClassRequiresHandlerInterface()
{
$result = $this->manager->getHandlerClass(SUPPORTPATH . 'Factories/BadFactory.php', 'Tests\Support');
$result = $this->factory->getHandlerClass(SUPPORTPATH . 'Cars/BadCar.php', 'Tests\Support');

$this->assertNull($result);
}

public function testGetHandlerClassFails()
{
$file = realpath(SUPPORTPATH . 'Factories/WidgetFactory.php');
$result = $this->manager->getHandlerClass($file, 'Foo\Bar');
$file = realpath(SUPPORTPATH . 'Cars/WidgetCar.php');
$result = $this->factory->getHandlerClass($file, 'Foo\Bar');

$this->assertNull($result);
}
Expand All @@ -120,36 +120,36 @@ public function testFilterHandlersRespectsLimit()
{
$limit = 1;

$method = $this->getPrivateMethodInvoker($this->manager, 'filterHandlers');
$method = $this->getPrivateMethodInvoker($this->factory, 'filterHandlers');
$result = $method($limit);

$this->assertCount($limit, $result);
}

public function testGetAttributesReturnsAttributes()
{
$result = $this->manager->getAttributes('widget');
$result = $this->factory->getAttributes('widget');

$this->assertIsArray($result);
$this->assertSame('Widget Plant', $result['name']);
}

public function testGetAttributesReturnsNull()
{
$result = $this->manager->getAttributes('Imaginary\Handler\Class');
$result = $this->factory->getAttributes('Imaginary\Handler\Class');

$this->assertNull($result);
}

public function testIgnoresClass()
{
$expected = ['Tests\Support\Factories\WidgetFactory'];
$expected = ['Tests\Support\Cars\WidgetCar'];

$config = config('Handlers');
$config->ignoredClasses = ['Tests\Support\Factories\PopFactory'];
$this->manager = new FactoryManager($config);
$config->ignoredClasses = ['Tests\Support\Cars\PopCar'];
$this->factory = new CarFactory($config);

$result = $this->manager->findAll();
$result = $this->factory->findAll();

$this->assertSame($expected, $result);
}
Expand Down
22 changes: 11 additions & 11 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Tests\Support\Managers\FactoryManager;
use Tests\Support\Factories\CarFactory;
use Tests\Support\TestCase;

/**
Expand All @@ -12,7 +12,7 @@ protected function tearDown(): void
{
parent::tearDown();

$this->manager->clearCache();
$this->factory->clearCache();
}

public function testDiscoveryUsesCache()
Expand All @@ -22,12 +22,12 @@ public function testDiscoveryUsesCache()
$config->cacheDuration = MINUTE;

$class = 'Foo\Bar\Baz';
cache()->save('handlers-factories', [
cache()->save('handlers-cars', [
'bam' => ['id' => 'bam', 'class' => $class],
]);

$this->manager = new FactoryManager($config);
$result = $this->manager->first();
$this->factory = new CarFactory($config);
$result = $this->factory->first();

$this->assertSame($class, $result); // @phpstan-ignore-line
}
Expand All @@ -38,23 +38,23 @@ public function testDiscoveryCreatesCache()
$config = config('Handlers');
$config->cacheDuration = MINUTE;

$this->manager = new FactoryManager($config);
$this->factory = new CarFactory($config);

$result = cache()->get('handlers-factories');
$result = cache()->get('handlers-cars');

$this->assertCount(2, $result);
$this->assertSame('Tests\Support\Factories\PopFactory', $result['pop']['class']);
$this->assertSame('Tests\Support\Cars\PopCar', $result['pop']['class']);
}

public function testDiscoveryIgnoresCache()
{
$expected = 'Tests\Support\Factories\PopFactory';
$expected = 'Tests\Support\Cars\PopCar';

cache()->save('handlers-factories', [
cache()->save('handlers-cars', [
'Foo\Bar\Baz' => ['name' => 'foobar'],
]);

$result = $this->manager->first();
$result = $this->factory->first();

$this->assertSame($expected, $result);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/FactoryFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Tatter\Handlers\Factories\FactoryFactory;
use Tests\Support\Factories\CarFactory;
use Tests\Support\TestCase;

/**
* @internal
*/
final class FactoryFactoryTest extends TestCase
{
public function testDiscovers()
{
// Discovery is alphabetical by handlerId
$expected = [
CarFactory::class,
FactoryFactory::class,
];

$factory = new FactoryFactory();
$result = $factory->findAll();

$this->assertSame($expected, $result);
}
}
25 changes: 0 additions & 25 deletions tests/ManagerManagerTest.php

This file was deleted.

0 comments on commit 9e5055d

Please sign in to comment.