/
AbstractIntegrationTest.php
155 lines (131 loc) · 4.53 KB
/
AbstractIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Tests\MyCLabs\ACL\Integration;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use MyCLabs\ACL\ACL;
use MyCLabs\ACL\Doctrine\ACLSetup;
use MyCLabs\ACL\Model\ClassResource;
use Tests\MyCLabs\ACL\Integration\Model\Actions;
abstract class AbstractIntegrationTest extends \PHPUnit_Framework_TestCase
{
/**
* @var EntityManager
*/
protected $em;
/**
* @var ACL
*/
protected $acl;
public function setUp()
{
$dbParams = $this->getDBParams();
// Create the DB
$this->dropAndCreateDB(DriverManager::getConnection($dbParams));
// Create the entity manager
$paths = [
__DIR__ . '/../../src/Model',
__DIR__ . '/Model',
__DIR__ . '/Issues/Issue10',
];
$config = Setup::createAnnotationMetadataConfiguration($paths, true, null, new ArrayCache(), false);
$this->em = EntityManager::create($dbParams, $config);
// Create the ACL object
$this->acl = $this->createACL();
$setup = $this->configureACL();
$setup->setUpEntityManager($this->em, function () {
return $this->acl;
});
// Create the schema
$tool = new SchemaTool($this->em);
$tool->createSchema($this->em->getMetadataFactory()->getAllMetadata());
// Necessary so that SQLite supports CASCADE DELETE
if ($dbParams['driver'] == 'pdo_sqlite') {
$this->em->getConnection()->executeQuery('PRAGMA foreign_keys = ON');
}
}
protected function createACL()
{
return new ACL($this->em);
}
private function configureACL()
{
$setup = new ACLSetup();
$setup->setIdentityClass(Model\User::class);
$roles = [
'ArticleEditor' => [
'resourceType' => Model\Article::class,
'actions' => new Actions([Actions::VIEW, Actions::EDIT])
],
'AllArticlesEditor' => [
'resource' => new ClassResource(Model\Article::class),
'actions' => new Actions([Actions::VIEW, Actions::EDIT])
],
'ArticlePublisher' => [
'resourceType' => Model\Article::class,
'actions' => new Actions([Actions::VIEW, Actions::PUBLISH])
],
'CategoryManager' => [
'resourceType' => Model\Category::class,
'actions' => new Actions([Actions::VIEW])
],
'ArticleEditorCopy' => [
'resourceType' => Model\Article::class,
'actions' => new Actions([Actions::VIEW, Actions::EDIT])
],
'AccountAdmin' => [
'resourceType' => Issues\Issue10\Account::class,
'actions' => Actions::all()
]
];
$setup->registerRoles($roles, $this->acl);
$setup->setActionsClass(Model\Actions::class);
return $setup;
}
/**
* Look into environment variables (defined in phpunit.xml configuration files).
* @return array
*/
private function getDBParams()
{
$dbParams = [
'driver' => 'pdo_sqlite',
'memory' => true,
];
if (isset($GLOBALS['db_type'])) {
$dbParams['driver'] = $GLOBALS['db_type'];
}
if (isset($GLOBALS['db_username'])) {
$dbParams['user'] = $GLOBALS['db_username'];
}
if (isset($GLOBALS['db_password'])) {
$dbParams['password'] = $GLOBALS['db_password'];
}
if (isset($GLOBALS['db_name'])) {
$dbParams['dbname'] = $GLOBALS['db_name'];
}
return $dbParams;
}
private function dropAndCreateDB(Connection $connection)
{
// Drop and recreate the database
if ($connection->getDatabasePlatform()->supportsCreateDropDatabase()) {
$dbname = $connection->getDatabase();
$connection->close();
$connection->getSchemaManager()->dropAndCreateDatabase($dbname);
$connection->connect();
} else {
$sm = $connection->getSchemaManager();
/* @var $schema Schema */
$schema = $sm->createSchema();
$stmts = $schema->toDropSql($connection->getDatabasePlatform());
foreach ($stmts as $stmt) {
$connection->exec($stmt);
}
}
}
}