Completed
Pull Request — master (#7)
by Loïc
02:38 queued 10s
created

DefaultContext::getExampleFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
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;
13
14
use AppBundle\Fixture\Factory\ExampleFactoryInterface;
15
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
16
use Doctrine\Bundle\PHPCRBundle\Command\WorkspacePurgeCommand;
17
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use Doctrine\ORM\EntityManager;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
23
use Symfony\Bundle\FrameworkBundle\Console\Application;
24
use Symfony\Component\Console\Tester\CommandTester;
25
26
/**
27
 * @author Loïc Frémont <[email protected]>
28
 */
29
class DefaultContext extends DefaultApiContext
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $applicationName = 'app';
35
36
    /**
37
     * @var Application
38
     */
39
    private $application;
40
41
    /**
42
     * @var CommandTester
43
     */
44
    private $tester;
45
46
    /**
47
     * @var ContainerAwareCommand
48
     */
49
    private $command;
50
51
    /**
52
     * @BeforeScenario
53
     */
54
    public function purgeDatabase(BeforeScenarioScope $scope)
0 ignored issues
show
Unused Code introduced by
The parameter $scope is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        /** @var EntityManager $em */
57
        $em = $this->getEntityManager();
58
        $stmt = $em
59
            ->getConnection()
60
            ->prepare('SET foreign_key_checks = 0;');
61
        $stmt->execute();
62
        $purger = new ORMPurger($this->getEntityManager());
0 ignored issues
show
Documentation introduced by
$this->getEntityManager() is of type object<Doctrine\Common\Persistence\ObjectManager>, but the function expects a null|object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
        $purger->purge();
64
        $stmt = $em
65
            ->getConnection()
66
            ->prepare('SET foreign_key_checks = 1;');
67
        $stmt->execute();
68
69
70
    }
71
72
    /**
73
     * @param string $resourceName
74
     * @param null|string $applicationName
75
     *
76
     * @return FactoryInterface
77
     */
78
    protected function getFactory($resourceName, $applicationName = null)
79
    {
80
        $applicationName = null === $applicationName ? $this->applicationName : $applicationName;
81
82
        /** @var FactoryInterface $factory */
83
        $factory = $this->getService($applicationName . '.factory.' . $resourceName);
84
85
        return $factory;
86
    }
87
88
    /**
89
     * @param string $resourceName
90
     * @param null|string $applicationName
91
     *
92
     * @return ExampleFactoryInterface
93
     */
94
    protected function getExampleFactory($resourceName, $applicationName = null): ExampleFactoryInterface
95
    {
96
        $applicationName = null === $applicationName ? $this->applicationName : $applicationName;
97
98
        /** @var ExampleFactoryInterface $factory */
99
        $factory = $this->getService($applicationName . '.fixture.example_factory.' . $resourceName);
100
101
        return $factory;
102
    }
103
104
    /**
105
     * @param string $type
106
     * @param array $criteria
107
     * @param null|string $applicationName
108
     *
109
     * @return object
110
     */
111
    protected function findOneBy($type, array $criteria, $applicationName = null)
112
    {
113
        $applicationName = null === $applicationName ? $this->applicationName : $applicationName;
114
115
        $resource = $this
116
            ->getRepository($type, $applicationName)
117
            ->findOneBy($criteria);
118
119
        if (null === $resource) {
120
            throw new \InvalidArgumentException(
121
                sprintf('%s for criteria "%s" was not found.', str_replace('_', ' ', ucfirst($type)), serialize($criteria))
122
            );
123
        }
124
125
        return $resource;
126
    }
127
128
    /**
129
     * @param string $resourceName
130
     * @param null|string $applicationName
131
     *
132
     * @return RepositoryInterface
133
     */
134
    protected function getRepository($resourceName, $applicationName = null)
135
    {
136
        $applicationName = null === $applicationName ? $this->applicationName : $applicationName;
137
138
        /** @var RepositoryInterface $repository */
139
        $repository = $this->getService($applicationName . '.repository.' . $resourceName);
140
141
        return $repository;
142
    }
143
144
    /**
145
     * @return ObjectManager
146
     */
147
    protected function getDocumentManager()
148
    {
149
        return $this->getService('doctrine_phpcr')->getManager();
150
    }
151
}