FeatureContext::getJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the LoopBackApiBundle package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
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
use Doctrine\Common\Persistence\ManagerRegistry;
13
use Doctrine\Common\Persistence\ObjectManager;
14
use Doctrine\ORM\Tools\SchemaTool;
15
use Fidry\LoopBackApiBundle\Tests\Functional\Bundle\TestBundle\Entity\Dummy;
16
use PHPUnit_Framework_Assert as PHPUnit;
17
use Sanpi\Behatch\Json\Json;
18
use Sanpi\Behatch\Json\JsonInspector;
19
20
/**
21
 * @author Théo FIDRY <[email protected]>
22
 */
23
class FeatureContext extends \Behat\MinkExtension\Context\RawMinkContext
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    /**
26
     * @var array
27
     */
28
    private $classes;
29
30
    /**
31
     * @var ObjectManager
32
     */
33
    private $manager;
34
35
    /**
36
     * @var SchemaTool
37
     */
38
    private $schemaTool;
39
40
    /**
41
     * Initializes context.
42
     *
43
     * @param ManagerRegistry $doctrine
44
     */
45
    public function __construct(ManagerRegistry $doctrine)
46
    {
47
        $this->manager = $doctrine->getManager();
48
        $this->schemaTool = new SchemaTool($this->manager);
49
        $this->classes = $this->manager->getMetadataFactory()->getAllMetadata();
50
        $this->inspector = new JsonInspector('javascript');
0 ignored issues
show
Bug introduced by
The property inspector does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
    }
52
53
    /**
54
     * @Given there is :nb dummy objects
55
     *
56
     * @param int $nbr
57
     */
58
    public function thereIsDummyObjects($nbr)
59
    {
60
        for ($i = 1; $i <= $nbr; ++$i) {
61
            $dummy = new Dummy();
62
            $dummy->setName('Dummy #'.$i);
63
            $dummy->setAlias('Alias #'.($nbr - $i));
0 ignored issues
show
Bug introduced by
The method setAlias() does not seem to exist on object<Fidry\LoopBackApi...estBundle\Entity\Dummy>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
65
            $this->manager->persist($dummy);
66
        }
67
68
        $this->manager->flush();
69
    }
70
71
    /**
72
     * Checks that given JSON node is null
73
     *
74
     * @Then the JSON node :node should be null
75
     */
76
    public function theJsonNodeShouldBeNull($node)
77
    {
78
        $actual = $this->getJsonNodeValue($node);
79
        PHPUnit::assertNull($actual, sprintf('The node value is `%s`', json_encode($actual)));
80
    }
81
82
    /**
83
     * @Then the JSON node :node should be equal to the boolean :value
84
     */
85
    public function theJsonNodeShouldBeEmpty($node, $value)
86
    {
87
        $actual = $this->getJsonNodeValue($node);
88
89
        if ('false' === $value) {
90
            $value = false;
91
        } else {
92
            $value = (bool) $value;
93
        }
94
95
        PHPUnit::assertEquals($value, $actual, sprintf('The node value is `%s`', json_encode($actual)));
96
    }
97
98
    /**
99
     * @param string $node JSON node.
100
     *
101
     * @return mixed JSON node value.
102
     */
103
    private function getJsonNodeValue($node)
104
    {
105
        $json = $this->getJson();
106
        return $this->inspector->evaluate($json, $node);
107
    }
108
109
    /**
110
     * @return Json Get JSON content of the response.
111
     */
112
    private function getJson()
113
    {
114
        return new Json($this->getSession()->getPage()->getContent());
115
    }
116
}
117