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 PHPUnit\Framework\Assert as Assertions; |
10
|
|
|
use Symfony\Component\PropertyAccess\Exception\AccessException; |
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Daniel Espendiller <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class DoctrinePlaceholderContext implements Context, PlaceholderBagAwareContext |
17
|
|
|
{ |
18
|
|
|
use KernelDictionary; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var PlaceholderBagInterface |
22
|
|
|
*/ |
23
|
|
|
private $placeholderBag; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $placeholder |
27
|
|
|
* @param string $model Entity, Repository or class name |
28
|
|
|
* @param string $criteria properties to filter on eg "id=foobar" parse_str supported |
29
|
|
|
* @param string $property Symfony PropertyAccessor syntax eg "foo.bar" "foo[bar]" |
30
|
|
|
* @Given /^I set a placeholder "([^"]*)" on Doctrine model "([^"]*)" with "([^"]*)" and "([^"]*)"$/ |
31
|
|
|
*/ |
32
|
|
|
public function iSetAPlaceholderOnDoctrineModelWithCriteriaAndProperty( |
33
|
|
|
string $placeholder, |
34
|
|
|
string $model, |
35
|
|
|
string $criteria, |
36
|
|
|
string $property |
37
|
|
|
) { |
38
|
|
|
$manager = $this->getContainer()->get('doctrine')->getManagerForClass($model); |
39
|
|
|
Assertions::assertNotNull($manager, 'No valid Doctrine manager found for ' . $model); |
40
|
|
|
|
41
|
|
|
parse_str($criteria, $find); |
42
|
|
|
|
43
|
|
|
$object = $manager->getRepository($model)->findOneBy($find); |
44
|
|
|
|
45
|
|
|
Assertions::assertNotNull( |
46
|
|
|
$object, |
47
|
|
|
sprintf('No valid model found "%s" "%s", "%s"', $model, $criteria, $property) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$value = PropertyAccess::createPropertyAccessor()->getValue($object, $property); |
52
|
|
|
} catch (AccessException $e) { |
53
|
|
|
Assertions::fail('Invalid value not found: ' . $e->getMessage()); |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->placeholderBag->add($placeholder, (string)$value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function setPlaceholderBag(PlaceholderBagInterface $placeholderBag) |
64
|
|
|
{ |
65
|
|
|
$this->placeholderBag = $placeholderBag; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|