Skip to content

Commit 2e3b7d5

Browse files
committed
minor(behat): rename exceptions without suffix
1 parent 1123a7f commit 2e3b7d5

29 files changed

+237
-127
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@
2525
/.grepai
2626
/.specify
2727
/specs
28+
.github/agents
29+
.github/prompts
30+
.vscode

src/Command/LoadFixturesCommand.php

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,14 @@ public function __construct(
4343
protected function configure(): void
4444
{
4545
$this
46-
->addArgument('name', InputArgument::OPTIONAL, 'The name of the story to load.')
46+
->addArgument('name', InputArgument::OPTIONAL, "Story's name or stories group's name to load.")
4747
->addOption('append', 'a', InputOption::VALUE_NONE, 'Skip resetting database and append data to the existing database.')
4848
;
4949
}
5050

5151
protected function execute(InputInterface $input, OutputInterface $output): int
5252
{
53-
$allFixtures = $this->fixtureStoryResolver->availableFixtureNames();
54-
55-
if (0 === \count($allFixtures)) {
53+
if (!$this->fixtureStoryResolver->hasAnyFixtures()) {
5654
throw new LogicException('No story as fixture available: add attribute #[AsFixture] to your story classes before running this command.');
5755
}
5856

@@ -68,37 +66,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6866
$this->resetDatabase();
6967
}
7068

71-
$stories = [];
72-
73-
if (null === ($name = $input->getArgument('name'))) {
74-
if (1 === \count($allFixtures)) {
75-
$name = $this->fixtureStoryResolver->availableFixtureNames()[0];
76-
} else {
77-
$storyNames = $this->fixtureStoryResolver->availableFixtureNames();
78-
if (\count($this->fixtureStoryResolver->availableGroupNames()) > 0) {
79-
$storyNames[] = '(choose a group of stories...)';
80-
}
81-
$name = $io->choice('Choose a story to load:', $storyNames);
82-
}
69+
$fixtureNameOrGroup = $input->getArgument('name') ?? $this->getNameWhenNotProvided($io);
8370

84-
if (!$this->fixtureStoryResolver->hasFixture($name)) {
85-
$groupsNames = $this->fixtureStoryResolver->availableGroupNames();
86-
$name = $io->choice('Choose a group of stories:', $groupsNames);
87-
}
88-
}
71+
$stories = $this->fixtureStoryResolver->resolve($fixtureNameOrGroup);
8972

90-
if ($this->fixtureStoryResolver->hasFixture($name)) {
91-
$io->comment("Loading story with name \"{$name}\"...");
92-
$stories = [$name => $this->fixtureStoryResolver->resolve($name)];
93-
}
94-
95-
if ($this->fixtureStoryResolver->hasGroup($name)) {
96-
$io->comment("Loading stories group \"{$name}\"...");
97-
$stories = $this->fixtureStoryResolver->resolveGroup($name);
73+
if (!$stories) {
74+
throw new InvalidArgumentException("Story with name or group \"{$fixtureNameOrGroup}\" does not exist.");
9875
}
9976

100-
if (!$stories) {
101-
throw new InvalidArgumentException("Story with name \"{$name}\" does not exist.");
77+
if ($this->fixtureStoryResolver->hasFixture($fixtureNameOrGroup)) {
78+
$io->comment("Loading story with name \"{$fixtureNameOrGroup}\"...");
79+
} else {
80+
$io->comment("Loading stories group \"{$fixtureNameOrGroup}\"...");
10281
}
10382

10483
foreach ($stories as $name => $storyClass) {
@@ -125,4 +104,24 @@ private function resetDatabase(): void
125104
$databaseResetter->resetBeforeFirstTest($this->kernel);
126105
}
127106
}
107+
108+
private function getNameWhenNotProvided(SymfonyStyle $io): string
109+
{
110+
if ($this->fixtureStoryResolver->hasOnlyOneFixture()) {
111+
return $this->fixtureStoryResolver->availableFixtureNames()[0];
112+
}
113+
114+
$storyNames = $this->fixtureStoryResolver->availableFixtureNames();
115+
if (\count($this->fixtureStoryResolver->availableGroupNames()) > 0) {
116+
$storyNames[] = '(choose a group of stories...)';
117+
}
118+
$name = $io->choice('Choose a story to load:', $storyNames);
119+
120+
if (!$this->fixtureStoryResolver->hasFixture($name)) {
121+
$groupsNames = $this->fixtureStoryResolver->availableGroupNames();
122+
$name = $io->choice('Choose a group of stories:', $groupsNames);
123+
}
124+
125+
return $name;
126+
}
128127
}

src/Story/FixtureStoryNotFoundException.php renamed to src/Story/FixtureStoryNotFound.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
*
1717
* @author Nicolas PHILIPPE <nikophil@gmail.com>
1818
*/
19-
final class FixtureStoryNotFoundException extends \RuntimeException
19+
final class FixtureStoryNotFound extends \RuntimeException
2020
{
2121
/**
2222
* @param list<string> $availableFixtures
2323
*/
24-
public static function forName(string $fixtureName, array $availableFixtures): self
24+
public static function forNameOrGroup(string $fixtureName, array $availableFixtures): self
2525
{
26-
$message = "Fixture story \"{$fixtureName}\" not found:";
26+
$message = "Fixture story with name or group \"{$fixtureName}\" not found:";
2727

2828
if ($availableFixtures) {
2929
$message .= ' Available fixtures: '.\implode(', ', $availableFixtures);

src/Story/FixtureStoryResolver.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,39 @@ public function __construct(
2929
}
3030

3131
/**
32-
* @return class-string<Story>
32+
* @return array<string, class-string<Story>>
3333
*
34-
* @throws FixtureStoryNotFoundException
34+
* @throws FixtureStoryNotFound
3535
*/
36-
public function resolve(string $fixtureName): string
36+
public function resolve(string $fixtureOrGroupName): array
3737
{
38-
if (!isset($this->fixtureStories[$fixtureName])) {
39-
throw FixtureStoryNotFoundException::forName($fixtureName, $this->availableFixtureNames());
38+
if ($this->hasFixture($fixtureOrGroupName)) {
39+
return [$fixtureOrGroupName => $this->fixtureStories[$fixtureOrGroupName]];
40+
}
41+
42+
if ($this->hasGroup($fixtureOrGroupName)) {
43+
return $this->resolveGroup($fixtureOrGroupName);
4044
}
4145

42-
return $this->fixtureStories[$fixtureName];
46+
throw FixtureStoryNotFound::forNameOrGroup(
47+
$fixtureOrGroupName,
48+
[...$this->availableFixtureNames(), ...$this->availableGroupNames()]
49+
);
4350
}
4451

45-
/**
46-
* @return array<string, class-string<Story>>
47-
*
48-
* @throws FixtureStoryNotFoundException
49-
*/
50-
public function resolveGroup(string $groupName): array
52+
public function hasAnyFixtures(): bool
5153
{
52-
if (!isset($this->groupedStories[$groupName])) {
53-
throw FixtureStoryNotFoundException::forGroup($groupName, $this->availableGroupNames());
54-
}
55-
56-
return $this->groupedStories[$groupName];
54+
return count($this->fixtureStories) > 0;
5755
}
5856

5957
public function hasFixture(string $name): bool
6058
{
6159
return isset($this->fixtureStories[$name]);
6260
}
6361

64-
public function hasGroup(string $name): bool
62+
public function hasOnlyOneFixture(): bool
6563
{
66-
return isset($this->groupedStories[$name]);
64+
return count($this->fixtureStories) === 1;
6765
}
6866

6967
/**
@@ -81,4 +79,23 @@ public function availableGroupNames(): array
8179
{
8280
return \array_keys($this->groupedStories);
8381
}
82+
83+
/**
84+
* @return array<string, class-string<Story>>
85+
*
86+
* @throws FixtureStoryNotFound
87+
*/
88+
private function resolveGroup(string $groupName): array
89+
{
90+
if (!isset($this->groupedStories[$groupName])) {
91+
throw FixtureStoryNotFound::forGroup($groupName, $this->availableGroupNames());
92+
}
93+
94+
return $this->groupedStories[$groupName];
95+
}
96+
97+
private function hasGroup(string $name): bool
98+
{
99+
return isset($this->groupedStories[$name]);
100+
}
84101
}

src/Test/Behat/Config/DatabaseResetMode.php renamed to src/Test/Behat/DatabaseResetMode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Zenstruck\Foundry\Test\Behat\Config;
14+
namespace Zenstruck\Foundry\Test\Behat;
1515

1616
/**
1717
* @internal

src/Test/Behat/DamaNativeExtensionIncompatibility.php renamed to src/Test/Behat/Exception/DamaNativeExtensionIncompatibility.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Zenstruck\Foundry\Test\Behat;
3+
namespace Zenstruck\Foundry\Test\Behat\Exception;
44

55
final class DamaNativeExtensionIncompatibility extends \LogicException
66
{

src/Test/Behat/FactoryNotResolvableException.php renamed to src/Test/Behat/Exception/FactoryNotResolvable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Zenstruck\Foundry\Test\Behat;
14+
namespace Zenstruck\Foundry\Test\Behat\Exception;
1515

1616
/**
1717
* @internal
1818
* @author Nicolas PHILIPPE <nikophil@gmail.com>
1919
*/
20-
final class FactoryNotResolvableException extends \RuntimeException
20+
final class FactoryNotResolvable extends \RuntimeException
2121
{
2222
public static function forName(string $name): self
2323
{

src/Test/Behat/InvalidObjectParameter.php renamed to src/Test/Behat/Exception/InvalidObjectParameter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Zenstruck\Foundry\Test\Behat;
14+
namespace Zenstruck\Foundry\Test\Behat\Exception;
1515

1616
/**
1717
* @internal
1818
* @author Nicolas PHILIPPE <nikophil@gmail.com>
19-
*
20-
* todo: rename les exceptions
2119
*/
2220
final class InvalidObjectParameter extends \RuntimeException
2321
{
24-
public static function objectReferencedInTableDoesNotExist(string $column, ObjectNotFoundException $previous): self
22+
public static function objectReferencedInTableDoesNotExist(string $column, ObjectNotFound $previous): self
2523
{
2624
return new self("A reference to an object cannot be resolved in the table, at column \"$column\": {$previous->getMessage()}", previous: $previous);
2725
}

src/Test/Behat/InvalidResetDbTagException.php renamed to src/Test/Behat/Exception/InvalidResetDbTag.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Zenstruck\Foundry\Test\Behat;
3+
namespace Zenstruck\Foundry\Test\Behat\Exception;
44

5-
final class InvalidResetDbTagException extends \LogicException
5+
final class InvalidResetDbTag extends \LogicException
66
{
77
public static function bothTagsUsed(): self
88
{

src/Test/Behat/ObjectAlreadyRegisteredException.php renamed to src/Test/Behat/Exception/ObjectAlreadyRegistered.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Zenstruck\Foundry\Test\Behat;
14+
namespace Zenstruck\Foundry\Test\Behat\Exception;
1515

1616
/**
1717
* @internal
1818
* @author Nicolas PHILIPPE <nikophil@gmail.com>
1919
*/
20-
final class ObjectAlreadyRegisteredException extends \RuntimeException
20+
final class ObjectAlreadyRegistered extends \RuntimeException
2121
{
2222
/** @param class-string $objectClass */
2323
public static function forClassAndName(string $objectClass, string $name): self
2424
{
25-
return new self("Object \"{$name}\" is already registered for class \"{$objectClass}\".");
25+
return new self("Object \"{$name}\" is already registered for class \"{$objectClass}\". This may happen when loading multiple Stories in a group that define objects with the same name.");
2626
}
2727
}

0 commit comments

Comments
 (0)