Skip to content

Commit

Permalink
Added Manager with Factory Support, refs #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Aug 28, 2015
1 parent 9e9923b commit 790f976
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Utils/FactoryManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Art4\JsonApiClient\Utils;

/**
* Manager Interface
*/
interface FactoryManagerInterface
{
/**
* Set a factory into the manager
*
* @param FactoryInterface $factory
* @return object
*/
public function setFactory(FactoryInterface $factory);

/**
* Get a factory from the manager
*
* @return FactoryInterface
*/
public function getFactory();
}
39 changes: 39 additions & 0 deletions src/Utils/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Art4\JsonApiClient\Utils;

class Manager implements ManagerInterface, FactoryManagerInterface
{
/**
* @var FactoryInterface
*/
protected $factory = null;

/**
* Set a factory into the manager
*
* @param FactoryInterface $factory
* @return object
*/
public function setFactory(FactoryInterface $factory)
{
$this->factory = $factory;

return $this;
}

/**
* Get a factory from the manager
*
* @return FactoryInterface
*/
public function getFactory()
{
if ( is_null($this->factory) )
{
$this->setFactory(new Factory);
}

return $this->factory;
}
}
8 changes: 8 additions & 0 deletions src/Utils/ManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Art4\JsonApiClient\Utils;

/**
* Manager Interface
*/
interface ManagerInterface { }
44 changes: 44 additions & 0 deletions tests/unit/Utils/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Art4\JsonApiClient\Utils\Tests;

use Art4\JsonApiClient\Utils\Manager;

class ManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function testSetFactoryReturnsSelf()
{
$factory = $this->getMockBuilder('Art4\JsonApiClient\Utils\Factory')
->getMock();

$manager = new Manager;

$this->assertSame($manager, $manager->setFactory($factory));
}

/**
* @test
*/
public function testGetFactoryReturnsFactoryInterface()
{
$factory = $this->getMockBuilder('Art4\JsonApiClient\Utils\Factory')
->getMock();

$manager = (new Manager)->setFactory($factory);

$this->assertInstanceOf('Art4\JsonApiClient\Utils\FactoryInterface', $manager->getFactory());
}

/**
* @test
*/
public function testGetFactoryWitoutSetReturnsFactoryInterface()
{
$manager = new Manager;

$this->assertInstanceOf('Art4\JsonApiClient\Utils\FactoryInterface', $manager->getFactory());
}
}

0 comments on commit 790f976

Please sign in to comment.