1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Behat\Context\Setup; |
13
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
17
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
18
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
19
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Mateusz Zalewski <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class TaxonomyContext implements Context |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var RepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $taxonRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FactoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $taxonFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ObjectManager |
38
|
|
|
*/ |
39
|
|
|
private $objectManager; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param RepositoryInterface $taxonRepository |
43
|
|
|
* @param FactoryInterface $taxonFactory |
44
|
|
|
* @param ObjectManager $objectManager |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
RepositoryInterface $taxonRepository, |
48
|
|
|
FactoryInterface $taxonFactory, |
49
|
|
|
ObjectManager $objectManager |
50
|
|
|
) { |
51
|
|
|
$this->taxonRepository = $taxonRepository; |
52
|
|
|
$this->taxonFactory = $taxonFactory; |
53
|
|
|
$this->objectManager = $objectManager; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @Given the store classifies its products as :firstTaxonName |
58
|
|
|
* @Given the store classifies its products as :firstTaxonName and :secondTaxonName |
59
|
|
|
* @Given the store classifies its products as :firstTaxonName, :secondTaxonName and :thirdTaxonName |
60
|
|
|
*/ |
61
|
|
|
public function storeClassifiesItsProductsAs($firstTaxonName, $secondTaxonName = null, $thirdTaxonName = null) |
62
|
|
|
{ |
63
|
|
|
foreach ([$firstTaxonName, $secondTaxonName, $thirdTaxonName] as $taxonName) { |
64
|
|
|
if (null === $taxonName) { |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->taxonRepository->add($this->createTaxon($taxonName)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @Given /^(it) (belongs to "[^"]+")$/ |
74
|
|
|
*/ |
75
|
|
|
public function itBelongsTo(ProductInterface $product, TaxonInterface $taxon) |
76
|
|
|
{ |
77
|
|
|
$product->addTaxon($taxon); |
78
|
|
|
|
79
|
|
|
$this->objectManager->flush($product); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $name |
84
|
|
|
* |
85
|
|
|
* @return TaxonInterface |
86
|
|
|
*/ |
87
|
|
|
private function createTaxon($name) |
88
|
|
|
{ |
89
|
|
|
$taxon = $this->taxonFactory->createNew(); |
90
|
|
|
$taxon->setName($name); |
91
|
|
|
$taxon->setCode($this->getCodeFromName($name)); |
92
|
|
|
|
93
|
|
|
return $taxon; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $name |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private function getCodeFromName($name) |
102
|
|
|
{ |
103
|
|
|
return str_replace([' ', '-'], '_', strtolower($name)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|