-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
1,048 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/docker/ | ||
/var/ | ||
!/var/database/.gitkeep | ||
/vendor/ | ||
/.phpcs-cache | ||
/.phpunit.result.cache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# FactoryAdapter | ||
|
||
The [FactoryAdapter][1] transforms the input value from a string into the result of a [zenstruck/foundry][2] factory method. | ||
|
||
Ex. The string `'<factory("user", "findBy", {"lastname": "Doe"})>'` will be transformed into a collection of user entities having "Doe" for lastname in the database. | ||
|
||
## Usage | ||
|
||
### Fetch an entity with a simple name | ||
|
||
Given there is an entity like: | ||
```php | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class User | ||
{ | ||
#[ORM\Column(type: Types::INTEGER)] | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
public int $id; | ||
|
||
#[ORM\Column] | ||
public string $firstname; | ||
|
||
#[ORM\Column] | ||
public string $lastname; | ||
|
||
#[ORM\Column] | ||
public string $email; | ||
} | ||
``` | ||
And there is 1 user whose firstname is "John" | ||
Then the following should be true | ||
|
||
```php | ||
<?php | ||
|
||
use App\Entity\User; | ||
use Presta\BehatEvaluator\Adapter\FactoryAdapter; | ||
|
||
$evaluate = new FactoryAdapter(); | ||
$value = $evaluate('<factory("user", {"firstname": "John"})>'); | ||
|
||
\assert($value instanceof User); | ||
\assert('John' === $value->firstname); | ||
``` | ||
|
||
### Count entities with a compound name | ||
|
||
Given there is an entity like: | ||
```php | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class UserGroup | ||
{ | ||
#[ORM\Column(type: Types::INTEGER)] | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
public int $id; | ||
|
||
#[ORM\Column] | ||
public string $name; | ||
} | ||
``` | ||
And there are 3 user groups, 1 of which being named "Manager" | ||
Then the following should be true | ||
|
||
```php | ||
<?php | ||
|
||
use Presta\BehatEvaluator\Adapter\FactoryAdapter; | ||
|
||
$evaluate = new FactoryAdapter(); | ||
$value = $evaluate('<factory("user group", "count", {"name": "Manager"})>'); | ||
|
||
\assert(1 === $value); | ||
``` | ||
|
||
### Get the value of a property of an entity in a sub namespace | ||
|
||
Given there is an entity like: | ||
```php | ||
<?php | ||
|
||
namespace App\Entity\User; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
#[ORM\Entity] | ||
class Role | ||
{ | ||
#[ORM\Column(type: Types::INTEGER)] | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue] | ||
public int $id; | ||
|
||
#[ORM\Column] | ||
public string $name; | ||
} | ||
``` | ||
And there is 1 user role named "Admin" | ||
Then the following should be true | ||
|
||
```php | ||
<?php | ||
|
||
use Presta\BehatEvaluator\Adapter\FactoryAdapter; | ||
|
||
$evaluate = new FactoryAdapter(); | ||
$value = $evaluate('<factory("user/role", {"name": "Admin"}, "name")>'); | ||
|
||
\assert('Admin' === $value); | ||
``` | ||
|
||
--- | ||
|
||
You may return to the [README][3] or read other [adapters][4] guides. | ||
|
||
[1]: ../../src/Adapter/FactoryAdapter.php | ||
[2]: https://github.com/zenstruck/foundry | ||
[3]: ../../README.md | ||
[4]: ../adapters/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Presta\BehatEvaluator\Adapter; | ||
|
||
use Presta\BehatEvaluator\ExpressionLanguage\ExpressionLanguage; | ||
|
||
/** | ||
* @example <factory("user", {"email": "john.doe@prestaconcept.net"})> | ||
* @example <factory("user", {"email": "john.doe@prestaconcept.net"}, "id")> | ||
* @example <factory("user", "find", {"email": "john.doe@prestaconcept.net"})> | ||
* @example <factory("user", "find", {"email": "john.doe@prestaconcept.net"}, "id")> | ||
* @example <factory("user/role", "count")> | ||
* @example <factory("rating number", "count")> | ||
*/ | ||
final class FactoryAdapter implements AdapterInterface | ||
{ | ||
public function __construct(private readonly ExpressionLanguage $expressionLanguage) | ||
{ | ||
} | ||
|
||
public function __invoke(mixed $value): mixed | ||
{ | ||
if (!\is_string($value)) { | ||
return $value; | ||
} | ||
|
||
preg_match_all("/<(?<expression>factory\([^)]*\))>/", $value, $matches); | ||
|
||
foreach ($matches['expression'] as $expression) { | ||
$evaluated = $this->expressionLanguage->evaluate($expression); | ||
if (!\is_string($evaluated)) { | ||
return $evaluated; | ||
} | ||
|
||
$value = str_replace("<$expression>", $evaluated, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/ExpressionLanguage/ArgumentGuesser/Factory/AccessorArgumentGuesser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Presta\BehatEvaluator\ExpressionLanguage\ArgumentGuesser\Factory; | ||
|
||
final class AccessorArgumentGuesser implements ArgumentGuesserInterface | ||
{ | ||
public function __invoke( | ||
array|string|null $method, | ||
array|string|null $attributes, | ||
string|null $accessor, | ||
): string|null { | ||
if (null === $method) { | ||
return null; | ||
} | ||
|
||
if (\is_string($attributes)) { | ||
return $attributes; | ||
} | ||
|
||
return $accessor; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/ExpressionLanguage/ArgumentGuesser/Factory/ArgumentGuesserInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Presta\BehatEvaluator\ExpressionLanguage\ArgumentGuesser\Factory; | ||
|
||
/** | ||
* @phpstan-type FactoryAttributes array<string, mixed> | ||
*/ | ||
interface ArgumentGuesserInterface | ||
{ | ||
/** | ||
* @param FactoryAttributes|string|null $method | ||
* @param FactoryAttributes|string|null $attributes | ||
*/ | ||
public function __invoke(string|array|null $method, string|array|null $attributes, string|null $accessor): mixed; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/ExpressionLanguage/ArgumentGuesser/Factory/AttributesArgumentGuesser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Presta\BehatEvaluator\ExpressionLanguage\ArgumentGuesser\Factory; | ||
|
||
final class AttributesArgumentGuesser implements ArgumentGuesserInterface | ||
{ | ||
/** | ||
* @return array<string, mixed>|null | ||
*/ | ||
public function __invoke( | ||
array|string|null $method, | ||
array|string|null $attributes, | ||
string|null $accessor, | ||
): array|null { | ||
if (\is_array($method)) { | ||
return $method; | ||
} | ||
|
||
if (\is_array($attributes)) { | ||
return $attributes; | ||
} | ||
|
||
return null; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/ExpressionLanguage/ArgumentGuesser/Factory/MethodArgumentGuesser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Presta\BehatEvaluator\ExpressionLanguage\ArgumentGuesser\Factory; | ||
|
||
final class MethodArgumentGuesser implements ArgumentGuesserInterface | ||
{ | ||
public function __invoke(array|string|null $method, array|string|null $attributes, string|null $accessor): string | ||
{ | ||
if (\is_string($method)) { | ||
return $method; | ||
} | ||
|
||
return 'find'; | ||
} | ||
} |
Oops, something went wrong.