Skip to content

Commit

Permalink
[minor] misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Jul 5, 2020
1 parent 7e5672f commit 21a87ff
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 43 deletions.
1 change: 0 additions & 1 deletion src/Bundle/Maker/MakeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public function configureDependencies(DependencyBuilder $dependencies): void

private function entityChoices(): array
{
// todo remove choices that already have a factory
$choices = [];

foreach ($this->managerRegistry->getManagers() as $manager) {
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Resources/skeleton/Factory.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class <?= $class_name ?> extends ModelFactory
protected function getDefaults(): array
{
return [
// TODO add your default values here (link to docs)
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories)
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Resources/skeleton/Story.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ final class <?= $class_name ?> extends Story
{
public function build(): void
{
// TODO build your story here (link to docs)
// TODO build your story here (https://github.com/zenstruck/foundry#stories)
}
}
2 changes: 1 addition & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ final public static function boot(Configuration $configuration): void
final public static function configuration(): Configuration
{
if (!self::$configuration) {
throw new \RuntimeException('Factory not yet booted.'); // todo
throw new \RuntimeException('Foundry is not yet booted, is the ZenstruckFoundryBundle installed/configured?');
}

return self::$configuration;
Expand Down
1 change: 0 additions & 1 deletion src/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ private function __construct()
*/
final public static function new($defaultAttributes = [], string ...$states): self
{
// todo - is this too magical?
if (\is_string($defaultAttributes)) {
$states = \array_merge([$defaultAttributes], $states);
$defaultAttributes = [];
Expand Down
13 changes: 4 additions & 9 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,16 @@ public function withoutAutoRefresh(callable $callback): self
return $this->isPersisted() ? $this->enableAutoRefresh() : $this;
}

public function assertPersisted(): self
public function assertPersisted(string $message = 'The object is not persisted.'): self
{
// todo improve message
Assert::assertNotNull($this->fetchObject(), 'The object is not persisted.');
Assert::assertNotNull($this->fetchObject(), $message);

return $this;
}

public function assertNotPersisted(): self
public function assertNotPersisted(string $message = 'The object is persisted but it should not be.'): self
{
// todo improve message
Assert::assertNull($this->fetchObject(), 'The object is persisted but it should not be.');
Assert::assertNull($this->fetchObject(), $message);

return $this;
}
Expand All @@ -217,9 +215,6 @@ public function executeCallback(callable $callback, ...$arguments): void
$callback($object, ...$arguments);
}

/**
* Todo - move to RepositoryProxy?
*/
private function fetchObject(): ?object
{
$id = $this->objectManager()->getClassMetadata($this->class)->getIdentifierValues($this->object);
Expand Down
39 changes: 16 additions & 23 deletions src/RepositoryProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,69 +36,62 @@ public function getCount(): int
return \count($this->findAll());
}

public function assertEmpty(): self
public function assertEmpty(string $message = ''): self
{
return $this->assertCount(0);
return $this->assertCount(0, $message);
}

public function assertCount(int $expectedCount): self
public function assertCount(int $expectedCount, string $message = ''): self
{
// todo add message
Assert::assertSame($expectedCount, $this->getCount());
Assert::assertSame($expectedCount, $this->getCount(), $message);

return $this;
}

public function assertCountGreaterThan(int $expected): self
public function assertCountGreaterThan(int $expected, string $message = ''): self
{
// todo add message
Assert::assertGreaterThan($expected, $this->getCount());
Assert::assertGreaterThan($expected, $this->getCount(), $message);

return $this;
}

public function assertCountGreaterThanOrEqual(int $expected): self
public function assertCountGreaterThanOrEqual(int $expected, string $message = ''): self
{
// todo add message
Assert::assertGreaterThanOrEqual($expected, $this->getCount());
Assert::assertGreaterThanOrEqual($expected, $this->getCount(), $message);

return $this;
}

public function assertCountLessThan(int $expected): self
public function assertCountLessThan(int $expected, string $message = ''): self
{
// todo add message
Assert::assertLessThan($expected, $this->getCount());
Assert::assertLessThan($expected, $this->getCount(), $message);

return $this;
}

public function assertCountLessThanOrEqual(int $expected): self
public function assertCountLessThanOrEqual(int $expected, string $message = ''): self
{
// todo add message
Assert::assertLessThanOrEqual($expected, $this->getCount());
Assert::assertLessThanOrEqual($expected, $this->getCount(), $message);

return $this;
}

/**
* @param object|array|mixed $criteria
*/
public function assertExists($criteria): self
public function assertExists($criteria, string $message = ''): self
{
// todo add message
Assert::assertNotNull($this->find($criteria));
Assert::assertNotNull($this->find($criteria), $message);

return $this;
}

/**
* @param object|array|mixed $criteria
*/
public function assertNotExists($criteria): self
public function assertNotExists($criteria, string $message = ''): self
{
// todo add message
Assert::assertNull($this->find($criteria));
Assert::assertNull($this->find($criteria), $message);

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ final public function add(string $name, object $object): self
final public function get(string $name): Proxy
{
if (!\array_key_exists($name, $this->objects)) {
throw new \InvalidArgumentException('explain that object was not registered'); // todo
throw new \InvalidArgumentException(\sprintf('"%s" was not registered. Did you forget to call "%s::add()"?', $name, static::class));
}

return $this->objects[$name];
Expand Down
1 change: 0 additions & 1 deletion src/StoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ private function getOrCreateStory(string $class): Story
}
}

// todo better error if story has constructor argument(s) but not registered as a service
return new $class();
}
}
15 changes: 11 additions & 4 deletions src/Test/TestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Faker;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Zenstruck\Foundry\Configuration;
use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\StoryManager;
Expand Down Expand Up @@ -66,12 +67,18 @@ public static function bootFactory(Configuration $configuration): Configuration
public static function bootFromContainer(ContainerInterface $container): Configuration
{
if (self::$useBundle) {
// todo improve/catch service not found exception - foundry-bundle not installed/configured...
return self::bootFactory($container->get(Configuration::class));
try {
return self::bootFactory($container->get(Configuration::class));
} catch (NotFoundExceptionInterface $e) {
throw new \LogicException('Could not boot Foundry, is the ZenstruckFoundryBundle installed/configured?', 0, $e);
}
}

// todo improve/catch service not found exception - doctrine-bundle not installed/configured...
return self::bootFactory(new Configuration($container->get('doctrine'), new StoryManager([])));
try {
return self::bootFactory(new Configuration($container->get('doctrine'), new StoryManager([])));
} catch (NotFoundExceptionInterface $e) {
throw new \LogicException('Could not boot Foundry, is the DoctrineBundle installed/configured?', 0, $e);
}
}

/**
Expand Down

0 comments on commit 21a87ff

Please sign in to comment.