Completed
Push — master ( c586b2...0e9fec )
by Kamil
21:30
created

FixtureContext::setDataToObject()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 16
rs 9.2
c 1
b 1
f 0
cc 4
eloc 9
nc 5
nop 2
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\Bundle\ResourceBundle\Behat;
13
14
use Behat\Gherkin\Node\TableNode;
15
use Symfony\Component\PropertyAccess\PropertyAccessor;
16
use Symfony\Component\PropertyAccess\StringUtil;
17
18
class FixtureContext extends DefaultContext
19
{
20
    /**
21
     * @Given /^there are the following ([^"]*):$/
22
     */
23
    public function thereAreFollowingResources($resource, TableNode $table)
24
    {
25
        foreach ($table->getHash() as $data) {
26
            $this->thereIsResource($resource, $data, false);
27
        }
28
29
        $this->getEntityManager()->flush();
30
    }
31
32
    public function thereIsResource($resource, $additionalData, $flush = true)
33
    {
34
        if ($additionalData instanceof TableNode) {
35
            $additionalData = $additionalData->getHash();
36
        }
37
38
        $entity = $this->getFactory($resource)->createNew();
39
40
        if (count($additionalData) > 0) {
41
            $this->setDataToObject($entity, $additionalData);
42
        }
43
44
        return $this->persistAndFlush($entity, $flush);
45
    }
46
47
    /**
48
     * @Given /^there are no ([^"]*)$/
49
     */
50
    public function thereAreNoResources($type)
51
    {
52
        $type = str_replace(' ', '_', StringUtil::singularify($type));
53
        // Hacky hack for multiple singular forms.
54
        $type = is_array($type) ? $type[1] : $type;
55
        // Hacky hack again because we do not retrieve the right singular with the previous hack...
56
        $type = $type == 'addresse' ? 'address' : $type;
57
58
        $manager = $this->getEntityManager();
59
60
        foreach ($this->getRepository($type)->findAll() as $resource) {
61
            $manager->remove($resource);
62
        }
63
64
        $manager->flush();
65
    }
66
67
    /**
68
     * @Given /^([^""]*) with following data should be created:$/
69
     */
70
    public function objectWithFollowingDataShouldBeCreated($type, TableNode $table)
71
    {
72
        $accessor = new PropertyAccessor();
73
74
        $data = $table->getRowsHash();
75
        $type = str_replace(' ', '_', trim($type));
76
77
        $object = $this->waitFor(function () use ($type, $data) {
78
            try {
79
                return $this->findOneByName($type, $data['name']);
80
            } catch (\InvalidArgumentException $exception) {
81
                return null;
82
            }
83
        });
84
85
        foreach ($data as $property => $value) {
86
            $objectValue = $accessor->getValue($object, $property);
87
            if (is_array($objectValue)) {
88
                $objectValue = implode(',', $objectValue);
89
            }
90
91
            if ($objectValue !== $value) {
92
                throw new \Exception(sprintf(
93
                    '%s object::%s has "%s" value but "%s" expected',
94
                    $type,
95
                    $property,
96
                    $objectValue,
97
                    is_array($value) ? implode(',', $value) : $value)
98
                );
99
            }
100
        }
101
    }
102
103
    /**
104
     * @Given /^I have deleted the ([^"]*) "([^""]*)"/
105
     */
106
    public function haveDeleted($resource, $name)
107
    {
108
        $manager = $this->getEntityManager();
109
        $manager->remove($this->findOneByName($resource, $name));
110
        $manager->flush();
111
    }
112
113
    /**
114
     * @Given I deleted :type with :property :value
115
     */
116
    public function iDeletedResource($type, $property, $value)
117
    {
118
        $user = $this->findOneBy($type, [$property => $value]);
119
        $entityManager = $this->getEntityManager();
120
        $entityManager->remove($user);
121
        $entityManager->flush();
122
    }
123
124
    /**
125
     * Set data to an object.
126
     *
127
     * @param $object
128
     * @param $data
129
     */
130
    protected function setDataToObject($object, array $data)
131
    {
132
        foreach ($data as $property => $value) {
133
            if (1 === preg_match('/date/i', $property)) {
134
                $value = new \DateTime($value);
135
            }
136
137
            $propertyParts = explode(' ', $property);
138
            $propertyName = implode(array_map('ucfirst', $propertyParts));
139
140
            $method = 'set'.$propertyName;
141
            if (method_exists($object, $method)) {
142
                $object->{'set'.$propertyName}($value);
143
            }
144
        }
145
    }
146
147
    /**
148
     * Persist and flush $entity.
149
     *
150
     * @param object $entity
151
     * @param bool $flush
152
     *
153
     * @return mixed
154
     */
155
    protected function persistAndFlush($entity, $flush = true)
156
    {
157
        $this->getEntityManager()->persist($entity);
158
159
        if ($flush) {
160
            $this->getEntityManager()->flush();
161
        }
162
163
        return $entity;
164
    }
165
}
166