[REFACTOR] reorganize namespaces for factories

This commit is contained in:
Marco Stoll 2019-06-21 10:36:57 +02:00
parent 588a41abfe
commit fc1d028b6a
11 changed files with 307 additions and 307 deletions

View File

@ -8,17 +8,16 @@
*/
declare(strict_types=1);
namespace FF\Services\Factories;
namespace FF\Factories;
use FF\Events\AbstractEvent;
use FF\Factories\AbstractFactory;
use FF\Factories\ClassLocators\BaseNamespaceClassLocator;
use FF\Factories\ClassLocators\ClassLocatorInterface;
/**
* Class EventsFactory
*
* @package FF\Services\Factories
* @package FF\Factories
*/
class EventsFactory extends AbstractFactory
{

View File

@ -8,7 +8,7 @@
*/
declare(strict_types=1);
namespace FF\Services\Factories;
namespace FF\Factories;
/**
* Class SF

View File

@ -8,9 +8,8 @@
*/
declare(strict_types=1);
namespace FF\Services\Factories;
namespace FF\Factories;
use FF\Factories\AbstractSingletonFactory;
use FF\Factories\ClassLocators\BaseNamespaceClassLocator;
use FF\Factories\ClassLocators\ClassLocatorInterface;
use FF\Factories\Exceptions\ClassNotFoundException;
@ -20,7 +19,7 @@ use FF\Services\Exceptions\ConfigurationException;
/**
* Class ServicesFactory
*
* @package FF\Services\Factories
* @package FF\Factories
*/
class ServicesFactory extends AbstractSingletonFactory
{

View File

@ -8,13 +8,14 @@
*/
declare(strict_types=1);
namespace FF\Services;
namespace FF\Services\Events;
use FF\DataStructures\IndexedCollection;
use FF\DataStructures\OrderedCollection;
use FF\Events\AbstractEvent;
use FF\Factories\EventsFactory;
use FF\Factories\Exceptions\ClassNotFoundException;
use FF\Services\Factories\EventsFactory;
use FF\Services\AbstractService;
/**
* Class EventBroker

View File

@ -10,8 +10,8 @@ declare(strict_types=1);
namespace FF\Services\Traits;
use FF\Services\EventBroker;
use FF\Services\Factories\SF;
use FF\Factories\SF;
use FF\Services\Events\EventBroker;
/**
* Trait EventEmitterTrait
@ -35,7 +35,7 @@ trait EventEmitterTrait
static $eventBroker = null;
if (is_null($eventBroker)) {
$eventBroker = SF::i()->get('EventBroker');
$eventBroker = SF::i()->get('Events\EventBroker');
}
$eventBroker->fire($classIdentifier, ...$args);

View File

@ -10,8 +10,8 @@ declare(strict_types=1);
namespace FF\Services\Traits;
use FF\Factories\SF;
use FF\Services\AbstractService;
use FF\Services\Factories\SF;
/**
* Trait ServiceLocatorTrait

View File

@ -8,10 +8,10 @@
*/
declare(strict_types=1);
namespace FF\Tests\Services\Factories;
namespace FF\Tests\Factories;
use FF\Factories\ClassLocators\BaseNamespaceClassLocator;
use FF\Services\Factories\EventsFactory;
use FF\Factories\EventsFactory;
use PHPUnit\Framework\TestCase;
/**

View File

@ -8,10 +8,10 @@
*/
declare(strict_types=1);
namespace FF\Tests\Services;
namespace FF\Tests\Factories;
use FF\Services\Factories\ServicesFactory;
use FF\Services\Factories\SF;
use FF\Factories\ServicesFactory;
use FF\Factories\SF;
use PHPUnit\Framework\TestCase;
/**

View File

@ -8,12 +8,12 @@
*/
declare(strict_types=1);
namespace FF\Tests\Services\Factories {
namespace FF\Tests\Factories {
use FF\Factories\ClassLocators\BaseNamespaceClassLocator;
use FF\Factories\Exceptions\ClassNotFoundException;
use FF\Factories\ServicesFactory;
use FF\Services\Exceptions\ConfigurationException;
use FF\Services\Factories\ServicesFactory;
use FF\Tests\Services\ServiceOne;
use FF\Tests\Services\ServiceTwo;
use PHPUnit\Framework\TestCase;

View File

@ -1,287 +0,0 @@
<?php
/**
* Definition of EventBrokerTest
*
* @author Marco Stoll <marco@fast-forward-encoding.de>
* @copyright 2019-forever Marco Stoll
* @filesource
*/
declare(strict_types=1);
namespace FF\Tests\Services {
use FF\DataStructures\IndexedCollection;
use FF\DataStructures\OrderedCollection;
use FF\Services\EventBroker;
use FF\Services\Factories\EventsFactory;
use FF\Tests\Services\Events\EventA;
use FF\Tests\Services\Events\EventB;
use PHPUnit\Framework\TestCase;
/**
* Test EventBrokerTest
*
* @package FF\Tests
*/
class EventBrokerTest extends TestCase
{
/**
* @var EventBroker
*/
protected $uut;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$this->uut = new EventBroker();
$this->uut->getEventsFactory()
->getClassLocator()
->prependNamespaces(__NAMESPACE__);
}
/**
* {@inheritdoc}
*/
protected function tearDown(): void
{
$this->uut->unsubscribeAll('EventA');
}
/**
* Tests the namesake method/feature
*/
public function testGetEventsFactory()
{
$this->assertInstanceOf(EventsFactory::class, $this->uut->getEventsFactory());
}
/**
* Tests the namesake method/feature
*/
public function testGetSubscriptions()
{
$this->assertInstanceOf(IndexedCollection::class, $this->uut->getSubscriptions());
}
/**
* Tests the namesake method/feature
*/
public function testGetSubscribers()
{
$this->assertInstanceOf(OrderedCollection::class, $this->uut->getSubscribers('EventA'));
}
/**
* Tests the namesake method/feature
*/
public function testSubscribe()
{
$callback = [new ListenerA(), 'shoutA'];
$same = $this->uut->subscribe($callback, 'EventA');
$this->assertSame($this->uut, $same);
$this->assertEquals(1, count($this->uut->getSubscribers('EventA')));
$this->assertSame($callback, $this->uut->getSubscribers('EventA')->getFirst());
}
/**
* Tests the namesake method/feature
*/
public function testSubscribeRepeated()
{
$callback = [new ListenerA(), 'shoutA'];
$this->uut->subscribe($callback, 'EventA')->subscribe($callback, 'EventA');
$this->assertEquals(1, count($this->uut->getSubscribers('EventA')));
}
/**
* Tests the namesake method/feature
*/
public function testSubscribeAppend()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutA'];
$this->uut->subscribe($callback1, 'EventA')
->subscribe($callback2, 'EventA');
$this->assertSame($callback1, $this->uut->getSubscribers('EventA')->get(0));
$this->assertSame($callback2, $this->uut->getSubscribers('EventA')->get(1));
}
/**
* Tests the namesake method/feature
*/
public function testSubscribeFirst()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutA'];
$same = $this->uut->subscribe($callback1, 'EventA')
->subscribeFirst($callback2, 'EventA');
$this->assertSame($this->uut, $same);
$this->assertSame($callback1, $this->uut->getSubscribers('EventA')->get(1));
$this->assertSame($callback2, $this->uut->getSubscribers('EventA')->get(0));
}
/**
* Tests the namesake method/feature
*/
public function testUnsubscribe()
{
$callback1 = [new ListenerA(), 'shoutA'];
$this->uut->subscribe($callback1, 'EventA')
->subscribe($callback1, 'EventB')
->unsubscribe($callback1);
$this->assertTrue($this->uut->getSubscribers('EventA')->isEmpty());
$this->assertTrue($this->uut->getSubscribers('EventB')->isEmpty());
}
/**
* Tests the namesake method/feature
*/
public function testUnsubscribeNamed()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutA'];
$same = $this->uut->subscribe($callback1, 'EventA')
->subscribe($callback2, 'EventA')
->unsubscribe($callback1, 'EventA');
$this->assertSame($this->uut, $same);
$this->assertEquals(1, count($this->uut->getSubscribers('EventA')));
$this->assertSame($callback2, $this->uut->getSubscribers('EventA')->get(0));
}
/**
* Tests the namesake method/feature
*/
public function testUnsubscribeAll()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutA'];
$same = $this->uut->subscribe($callback1, 'EventA')
->subscribe($callback2, 'EventA')
->unsubscribeAll('EventA');
$this->assertSame($this->uut, $same);
$this->assertTrue($this->uut->getSubscribers('EventA')->isEmpty());
}
/**
* Tests the namesake method/feature
*/
public function testHasSubscribers()
{
$callback1 = [new ListenerA(), 'shoutA'];
$this->uut->subscribe($callback1, 'EventA');
$this->assertTrue($this->uut->hasSubscribers('EventA'));
$this->assertFalse($this->uut->hasSubscribers('EventB'));
}
/**
* Tests the namesake method/feature
*/
public function testIsSubscribed()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutB'];
$this->uut->subscribe($callback1, 'EventA');
$this->assertTrue($this->uut->isSubscribed($callback1, 'EventA'));
$this->assertFalse($this->uut->isSubscribed($callback1, 'EventB'));
$this->assertFalse($this->uut->isSubscribed($callback2, 'EventA'));
$this->assertFalse($this->uut->isSubscribed($callback2, 'EventB'));
}
/**
* Tests the namesake method/feature
*/
public function testFire()
{
$same = $this->uut->fire('EventA', 'foo');
$this->assertSame($this->uut, $same);
}
/**
* Tests the namesake method/feature
*/
public function testFireWithListener()
{
$this->expectOutputString('foo');
$this->uut->subscribe([new ListenerA(), 'shoutA'], 'EventA')
->fire('EventA', 'foo');
}
/**
* Tests the namesake method/feature
*/
public function testFireCancel()
{
$this->expectOutputString('foo'); // output would be 'foofoo' if event canceling does not work
$this->uut->subscribe([new ListenerA(), 'cancelA'], 'EventA')
->subscribe([new ListenerB(), 'neverToReach'], 'EventA')
->fire('EventA', 'foo');
}
}
class ListenerA
{
public function shoutA(EventA $event)
{
print $event->content;
}
public function shoutB(EventB $event)
{
print $event->content;
}
public function cancelA(EventA $event)
{
print $event->content;
$event->cancel();
}
}
class ListenerB extends ListenerA
{
public function shoutB(EventB $event)
{
print $event->content;
}
public function neverToReach(EventA $event)
{
print $event->content;
}
}
}
namespace FF\Tests\Services\Events {
use FF\Events\AbstractEvent;
class EventA extends AbstractEvent
{
public $content;
public function __construct(string $content)
{
$this->content = $content;
}
}
class EventB extends EventA
{
}
}

View File

@ -0,0 +1,288 @@
<?php
/**
* Definition of EventBrokerTest
*
* @author Marco Stoll <marco@fast-forward-encoding.de>
* @copyright 2019-forever Marco Stoll
* @filesource
*/
declare(strict_types=1);
namespace FF\Tests\Services\Events;
use FF\DataStructures\IndexedCollection;
use FF\DataStructures\OrderedCollection;
use FF\Events\AbstractEvent;
use FF\Factories\EventsFactory;
use FF\Services\Events\EventBroker;
use PHPUnit\Framework\TestCase;
/**
* Test EventBrokerTest
*
* @package FF\Tests
*/
class EventBrokerTest extends TestCase
{
/**
* @var EventBroker
*/
protected $uut;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$this->uut = new EventBroker();
$this->uut->getEventsFactory()
->getClassLocator()
->prependNamespaces('FF\Tests\Services');
}
/**
* {@inheritdoc}
*/
protected function tearDown(): void
{
$this->uut->unsubscribeAll('EventA');
}
/**
* Tests the namesake method/feature
*/
public function testGetEventsFactory()
{
$this->assertInstanceOf(EventsFactory::class, $this->uut->getEventsFactory());
}
/**
* Tests the namesake method/feature
*/
public function testGetSubscriptions()
{
$this->assertInstanceOf(IndexedCollection::class, $this->uut->getSubscriptions());
}
/**
* Tests the namesake method/feature
*/
public function testGetSubscribers()
{
$this->assertInstanceOf(OrderedCollection::class, $this->uut->getSubscribers('EventA'));
}
/**
* Tests the namesake method/feature
*/
public function testSubscribe()
{
$callback = [new ListenerA(), 'shoutA'];
$same = $this->uut->subscribe($callback, 'EventA');
$this->assertSame($this->uut, $same);
$this->assertEquals(1, count($this->uut->getSubscribers('EventA')));
$this->assertSame($callback, $this->uut->getSubscribers('EventA')->getFirst());
}
/**
* Tests the namesake method/feature
*/
public function testSubscribeRepeated()
{
$callback = [new ListenerA(), 'shoutA'];
$this->uut->subscribe($callback, 'EventA')->subscribe($callback, 'EventA');
$this->assertEquals(1, count($this->uut->getSubscribers('EventA')));
}
/**
* Tests the namesake method/feature
*/
public function testSubscribeAppend()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutA'];
$this->uut->subscribe($callback1, 'EventA')
->subscribe($callback2, 'EventA');
$this->assertSame($callback1, $this->uut->getSubscribers('EventA')->get(0));
$this->assertSame($callback2, $this->uut->getSubscribers('EventA')->get(1));
}
/**
* Tests the namesake method/feature
*/
public function testSubscribeFirst()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutA'];
$same = $this->uut->subscribe($callback1, 'EventA')
->subscribeFirst($callback2, 'EventA');
$this->assertSame($this->uut, $same);
$this->assertSame($callback1, $this->uut->getSubscribers('EventA')->get(1));
$this->assertSame($callback2, $this->uut->getSubscribers('EventA')->get(0));
}
/**
* Tests the namesake method/feature
*/
public function testUnsubscribe()
{
$callback1 = [new ListenerA(), 'shoutA'];
$this->uut->subscribe($callback1, 'EventA')
->subscribe($callback1, 'EventB')
->unsubscribe($callback1);
$this->assertTrue($this->uut->getSubscribers('EventA')->isEmpty());
$this->assertTrue($this->uut->getSubscribers('EventB')->isEmpty());
}
/**
* Tests the namesake method/feature
*/
public function testUnsubscribeNamed()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutA'];
$same = $this->uut->subscribe($callback1, 'EventA')
->subscribe($callback2, 'EventA')
->unsubscribe($callback1, 'EventA');
$this->assertSame($this->uut, $same);
$this->assertEquals(1, count($this->uut->getSubscribers('EventA')));
$this->assertSame($callback2, $this->uut->getSubscribers('EventA')->get(0));
}
/**
* Tests the namesake method/feature
*/
public function testUnsubscribeAll()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutA'];
$same = $this->uut->subscribe($callback1, 'EventA')
->subscribe($callback2, 'EventA')
->unsubscribeAll('EventA');
$this->assertSame($this->uut, $same);
$this->assertTrue($this->uut->getSubscribers('EventA')->isEmpty());
}
/**
* Tests the namesake method/feature
*/
public function testHasSubscribers()
{
$callback1 = [new ListenerA(), 'shoutA'];
$this->uut->subscribe($callback1, 'EventA');
$this->assertTrue($this->uut->hasSubscribers('EventA'));
$this->assertFalse($this->uut->hasSubscribers('EventB'));
}
/**
* Tests the namesake method/feature
*/
public function testIsSubscribed()
{
$callback1 = [new ListenerA(), 'shoutA'];
$callback2 = [new ListenerA(), 'shoutB'];
$this->uut->subscribe($callback1, 'EventA');
$this->assertTrue($this->uut->isSubscribed($callback1, 'EventA'));
$this->assertFalse($this->uut->isSubscribed($callback1, 'EventB'));
$this->assertFalse($this->uut->isSubscribed($callback2, 'EventA'));
$this->assertFalse($this->uut->isSubscribed($callback2, 'EventB'));
}
/**
* Tests the namesake method/feature
*/
public function testFire()
{
$same = $this->uut->fire('EventA', 'foo');
$this->assertSame($this->uut, $same);
}
/**
* Tests the namesake method/feature
*/
public function testFireWithListener()
{
$this->expectOutputString('foo');
$this->uut->subscribe([new ListenerA(), 'shoutA'], 'EventA')
->fire('EventA', 'foo');
}
/**
* Tests the namesake method/feature
*/
public function testFireCancel()
{
$this->expectOutputString('foo'); // output would be 'foofoo' if event canceling does not work
$this->uut->subscribe([new ListenerA(), 'cancelA'], 'EventA')
->subscribe([new ListenerB(), 'neverToReach'], 'EventA')
->fire('EventA', 'foo');
}
/**
* Tests the namesake method/feature
*/
public function testGetClassIdentifier()
{
$this->assertEquals('Events\EventBroker', EventBroker::getClassIdentifier());
}
}
class ListenerA
{
public function shoutA(EventA $event)
{
print $event->content;
}
public function shoutB(EventB $event)
{
print $event->content;
}
public function cancelA(EventA $event)
{
print $event->content;
$event->cancel();
}
}
class ListenerB extends ListenerA
{
public function shoutB(EventB $event)
{
print $event->content;
}
public function neverToReach(EventA $event)
{
print $event->content;
}
}
class EventA extends AbstractEvent
{
public $content;
public function __construct(string $content)
{
$this->content = $content;
}
}
class EventB extends EventA
{
}