Skip to content

Commit

Permalink
Experimental usage of Builder Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Sep 7, 2016
1 parent 3fd85ce commit 11bd973
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 25 deletions.
13 changes: 10 additions & 3 deletions src/Builder/AbstractBuilder.php
Expand Up @@ -2,8 +2,8 @@

namespace AvalancheDevelopment\Approach\Builder;

use AvalancheDevelopment\Approach\BuilderFactory;
use AvalancheDevelopment\Approach\SchemaObjectFactory;

use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;

Expand All @@ -12,14 +12,21 @@ abstract class AbstractBuilder

use LoggerAwareTrait;

/** @var BuilderFactory */
protected $builderFactory;

/** @var SchemaObjectFactory */
protected $schemaObjectFactory;

/**
* @param BuilderFactory $builderFactory
* @param SchemaObjectFactory $schemaObjectFactory
*/
public function __construct(SchemaObjectFactory $schemaObjectFactory)
{
public function __construct(
BuilderFactory $builderFactory,
SchemaObjectFactory $schemaObjectFactory
) {
$this->builderFactory = $builderFactory;
$this->schemaObjectFactory = $schemaObjectFactory;
$this->logger = new NullLogger;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/Info.php
Expand Up @@ -58,7 +58,7 @@ protected function buildContact(array $data)
return;
}

return (new Contact)($data['contact']);
return $this->builderFactory->newBuilder('Contact')($data['contact']);
}

/**
Expand All @@ -71,6 +71,6 @@ protected function buildLicense(array $data)
return;
}

return (new License)($data['license']);
return $this->builderFactory->newBuilder('License')($data['license']);
}
}
2 changes: 1 addition & 1 deletion src/BuilderFactory.php
Expand Up @@ -30,7 +30,7 @@ public function __construct(SchemaObjectFactory $schemaObjectFactory)
public function newBuilder($className)
{
$fullPath = $this->resolvePath($className);
$builder = new $fullPath($schemaObjectFactory); // todo error handling
$builder = new $fullPath($this, $schemaObjectFactory); // todo error handling
$builder->setLogger($this->logger);
return $builder;
}
Expand Down
16 changes: 13 additions & 3 deletions tests/unit/src/Builder/AbstractBuilderTest.php
Expand Up @@ -2,30 +2,40 @@

namespace AvalancheDevelopment\Approach\Builder;

use AvalancheDevelopment\Approach\BuilderFactory;
use AvalancheDevelopment\Approach\SchemaObjectFactory;
use PHPUnit_Framework_TestCase;
use Psr\Log\NullLogger;

class AbstractBuilderTest extends PHPUnit_Framework_TestCase
{

public function testConstructSetsSchemaObjectFactory()
public function testConstructSetsFactories()
{
$builderFactory = $this->createMock(BuilderFactory::class);
$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);

$abstractBuilder = $this->getMockBuilder(AbstractBuilder::class)
->setConstructorArgs([ $schemaObjectFactory ])
->setConstructorArgs([
$builderFactory,
$schemaObjectFactory,
])
->getMock();

$this->assertAttributeSame($builderFactory, 'builderFactory', $abstractBuilder);
$this->assertAttributeSame($schemaObjectFactory, 'schemaObjectFactory', $abstractBuilder);
}

public function testConstructSetsNullLogger()
{
$builderFactory = $this->createMock(BuilderFactory::class);
$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);

$abstractBuilder = $this->getMockBuilder(AbstractBuilder::class)
->setConstructorArgs([ $schemaObjectFactory ])
->setConstructorArgs([
$builderFactory,
$schemaObjectFactory,
])
->getMock();

$this->assertAttributeInstanceOf(NullLogger::class, 'logger', $abstractBuilder);
Expand Down
35 changes: 26 additions & 9 deletions tests/unit/src/Builder/ContactTest.php
Expand Up @@ -2,6 +2,7 @@

namespace AvalancheDevelopment\Approach\Builder;

use AvalancheDevelopment\Approach\BuilderFactory;
use AvalancheDevelopment\Approach\Schema\Contact as ContactObject;
use AvalancheDevelopment\Approach\SchemaObjectFactory;
use PHPUnit_Framework_TestCase;
Expand All @@ -12,18 +13,20 @@ class ContactTest extends PHPUnit_Framework_TestCase

public function testContactImplementsBuilderInterface()
{
$builderFactory = $this->createMock(BuilderFactory::class);
$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);

$this->assertInstanceOf(BuilderInterface::class, $contactBuilder);
}

public function testContactImplementsLoggerInterface()
{
$builderFactory = $this->createMock(BuilderFactory::class);
$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);

$this->assertInstanceOf(LoggerAwareInterface::class, $contactBuilder);
}
Expand All @@ -34,12 +37,14 @@ public function testInvokeDoesNotSetNameIfEmpty()
$contactObject->expects($this->never())
->method('setName');

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('Contact')
->willReturn($contactObject);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);
$contactBuilder([]);
}

Expand All @@ -52,12 +57,14 @@ public function testInvokeSetsNameIfNotEmpty()
->method('setName')
->with($name);

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('Contact')
->willReturn($contactObject);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);
$contactBuilder([ 'name' => $name ]);
}

Expand All @@ -67,12 +74,14 @@ public function testInvokeDoesNotSetUrlIfEmpty()
$contactObject->expects($this->never())
->method('setUrl');

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('Contact')
->willReturn($contactObject);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);
$contactBuilder([]);
}

Expand All @@ -85,12 +94,14 @@ public function testInvokeSetsUrlIfNotEmpty()
->method('setUrl')
->with($url);

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('Contact')
->willReturn($contactObject);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);
$contactBuilder([ 'url' => $url ]);
}

Expand All @@ -100,12 +111,14 @@ public function testInvokeDoesNotSetEmailIfEmpty()
$contactObject->expects($this->never())
->method('setEmail');

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('Contact')
->willReturn($contactObject);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);
$contactBuilder([]);
}

Expand All @@ -118,25 +131,29 @@ public function testInvokeSetsEmailIfNotEmpty()
->method('setEmail')
->with($email);

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('Contact')
->willReturn($contactObject);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);
$contactBuilder([ 'email' => $email ]);
}

public function testInvokeReturnsContactObject()
{
$contactObject = $this->createMock(ContactObject::class);

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('Contact')
->willReturn($contactObject);

$contactBuilder = new Contact($schemaObjectFactory);
$contactBuilder = new Contact($builderFactory, $schemaObjectFactory);
$result = $contactBuilder([]);

$this->assertSame($result, $contactObject);
Expand Down
27 changes: 20 additions & 7 deletions tests/unit/src/Builder/LicenseTest.php
Expand Up @@ -2,6 +2,7 @@

namespace AvalancheDevelopment\Approach\Builder;

use AvalancheDevelopment\Approach\BuilderFactory;
use AvalancheDevelopment\Approach\Schema\License as LicenseObject;
use AvalancheDevelopment\Approach\SchemaObjectFactory;
use PHPUnit_Framework_TestCase;
Expand All @@ -13,18 +14,20 @@ class LicenseTest extends PHPUnit_Framework_TestCase

public function testLicenseImplementsBuilderInterface()
{
$builderFactory = $this->createMock(BuilderFactory::class);
$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);

$licenseBuilder = new License($schemaObjectFactory);
$licenseBuilder = new License($builderFactory, $schemaObjectFactory);

$this->assertInstanceOf(BuilderInterface::class, $licenseBuilder);
}

public function testLicenseImplementsLoggerInterface()
{
$builderFactory = $this->createMock(BuilderFactory::class);
$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);

$licenseBuilder = new License($schemaObjectFactory);
$licenseBuilder = new License($builderFactory, $schemaObjectFactory);

$this->assertInstanceOf(LoggerAwareInterface::class, $licenseBuilder);
}
Expand All @@ -40,12 +43,14 @@ public function testInvokeBailsIfNameIsEmpty()
$licenseObject->expects($this->never())
->method('setName');

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('License')
->willReturn($licenseObject);

$licenseBuilder = new License($schemaObjectFactory);
$licenseBuilder = new License($builderFactory, $schemaObjectFactory);
$licenseBuilder->setLogger($logger);
$result = $licenseBuilder([]);

Expand All @@ -61,12 +66,14 @@ public function testInvokeSetsNameIfNotEmpty()
->method('setName')
->with($name);

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('License')
->willReturn($licenseObject);

$licenseBuilder = new License($schemaObjectFactory);
$licenseBuilder = new License($builderFactory, $schemaObjectFactory);
$licenseBuilder([ 'name' => $name ]);
}

Expand All @@ -76,12 +83,14 @@ public function testInvokeDoesNotSetUrlIfEmpty()
$licenseObject->expects($this->never())
->method('setUrl');

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('License')
->willReturn($licenseObject);

$licenseBuilder = new License($schemaObjectFactory);
$licenseBuilder = new License($builderFactory, $schemaObjectFactory);
$licenseBuilder([ 'name' => 'Apache 2.0' ]);
}

Expand All @@ -92,12 +101,14 @@ public function testInvokeSetsUrlIfNotEmpty()
->method('setUrl')
->with('http://domain.tld');

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('License')
->willReturn($licenseObject);

$licenseBuilder = new License($schemaObjectFactory);
$licenseBuilder = new License($builderFactory, $schemaObjectFactory);
$licenseBuilder([
'name' => 'Apache 2.0',
'url' => 'http://domain.tld'
Expand All @@ -108,12 +119,14 @@ public function testInvokeReturnsLicenseObject()
{
$licenseObject = $this->createMock(LicenseObject::class);

$builderFactory = $this->createMock(BuilderFactory::class);

$schemaObjectFactory = $this->createMock(SchemaObjectFactory::class);
$schemaObjectFactory->method('newSchemaObject')
->with('License')
->willReturn($licenseObject);

$licenseBuilder = new License($schemaObjectFactory);
$licenseBuilder = new License($builderFactory, $schemaObjectFactory);
$result = $licenseBuilder([ 'name' => 'Apache 2.0' ]);

$this->assertSame($result, $licenseObject);
Expand Down

0 comments on commit 11bd973

Please sign in to comment.