FixtureContext::aUserWithNameAndPasswordExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
crap 2
1
<?php
2
3
namespace TreeHouse\BaseApiBundle\Behat;
4
5
use Behat\Gherkin\Node\TableNode;
6
use Doctrine\Common\Inflector\Inflector;
7
use Doctrine\ORM\EntityManager;
8
use FOS\UserBundle\Model\UserManagerInterface;
9
use PHPUnit_Framework_Assert as Assert;
10
11
class FixtureContext extends BaseFeatureContext
12
{
13
    /**
14
     * @Given an api user named :username with password :password exists
15
     */
16
    public function anApiUserWithNameAndPasswordExists($username, $password)
17
    {
18
        $user = $this->aUserWithNameAndPasswordExists($username, $password);
19
        $user->addRole('ROLE_API_USER');
20
21
        /** @var UserManagerInterface $userManager */
22
        $userManager = $this->get('fos_user.user_manager');
23
        $userManager->updateUser($user);
24
25
        return $user;
26
    }
27
28
    /**
29
     * @Given a user named :username with password :password exists
30
     */
31
    public function aUserWithNameAndPasswordExists($username, $password)
32
    {
33
        /** @var UserManagerInterface $userManager */
34
        $userManager = $this->get('fos_user.user_manager');
35
36
        $user = $userManager->createUser();
37
        $user->setEnabled(true);
38
        $user->setUsername($username);
39
        $user->setEmail(sprintf('%[email protected]', $username));
40
        $user->setPlainPassword($password);
41
        $userManager->updateUser($user);
42
43
        return $user;
44
    }
45
46
    /**
47
     * @Given the following :entityName entities exist:
48
     */
49
    public function theFollowingEntitiesExist($entityName, TableNode $table)
50
    {
51
        /** @var EntityManager $doctrine */
52
        $doctrine = $this->get('doctrine')->getManager();
53
        $meta = $doctrine->getClassMetadata($entityName);
54
55
        $rows = [];
56
        $hash = $table->getHash();
57
        foreach ($hash as $row) {
58
            $id = $row['id'];
59
            unset($row['id']);
60
61
            foreach ($row as $property => &$value) {
62
                $propertyName = Inflector::camelize($property);
63
64
                $fieldType = $meta->getTypeOfField($propertyName);
65
                switch ($fieldType) {
66
                    case 'array':
67
                    case 'json_array':
68
                        $value = json_decode($value, true);
69
                        break;
70
                    case 'datetime':
71
                        $value = new \DateTime($value);
72
                        break;
73
                }
74
            }
75
76
            $rows[$id] = $row;
77
        }
78
79
        $this->persistEntities($entityName, $rows);
80
    }
81
82
    /**
83
     * @Given the entity :entityName with id :id does not exist
84
     */
85
    public function theEntityWithIdDoesNotExist($entityName, $id)
86
    {
87
        $entity = $this->get('doctrine')->getRepository($entityName)->findOneById($id);
88
89
        Assert::assertNull($entity, "Result of 'findOneById' should be NULL when an entity does not exist");
90
    }
91
}
92