1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace espend\Behat\PlaceholderExtension\Context; |
5
|
|
|
|
6
|
|
|
use Behat\Behat\Context\Context; |
7
|
|
|
use Behat\Symfony2Extension\Context\KernelDictionary; |
8
|
|
|
use espend\Behat\PlaceholderExtension\PlaceholderBagInterface; |
9
|
|
|
use espend\Behat\PlaceholderExtension\Utils\PlaceholderUtil; |
10
|
|
|
use PHPUnit\Framework\Assert as Assertions; |
11
|
|
|
use Symfony\Component\PropertyAccess\Exception\AccessException; |
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Daniel Espendiller <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class DoctrinePlaceholderContext implements Context, PlaceholderBagAwareContextInterface |
18
|
|
|
{ |
19
|
|
|
use KernelDictionary; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var PlaceholderBagInterface |
23
|
|
|
*/ |
24
|
|
|
private $placeholderBag; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $placeholder |
28
|
|
|
* @param string $model Entity, Repository or class name |
29
|
|
|
* @param string $leftOperator filter property |
30
|
|
|
* @param string $rightOperator filter property value |
31
|
|
|
* @param string $property Symfony PropertyAccessor syntax eg "foo.bar" "foo[bar]" |
32
|
|
|
* @Given /^set placeholder "([^"]*)" of "([^"]*)" on Doctrine model "([^"]*)" with "([^"]*)" equals "([^"]*)"$/ |
33
|
|
|
*/ |
34
|
5 |
|
public function setPlaceholderOfPropertyOnDoctrineModelWithCriteriaAndProperty( |
35
|
|
|
string $placeholder, |
36
|
|
|
string $property, |
37
|
|
|
string $model, |
38
|
|
|
string $leftOperator, |
39
|
|
|
string $rightOperator |
40
|
|
|
) { |
41
|
5 |
|
PlaceholderUtil::isValidPlaceholderOrThrowException($placeholder); |
42
|
|
|
|
43
|
3 |
|
$manager = $this->getContainer()->get('doctrine')->getManagerForClass($model); |
44
|
3 |
|
Assertions::assertNotNull($manager, 'No valid Doctrine manager found for ' . $model); |
45
|
|
|
|
46
|
3 |
|
$object = $manager->getRepository($model)->findOneBy([$leftOperator => $rightOperator]); |
47
|
|
|
|
48
|
3 |
|
Assertions::assertNotNull( |
49
|
3 |
|
$object, |
50
|
3 |
|
sprintf('No valid model found "%s" "%s", "%s"', $model, $leftOperator . '=' . $rightOperator , $property) |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
try { |
54
|
2 |
|
$value = PropertyAccess::createPropertyAccessor()->getValue($object, $property); |
55
|
1 |
|
} catch (AccessException $e) { |
56
|
1 |
|
Assertions::fail('Invalid value not found: ' . $e->getMessage()); |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$this->placeholderBag->add($placeholder, (string)$value); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
4 |
|
public function setPlaceholderBag(PlaceholderBagInterface $placeholderBag) |
67
|
|
|
{ |
68
|
4 |
|
$this->placeholderBag = $placeholderBag; |
69
|
4 |
|
} |
70
|
|
|
} |
71
|
|
|
|