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; |
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) |
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
|
|
|
|