Completed
Pull Request — master (#11)
by Loïc
02:42
created

AddressContext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of Jedisjeux.
5
 *
6
 * (c) Loïc Frémont
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 AppBundle\Behat\Context\Setup;
13
14
use AppBundle\Fixture\Factory\ExampleFactoryInterface;
15
use Behat\Behat\Context\Context;
16
use Doctrine\Common\Persistence\ObjectManager;
17
18
class AddressContext implements Context
19
{
20
    /**
21
     * @var ObjectManager
22
     */
23
    private $entityManager;
24
25
    /**
26
     * @var ExampleFactoryInterface
27
     */
28
    private $factory;
29
30
    /**
31
     * @param ObjectManager $manager
32
     * @param ExampleFactoryInterface $factory
33
     */
34
    public function __construct(ObjectManager $manager, ExampleFactoryInterface $factory)
35
    {
36
        $this->entityManager = $manager;
37
        $this->factory = $factory;
38
    }
39
40
    /**
41
     * @Given there is an address located at :city
42
     */
43
    public function thereIsAddressLocatedAtCity(string $city)
44
    {
45
        $this->createAddresses(1, ['city' => $city]);
46
    }
47
48
    /**
49
     * @Given there are :count addresses located at :city
50
     */
51
    public function thereAreAddressesLocatedAtCity(int $count, string $city)
52
    {
53
        $this->createAddresses((int) $count, ['city' => $city]);
54
    }
55
56
    /**
57
     * @param int $count
58
     *
59
     * @param array $options
60
     */
61
    private function createAddresses(int $count, array $options = []): void
62
    {
63
        for ($i=0 ; $i<$count ; $i++) {
64
            $address = $this->factory->create($options);
65
            $this->entityManager->persist($address);
66
        }
67
68
        $this->entityManager->flush();
69
    }
70
}
71