Completed
Push — develop ( e0343e...7d04f4 )
by Daniel
08:13
created

EntityManagerContext   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 70
rs 10
c 1
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A tableExists() 0 3 1
A schemaIsValid() 0 5 1
A createDatabase() 0 3 1
A __construct() 0 6 1
A tableTotal() 0 3 1
A dropDatabase() 0 4 1
1
<?php
2
3
use Behat\Behat\Context\Context;
4
use Behatch\HttpCall\Request;
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Doctrine\ORM\Tools\SchemaTool;
8
use Doctrine\ORM\Tools\SchemaValidator;
9
use PHPUnit\Framework\Assert;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Assert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
/**
12
 * Defines application features from the specific context.
13
 */
14
class EntityManagerContext implements Context
15
{
16
    /**
17
     * @var EntityManagerInterface
18
     */
19
    private $manager;
20
    private $doctrine;
21
    private $schemaTool;
22
    private $classes;
23
24
    /**
25
     * Initializes context.
26
     *
27
     * Every scenario gets its own context instance.
28
     * You can also pass arbitrary arguments to the
29
     * context constructor through behat.yml.
30
     * @param ManagerRegistry $doctrine
31
     * @param Request $request
32
     */
33
    public function __construct(ManagerRegistry $doctrine, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

33
    public function __construct(ManagerRegistry $doctrine, /** @scrutinizer ignore-unused */ Request $request)

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

Loading history...
34
    {
35
        $this->doctrine = $doctrine;
36
        $this->manager = $doctrine->getManager();
37
        $this->schemaTool = new SchemaTool($this->manager);
38
        $this->classes = $this->manager->getMetadataFactory()->getAllMetadata();
39
    }
40
41
    /**
42
     * @BeforeScenario @createSchema
43
     * @throws \Doctrine\ORM\Tools\ToolsException
44
     */
45
    public function createDatabase()
46
    {
47
        $this->schemaTool->createSchema($this->classes);
48
    }
49
50
    /**
51
     * @When drop the schema
52
     * @AfterScenario @dropSchema
53
     */
54
    public function dropDatabase()
55
    {
56
        $this->schemaTool->dropSchema($this->classes);
57
        $this->doctrine->getManager()->clear();
58
    }
59
60
    /**
61
     * @Then the database schema should be valid
62
     */
63
    public function schemaIsValid()
64
    {
65
        $validator = new SchemaValidator($this->manager);
66
        $errors = $validator->validateMapping();
67
        Assert::assertCount(0, $errors, json_encode($errors, JSON_PRETTY_PRINT));
68
    }
69
70
    /**
71
     * @Then the table :table should exist
72
     */
73
    public function tableExists(string $table)
74
    {
75
        Assert::assertTrue($this->manager->getConnection()->getSchemaManager()->tablesExist([$table]));
76
    }
77
78
    /**
79
     * @Then there should be :total tables in the database
80
     */
81
    public function tableTotal(int $total)
82
    {
83
        Assert::assertCount($total, $this->manager->getConnection()->getSchemaManager()->listTables());
84
    }
85
}
86