Completed
Push — master ( 81c6e7...6c71bd )
by Piotr
02:30
created

DataContext::followingNewsExistInDatabase()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 1
1
<?php
2
/**
3
 * (c) FSi sp. z o.o. <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace FSi\Bundle\AdminBundle\Behat\Context;
10
11
use Behat\Gherkin\Node\TableNode;
12
use Behat\Symfony2Extension\Context\KernelAwareContext;
13
use Doctrine\ORM\Tools\SchemaTool;
14
use Faker\Factory;
15
use Faker\ORM\Doctrine\Populator;
16
use FSi\FixturesBundle\Entity\News;
17
use FSi\FixturesBundle\Entity\Tag;
18
use Symfony\Component\HttpKernel\KernelInterface;
19
use Symfony\Component\PropertyAccess\PropertyAccess;
20
21
class DataContext implements KernelAwareContext
22
{
23
    /**
24
     * @var KernelInterface
25
     */
26
    protected $kernel;
27
28
    /**
29
     * @param KernelInterface $kernel
30
     */
31
    public function setKernel(KernelInterface $kernel)
32
    {
33
        $this->kernel = $kernel;
34
    }
35
36
    /**
37
     * @BeforeScenario
38
     */
39
    public function createDatabase()
40
    {
41
        $this->deleteDatabaseIfExist();
42
        $metadata = $this->getDoctrine()->getManager()->getMetadataFactory()->getAllMetadata();
43
        $tool = new SchemaTool($this->getDoctrine()->getManager());
44
        $tool->createSchema($metadata);
45
    }
46
47
    /**
48
     * @AfterScenario
49
     */
50
    public function deleteDatabaseIfExist()
51
    {
52
        $dbFilePath = $this->kernel->getRootDir() . '/data.sqlite';
53
54
        if (file_exists($dbFilePath)) {
55
            unlink($dbFilePath);
56
        }
57
    }
58
59
    /**
60
     * @Then /^new news should be created$/
61
     */
62
    public function newNewsShouldBeCreated()
63
    {
64
        $this->thereShouldBeNewsInDatabase(1);
65
    }
66
67
    /**
68
     * @Then /^new subscriber should be created$/
69
     */
70
    public function newSubscriberShouldBeCreated()
71
    {
72
        $this->thereShouldBeSubscribersInDatabase(1);
73
    }
74
75
    /**
76
     * @Then /^there should be (\d+) news in database$/
77
     */
78
    public function thereShouldBeNewsInDatabase($newsCount)
79
    {
80
        expect(count($this->getEntityRepository('FSi\FixturesBundle\Entity\News')->findAll()))->toBe($newsCount);
81
    }
82
83
    /**
84
     * @Given /^the following news exist in database$/
85
     */
86
    public function followingNewsExistInDatabase(TableNode $table)
87
    {
88
        $generator = Factory::create();
89
        foreach ($table->getHash() as $newsNode) {
90
            $news = $this->getEntityRepository('FSi\FixturesBundle\Entity\News')->findOneByTitle($newsNode['Title']);
91
            if (!isset($news)) {
92
                $news = new News();
93
            }
94
95
            $news->setTitle($newsNode['Title']);
96
            if (isset($newsNode['Date']) && $newsNode['Date']) {
97
                $news->setDate(\DateTime::createFromFormat('Y-m-d', $newsNode['Date']));
98
            }
99
            $news->setCreatedAt($generator->dateTime());
100
            $news->setVisible($generator->boolean());
101
            $news->setCreatorEmail($generator->email());
102
103
            $this->getDoctrine()->getManager()->persist($news);
104
            $this->getDoctrine()->getManager()->flush();
105
        }
106
    }
107
108
    /**
109
     * @Given /^there are (\d+) news in database$/
110
     * @Given /^there is (\d+) news in database$/
111
     */
112
    public function thereAreNewsInDatabase($newsCount)
113
    {
114
        $generator = Factory::create();
115
        $populator = new Populator($generator, $this->getDoctrine()->getManager());
116
117
        $populator->addEntity('FSi\FixturesBundle\Entity\News', $newsCount, array(
118
            'creatorEmail' => function() use ($generator) { return $generator->email(); },
119
            'categories' => function() use($generator) {return array($generator->text(), $generator->text());},
120
            'photoKey' => null
121
        ), array(function(News $news) use($generator) {
122
            $tag = new Tag();
123
            $tag->setName($generator->sentence());
124
            $tag->setNews($news);
125
            $news->setTags(array($tag));
126
        }));
127
        $populator->execute();
128
129
        expect(count($this->getEntityRepository('FSi\FixturesBundle\Entity\News')->findAll()))->toBe($newsCount);
130
    }
131
132
    /**
133
     * @Given /^there is news with id (\d+) in database$/
134
     */
135
    public function thereIsNewsWithIdInDatabase($id)
136
    {
137
        $generator = Factory::create();
138
        $populator = new Populator($generator, $this->getDoctrine()->getManager());
139
140
        $populator->addEntity('FSi\FixturesBundle\Entity\News', 1, array(
141
            'id' => $id,
142
            'creatorEmail' => function() use ($generator) { return $generator->email(); },
143
            'photoKey' => null
144
        ));
145
        $populator->execute();
146
147
        expect(count($this->getEntityRepository('FSi\FixturesBundle\Entity\News')->findAll()))->toBe(1);
148
    }
149
150
    /**
151
     * @Then /^there should be news with "([^"]*)" title in database$/
152
     */
153
    public function thereShouldBeNewsWithTitleInDatabase($title)
154
    {
155
        expect($this->getEntityRepository('FSi\FixturesBundle\Entity\News')->findOneByTitle($title))
156
            ->toBeAnInstanceOf('FSi\FixturesBundle\Entity\News');
157
    }
158
159
    /**
160
     * @Given /^news "([^"]*)" should not exist in database anymore$/
161
     */
162
    public function newsShouldNotExistInDatabaseAnymore($title)
163
    {
164
        expect($this->getEntityRepository('FSi\FixturesBundle\Entity\News')->findOneBy(array(
165
            'title' => $title
166
        )))->toBe(null);
167
    }
168
169
170
    /**
171
     * @Given /^there should not be any news in database$/
172
     */
173
    public function thereShouldNotBeAnyNewsInDatabase()
174
    {
175
        expect(count($this->getEntityRepository('FSi\FixturesBundle\Entity\News')->findAll()))->toBe(0);
176
    }
177
178
    /**
179
     * @Given /^there is (\d+) subscriber in database$/
180
     * @Given /^there are (\d+) subscribers in database$/
181
     */
182
    public function thereAreSubscribersInDatabase($count)
183
    {
184
        $generator = Factory::create();
185
        $populator = new Populator($generator, $this->getDoctrine()->getManager());
186
187
        $populator->addEntity('FSi\FixturesBundle\Entity\Subscriber', $count, array(
188
            'email' => function() use ($generator) { return $generator->email(); }
189
        ));
190
        $populator->execute();
191
192
        expect(count($this->getEntityRepository('FSi\FixturesBundle\Entity\Subscriber')->findAll()))->toBe($count);
193
    }
194
195
    /**
196
     * @Given /^there is subscriber with id (\d+) in database$/
197
     */
198
    public function thereIsSubscriberWithIdInDatabase($id)
199
    {
200
        $generator = Factory::create();
201
        $populator = new Populator($generator, $this->getDoctrine()->getManager());
202
203
        $populator->addEntity('FSi\FixturesBundle\Entity\Subscriber', 1, array(
204
            'id' => $id,
205
            'email' => function() use ($generator) { return $generator->email(); }
206
        ));
207
        $populator->execute();
208
209
        expect(count($this->getEntityRepository('FSi\FixturesBundle\Entity\Subscriber')->findAll()))->toBe(1);
210
    }
211
212
    /**
213
     * @Given /^there should be (\d+) subscribers in database$/
214
     */
215
    public function thereShouldBeSubscribersInDatabase($count)
216
    {
217
        expect(count($this->getEntityRepository('FSi\FixturesBundle\Entity\Subscriber')->findAll()))->toBe($count);
218
    }
219
220
    /**
221
     * @Given /^there should not be any subscribers in database$/
222
     */
223
    public function thereShouldNotBeAnySubscribersInDatabase()
224
    {
225
        expect(count($this->getEntityRepository('FSi\FixturesBundle\Entity\Subscriber')->findAll()))->toBe(0);
226
    }
227
228
    /**
229
     * @param string $name
230
     * @return \Doctrine\Orm\EntityRepository
231
     */
232
    protected function getEntityRepository($name)
233
    {
234
        return $this->getDoctrine()->getManager()->getRepository($name);
235
    }
236
237
    /**
238
     * @return \Doctrine\Bundle\DoctrineBundle\Registry
239
     */
240
    protected function getDoctrine()
241
    {
242
        return $this->kernel->getContainer()->get('doctrine');
243
    }
244
245
    /**
246
     * @Then /^news should have (\d+) elements in collection "([^"]*)"$/
247
     */
248
    public function newsShouldHaveElementsInCollection($expectedCount, $collectionName)
249
    {
250
        $manager = $this->getDoctrine()->getManager();
251
        $manager->clear();
252
253
        $news = $manager
254
            ->getRepository('FSi\FixturesBundle\Entity\News')
255
            ->findBy(array(), array(), 1);
256
        $news = reset($news);
257
258
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
259
        $tags = $propertyAccessor->getValue($news, strtolower($collectionName));
260
261
        expect(count($tags))->toBe($expectedCount);
262
    }
263
}
264