Skip to content

Commit 8039cd2

Browse files
authored
Merge pull request #5 from 77web/feat/add-tests
add tests
2 parents a7229f4 + ccee48a commit 8039cd2

File tree

8 files changed

+154
-2
lines changed

8 files changed

+154
-2
lines changed

compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: "3.9"
2+
services:
3+
postgres:
4+
image: postgres:15.0-alpine
5+
environment:
6+
TZ: Asia/Tokyo
7+
POSTGRES_DB: test
8+
POSTGRES_USER: test
9+
POSTGRES_PASSWORD: password
10+
POSTGRES_INITDB_ARGS: --encoding=UTF-8 --locale=C
11+
ports:
12+
- "5432:5432"

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
"require-dev": {
1010
"phpunit/phpunit": "^10.0",
1111
"friendsofphp/php-cs-fixer": "^3.15",
12-
"phpstan/phpstan": "^1.10"
12+
"phpstan/phpstan": "^1.10",
13+
"symfony/cache": "^7.1"
1314
},
1415
"license": "MIT",
1516
"autoload": {
1617
"psr-4": {
1718
"Linkage\\DoctrineRowLevelSecurity\\": "src/"
1819
}
1920
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"Linkage\\DoctrineRowLevelSecurity\\Tests\\": "tests/"
24+
}
25+
},
2026
"authors": [
2127
{
2228
"name": "Hiromi Hishida",

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
bootstrap="vendor/autoload.php"
55
cacheDirectory=".phpunit.cache"
66
executionOrder="depends,defects"
7-
requireCoverageMetadata="true"
7+
requireCoverageMetadata="false"
88
beStrictAboutCoverageMetadata="true"
99
beStrictAboutOutputDuringTests="true"
1010
failOnRisky="true"

tests/Entity/Dog.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Linkage\DoctrineRowLevelSecurity\Tests\Entity;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Linkage\DoctrineRowLevelSecurity\RowLevelSecurity;
9+
10+
#[ORM\Table]
11+
#[ORM\Entity]
12+
#[RowLevelSecurity(name: 'dog_policy', role: 'dog_owner', using: 'owner_id = current_user::uuid')]
13+
class Dog
14+
{
15+
#[ORM\Id]
16+
#[ORM\Column(type: 'guid')]
17+
public string $id;
18+
19+
#[ORM\ManyToOne(targetEntity: DogOwner::class)]
20+
public DogOwner $owner;
21+
22+
#[ORM\Column(type: 'string')]
23+
public string $name;
24+
}

tests/Entity/DogOwner.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Linkage\DoctrineRowLevelSecurity\Tests\Entity;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
9+
#[ORM\Entity]
10+
#[ORM\Table]
11+
class DogOwner
12+
{
13+
#[ORM\Id]
14+
#[ORM\Column(type: 'guid')]
15+
public string $id;
16+
17+
#[ORM\Column(type: 'string')]
18+
public string $name;
19+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Linkage\DoctrineRowLevelSecurity\Tests\Functional;
6+
7+
use Doctrine\Common\EventManager;
8+
use Doctrine\DBAL\Connection;
9+
use Doctrine\DBAL\DriverManager;
10+
use Doctrine\ORM\EntityManager;
11+
use Doctrine\ORM\Mapping\ClassMetadataFactory;
12+
use Doctrine\ORM\ORMSetup;
13+
use Doctrine\ORM\Tools\SchemaTool;
14+
use Linkage\DoctrineRowLevelSecurity\RowLevelSecurityAwarePostgreSqlConnection;
15+
use Linkage\DoctrineRowLevelSecurity\RowLevelSecurityListener;
16+
use Linkage\DoctrineRowLevelSecurity\Tests\Entity\Dog;
17+
use Linkage\DoctrineRowLevelSecurity\Tests\Entity\DogOwner;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
20+
21+
class RowLevelSecurityUsageTest extends TestCase
22+
{
23+
private Connection $conn;
24+
25+
protected function setUp(): void
26+
{
27+
$connectionParams = [
28+
'dbname' => 'test',
29+
'user' => 'test',
30+
'password' => 'password',
31+
'host' => 'localhost',
32+
'driver' => 'pdo_pgsql',
33+
'wrapperClass' => RowLevelSecurityAwarePostgreSqlConnection::class,
34+
];
35+
$conn = DriverManager::getConnection($connectionParams);
36+
foreach (explode(';', (string) file_get_contents(__DIR__ . '/drop_table.sql')) as $dropSql) {
37+
if (trim($dropSql) === '') {
38+
continue;
39+
}
40+
$conn->executeQuery($dropSql);
41+
}
42+
foreach (explode(';', (string) file_get_contents(__DIR__ . '/create_table.sql')) as $createSql) {
43+
if (trim($createSql) === '') {
44+
continue;
45+
}
46+
$conn->executeQuery($createSql);
47+
}
48+
49+
$configuration = ORMSetup::createAttributeMetadataConfiguration(
50+
paths: [__DIR__."/../Entity"],
51+
isDevMode: true,
52+
cache: new ArrayAdapter(),
53+
);
54+
$this->em = new EntityManager(
55+
$conn,
56+
$configuration,
57+
new EventManager(),
58+
);
59+
$this->em->getEventManager()->addEventSubscriber(new RowLevelSecurityListener());
60+
}
61+
62+
public function testCreateSchema(): void
63+
{
64+
$schemaTool = new SchemaTool($this->em);
65+
$this->em->getConnection()->getDatabasePlatform()->setEventManager($this->em->getEventManager());
66+
$classMetadataFactory = new ClassMetadataFactory();
67+
$classMetadataFactory->setEntityManager($this->em);
68+
$sql = $schemaTool->getCreateSchemaSql([
69+
$classMetadataFactory->getMetadataFor(DogOwner::class),
70+
$classMetadataFactory->getMetadataFor(Dog::class),
71+
]);
72+
73+
$this->assertContains('CREATE POLICY dog_policy ON Dog TO dog_owner USING (owner_id = current_user::uuid)', $sql);
74+
$this->assertContains('GRANT ALL ON TABLE Dog TO dog_owner', $sql);
75+
$this->assertContains('ALTER TABLE Dog ENABLE ROW LEVEL SECURITY', $sql);
76+
}
77+
}

tests/Functional/create_table.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE dog_owner
2+
(
3+
id uuid primary key,
4+
name varchar(255) not null
5+
);
6+
CREATE TABLE dog
7+
(
8+
id uuid primary key,
9+
owner_id uuid not null,
10+
name varchar(255) not null
11+
);
12+
ALTER TABLE dog ADD CONSTRAINT user_id_fk FOREIGN KEY (owner_id) REFERENCES dog_owner (id);

tests/Functional/drop_table.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
drop table if exists dog;
2+
drop table if exists dog_owner;

0 commit comments

Comments
 (0)