Skip to content

Commit

Permalink
Implement Populator (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Jul 27, 2016
1 parent f961428 commit 3a90253
Show file tree
Hide file tree
Showing 48 changed files with 407 additions and 280 deletions.
15 changes: 3 additions & 12 deletions fixtures/Nelmio/Alice/Definition/Fixture/DummyFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ class DummyFixture implements FixtureInterface
/**
* @var string
*/
private $reference;
private $id;

public function __construct(string $reference)
public function __construct(string $id)
{
$this->id = __CLASS__.'#'.$reference;
$this->reference = $reference;
$this->id = $id;
}

/**
Expand All @@ -38,14 +37,6 @@ public function getId(): string
return $this->id;
}

/**
* @inheritdoc
*/
public function getReference(): string
{
return $this->reference;
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 0 additions & 8 deletions fixtures/Nelmio/Alice/Definition/Fixture/FakeFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ public function getId(): string
$this->__call();
}

/**
* @inheritdoc
*/
public function getReference(): string
{
$this->__call();
}

/**
* @inheritdoc
*/
Expand Down
30 changes: 30 additions & 0 deletions fixtures/Nelmio/Alice/Generator/Hydrator/FakeHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\Alice\Generator\Hydrator;

use Nelmio\Alice\Definition\Property;
use Nelmio\Alice\Generator\HydratorInterface;
use Nelmio\Alice\NotCallableTrait;
use Nelmio\Alice\ObjectInterface;

class FakeHydrator implements HydratorInterface
{
use NotCallableTrait;

/**
* @inheritdoc
*/
public function hydrate(ObjectInterface $object, Property $property): ObjectInterface
{
$this->__call();
}
}
8 changes: 0 additions & 8 deletions src/Nelmio/Alice/Definition/Fixture/FixtureWithFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ public function getId(): string
{
return $this->fixture->getId();
}

/**
* @inheritdoc
*/
public function getReference(): string
{
return $this->fixture->getReference();
}

/**
* @inheritdoc
Expand Down
18 changes: 2 additions & 16 deletions src/Nelmio/Alice/Definition/Fixture/SimpleFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ final class SimpleFixture implements FixtureInterface
*/
private $id;

/**
* @var string
*/
private $reference;

/**
* @var string
*/
Expand All @@ -39,11 +34,10 @@ final class SimpleFixture implements FixtureInterface
*/
private $specs;

public function __construct(string $reference, string $className, SpecificationBag $specs)
public function __construct(string $id, string $className, SpecificationBag $specs)
{
$this->id = $className.'#'.$reference;
$this->id = $id;
$this->className = $className;
$this->reference = $reference;
$this->specs = $specs;
}

Expand All @@ -55,14 +49,6 @@ public function getId(): string
return $this->id;
}

/**
* @inheritdoc
*/
public function getReference(): string
{
return $this->reference;
}

/**
* @inheritdoc
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Nelmio/Alice/Definition/Fixture/Templating.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(FixtureWithFlags $fixture)

if ($flag instanceof ExtendFlag) {
// Potential flag duplication is handled at the flagbag level
$this->extends[] = $flag->getExtendedFixture()->createAbsoluteFrom($fixture);
$this->extends[] = $flag->getExtendedFixture();
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/Nelmio/Alice/Definition/Fixture/TemplatingFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ public function getId(): string
return $this->fixture->getId();
}

/**
* @inheritdoc
*/
public function getReference(): string
{
return $this->fixture->getReference();
}

/**
* @inheritdoc
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Nelmio/Alice/Definition/Flag/ExtendFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class ExtendFlag implements FlagInterface
public function __construct(FixtureReference $extendedFixture)
{
$this->extendedFixture = $extendedFixture;
$this->stringValue = 'extends '.$extendedFixture->getReference();
$this->stringValue = 'extends '.$extendedFixture->getId();
}

public function getExtendedFixture(): FixtureReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(ServiceReferenceInterface $caller, string $method, a
$this->caller = $caller;
$this->method = $method;
$this->arguments = $arguments;
$this->stringValue = $caller->getReference().$method;
$this->stringValue = $caller->getId().$method;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Nelmio/Alice/Definition/Object/SimpleObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,17 @@ public function __construct(string $reference, $instance)
$this->reference = $reference;
$this->instance = $instance;
}


/**
* @param \object $newInstance
*
* @return SimpleObject
*/
public function withInstance($newInstance): self
{
return new self($this->reference, $newInstance);
}

public function getReference(): string
{
return $this->reference;
Expand All @@ -56,7 +66,7 @@ public function getInstance()
{
return clone $this->instance;
}

public function __clone()
{
$this->instance = clone $this->instance;
Expand Down
10 changes: 10 additions & 0 deletions src/Nelmio/Alice/Definition/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public function __construct(string $name, $value)
$this->value = $value;
}

/**
* param ValueInterface|mixed $value
*
* @return self
*/
public function withValue($value): self
{
return new self($this->name, $value);
}

public function getName(): string
{
return $this->name;
Expand Down
12 changes: 11 additions & 1 deletion src/Nelmio/Alice/Definition/PropertyBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Nelmio\Alice\Definition;

final class PropertyBag
use Traversable;

final class PropertyBag implements \IteratorAggregate
{
/**
* @var Property[]
Expand Down Expand Up @@ -43,4 +45,12 @@ public function mergeWith(self $propertyBag): self

return $clone;
}

/**
* @inheritdoc
*/
public function getIterator()
{
return new \ArrayIterator(array_values($this->properties));
}
}
43 changes: 7 additions & 36 deletions src/Nelmio/Alice/Definition/ServiceReference/FixtureReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,27 @@
namespace Nelmio\Alice\Definition\ServiceReference;

use Nelmio\Alice\Definition\ServiceReferenceInterface;
use Nelmio\Alice\FixtureInterface;

/**
* Value object to point to refer to a fixture. The reference can be relative, e.g. 'user_base' (fixture reference) or
* absolute e.g. 'Nelmio\Alice\User#user_base' (fixture ID).
* Value object to point to refer to a fixture.
*/
final class FixtureReference implements ServiceReferenceInterface
{
/**
* @var string
*/
private $reference;
private $id;

/**
* @param string $reference
* @param string $fixtureId
*/
public function __construct(string $reference)
public function __construct(string $fixtureId)
{
$this->reference = $reference;
$this->id = $fixtureId;
}

/**
* A fixture reference may be relative, e.g. 'user_base' (fixture reference). By giving it a fixture, this method
* creates a new reference which will be absolute, e.g. 'Nelmio\Alice\User#user_base' (fixture ID.
*
* @param FixtureInterface $fixture
*
* @return self
*/
public function createAbsoluteFrom(FixtureInterface $fixture): self
{
if (false !== strpos($this->reference, '#')) {
throw new \BadMethodCallException(
sprintf(
'Attempted to make the reference "%s" absolute from the fixture of ID "%s", however the reference '.
'is already absolute.',
$this->reference,
$fixture->getId()
)
);
}

$clone = clone $this;
$clone->reference = $fixture->getClassName().'#'.$this->reference;

return $clone;
}

public function getReference(): string
public function getId(): string
{
return $this->reference;
return $this->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(string $reference)
$this->reference = $reference;
}

public function getReference(): string
public function getId(): string
{
return $this->reference;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(string $reference)
$this->reference = $reference;
}

public function getReference(): string
public function getId(): string
{
return $this->reference;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nelmio/Alice/Definition/ServiceReferenceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
*/
interface ServiceReferenceInterface
{
public function getReference(): string;
public function getId(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function create(FixtureInterface $fixture, \Throwable $previous)
return new static(
sprintf(
'Could no instantiate fixture "%s".',
$fixture->getReference()
$fixture->getId()
),
0,
$previous
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function create(FixtureInterface $fixture): self
return new static(
sprintf(
'No suitable instantiator found for the fixture "%s".',
$fixture->getReference()
$fixture->getId()
)
);
}
Expand Down
8 changes: 1 addition & 7 deletions src/Nelmio/Alice/FixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@
*/
interface FixtureInterface
{
/**
* @return string Unique across a whole fixture set, mainly used to build unique values. By default is
* 'className#reference'
*/
public function getId(): string;

/**
* @return string e.g. 'dummy0'. May contain flags depending of the implementation.
*/
public function getReference(): string;
public function getId(): string;

/**
* @return string FQCN. May contain flags depending of the implementation.
Expand Down
Loading

0 comments on commit 3a90253

Please sign in to comment.