Skip to content

add tests #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.9"
services:
postgres:
image: postgres:15.0-alpine
environment:
TZ: Asia/Tokyo
POSTGRES_DB: test
POSTGRES_USER: test
POSTGRES_PASSWORD: password
POSTGRES_INITDB_ARGS: --encoding=UTF-8 --locale=C
ports:
- "5432:5432"
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
"require-dev": {
"phpunit/phpunit": "^10.0",
"friendsofphp/php-cs-fixer": "^3.15",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.10",
"symfony/cache": "^7.1"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Linkage\\DoctrineRowLevelSecurity\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Linkage\\DoctrineRowLevelSecurity\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "Hiromi Hishida",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
Expand Down
24 changes: 24 additions & 0 deletions tests/Entity/Dog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Linkage\DoctrineRowLevelSecurity\Tests\Entity;

use Doctrine\ORM\Mapping as ORM;
use Linkage\DoctrineRowLevelSecurity\RowLevelSecurity;

#[ORM\Table]
#[ORM\Entity]
#[RowLevelSecurity(name: 'dog_policy', role: 'dog_owner', using: 'owner_id = current_user::uuid')]
class Dog
{
#[ORM\Id]
#[ORM\Column(type: 'guid')]
public string $id;

#[ORM\ManyToOne(targetEntity: DogOwner::class)]
public DogOwner $owner;

#[ORM\Column(type: 'string')]
public string $name;
}
19 changes: 19 additions & 0 deletions tests/Entity/DogOwner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Linkage\DoctrineRowLevelSecurity\Tests\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table]
class DogOwner
{
#[ORM\Id]
#[ORM\Column(type: 'guid')]
public string $id;

#[ORM\Column(type: 'string')]
public string $name;
}
77 changes: 77 additions & 0 deletions tests/Functional/RowLevelSecurityUsageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Linkage\DoctrineRowLevelSecurity\Tests\Functional;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\Tools\SchemaTool;
use Linkage\DoctrineRowLevelSecurity\RowLevelSecurityAwarePostgreSqlConnection;
use Linkage\DoctrineRowLevelSecurity\RowLevelSecurityListener;
use Linkage\DoctrineRowLevelSecurity\Tests\Entity\Dog;
use Linkage\DoctrineRowLevelSecurity\Tests\Entity\DogOwner;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

class RowLevelSecurityUsageTest extends TestCase
{
private Connection $conn;

protected function setUp(): void
{
$connectionParams = [
'dbname' => 'test',
'user' => 'test',
'password' => 'password',
'host' => 'localhost',
'driver' => 'pdo_pgsql',
'wrapperClass' => RowLevelSecurityAwarePostgreSqlConnection::class,
];
$conn = DriverManager::getConnection($connectionParams);
foreach (explode(';', (string) file_get_contents(__DIR__ . '/drop_table.sql')) as $dropSql) {
if (trim($dropSql) === '') {
continue;
}
$conn->executeQuery($dropSql);
}
foreach (explode(';', (string) file_get_contents(__DIR__ . '/create_table.sql')) as $createSql) {
if (trim($createSql) === '') {
continue;
}
$conn->executeQuery($createSql);
}

$configuration = ORMSetup::createAttributeMetadataConfiguration(
paths: [__DIR__."/../Entity"],
isDevMode: true,
cache: new ArrayAdapter(),
);
$this->em = new EntityManager(
$conn,
$configuration,
new EventManager(),
);
$this->em->getEventManager()->addEventSubscriber(new RowLevelSecurityListener());
}

public function testCreateSchema(): void
{
$schemaTool = new SchemaTool($this->em);
$this->em->getConnection()->getDatabasePlatform()->setEventManager($this->em->getEventManager());
$classMetadataFactory = new ClassMetadataFactory();
$classMetadataFactory->setEntityManager($this->em);
$sql = $schemaTool->getCreateSchemaSql([
$classMetadataFactory->getMetadataFor(DogOwner::class),
$classMetadataFactory->getMetadataFor(Dog::class),
]);

$this->assertContains('CREATE POLICY dog_policy ON Dog TO dog_owner USING (owner_id = current_user::uuid)', $sql);
$this->assertContains('GRANT ALL ON TABLE Dog TO dog_owner', $sql);
$this->assertContains('ALTER TABLE Dog ENABLE ROW LEVEL SECURITY', $sql);
}
}
12 changes: 12 additions & 0 deletions tests/Functional/create_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE dog_owner
(
id uuid primary key,
name varchar(255) not null
);
CREATE TABLE dog
(
id uuid primary key,
owner_id uuid not null,
name varchar(255) not null
);
ALTER TABLE dog ADD CONSTRAINT user_id_fk FOREIGN KEY (owner_id) REFERENCES dog_owner (id);
2 changes: 2 additions & 0 deletions tests/Functional/drop_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
drop table if exists dog;
drop table if exists dog_owner;