Skip to content

Commit

Permalink
Merge pull request #172 from dbannik/#171
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper authored Sep 19, 2023
2 parents 81a424b + b777344 commit a3e5492
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 52 deletions.
6 changes: 4 additions & 2 deletions src/Model/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DH\Auditor\Model;

use DateTimeImmutable;

/**
* @see \DH\Auditor\Tests\Model\EntryTest
*/
Expand Down Expand Up @@ -31,7 +33,7 @@ final class Entry

private ?string $ip = null;

private string $created_at;
private DateTimeImmutable $created_at;

/**
* Get the value of id.
Expand Down Expand Up @@ -110,7 +112,7 @@ public function getIp(): ?string
/**
* Get the value of created_at.
*/
public function getCreatedAt(): string
public function getCreatedAt(): DateTimeImmutable
{
return $this->created_at;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Provider/Doctrine/Persistence/Reader/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DH\Auditor\Provider\Doctrine\Persistence\Reader;

use DateTimeImmutable;
use DateTimeZone;
use DH\Auditor\Exception\InvalidArgumentException;
use DH\Auditor\Model\Entry;
use DH\Auditor\Provider\Doctrine\Persistence\Helper\SchemaHelper;
Expand Down Expand Up @@ -68,10 +70,13 @@ final class Query

private int $limit = 0;

public function __construct(string $table, Connection $connection)
private DateTimeZone $timezone;

public function __construct(string $table, Connection $connection, string $timezone)
{
$this->connection = $connection;
$this->table = $table;
$this->timezone = new DateTimeZone($timezone);

foreach ($this->getSupportedFilters() as $filterType) {
$this->filters[$filterType] = [];
Expand All @@ -89,6 +94,7 @@ public function execute(): array
$result = [];
\assert($statement instanceof Result);
foreach ($statement->fetchAllAssociative() as $row) {
$row['created_at'] = new DateTimeImmutable($row['created_at'], $this->timezone);
$result[] = Entry::fromArray($row);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Provider/Doctrine/Persistence/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function createQuery(string $entity, array $options = []): Query
$this->configureOptions($resolver);
$config = $resolver->resolve($options);

/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity($entity);
$connection = $this->provider->getStorageServiceForEntity($entity)->getEntityManager()->getConnection();
$timezone = $this->provider->getAuditor()->getConfiguration()->getTimezone();

$query = new Query($this->getEntityAuditTableName($entity), $storageService->getEntityManager()->getConnection());
$query = new Query($this->getEntityAuditTableName($entity), $connection, $timezone);
$query
->addOrderBy(Query::CREATED_AT, 'DESC')
->addOrderBy(Query::ID, 'DESC')
Expand Down
34 changes: 16 additions & 18 deletions tests/Model/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DH\Auditor\Tests\Model;

use DateTimeImmutable;
use DH\Auditor\Model\Entry;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
Expand All @@ -14,27 +15,24 @@
#[\PHPUnit\Framework\Attributes\Small]
final class EntryTest extends TestCase
{
/**
* @var array<string, int|string>
*/
private const ATTRIBUTES = [
'id' => 1,
'type' => 'type',
'object_id' => '1',
'diffs' => '{}',
'blame_id' => 1,
'blame_user' => 'John Doe',
'blame_user_fqdn' => 'Acme\User',
'blame_user_firewall' => 'main',
'ip' => '1.2.3.4',
'created_at' => 'now',
];

public function testAccessors(): void
{
$attributes = [
'id' => 1,
'type' => 'type',
'object_id' => '1',
'diffs' => '{}',
'blame_id' => 1,
'blame_user' => 'John Doe',
'blame_user_fqdn' => 'Acme\User',
'blame_user_firewall' => 'main',
'ip' => '1.2.3.4',
'created_at' => new DateTimeImmutable(),
];

$entry = new Entry();
$reflectionClass = new ReflectionClass(Entry::class);
foreach (self::ATTRIBUTES as $name => $value) {
foreach ($attributes as $name => $value) {
$attribute = $reflectionClass->getProperty($name);
$attribute->setAccessible(true);
$attribute->setValue($entry, $value);
Expand All @@ -50,7 +48,7 @@ public function testAccessors(): void
self::assertSame('Acme\User', $entry->getUserFqdn(), 'Entry::getUserFqdn() is ok.');
self::assertSame('main', $entry->getUserFirewall(), 'Entry::getUserFirewall() is ok.');
self::assertSame('1.2.3.4', $entry->getIp(), 'Entry::getIp() is ok.');
self::assertSame('now', $entry->getCreatedAt(), 'Entry::getCreatedAt() is ok.');
self::assertSame($attributes['created_at'], $entry->getCreatedAt(), 'Entry::getCreatedAt() is ok.');
}

public function testUndefinedUser(): void
Expand Down
54 changes: 27 additions & 27 deletions tests/Provider/Doctrine/Persistence/Reader/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class QueryTest extends TestCase

public function testNoFiltersByDefault(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');

$filters = $query->getFilters();
foreach ($filters as $values) {
Expand All @@ -36,7 +36,7 @@ public function testNoFiltersByDefault(): void
#[\PHPUnit\Framework\Attributes\Depends('testNoFiltersByDefault')]
public function testAddSimpleFilter(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$filter1 = new SimpleFilter(Query::TRANSACTION_HASH, '123abc');
$query->addFilter($filter1);

Expand All @@ -62,7 +62,7 @@ public function testAddSimpleFilter(): void
#[\PHPUnit\Framework\Attributes\Depends('testAddSimpleFilter')]
public function testAddUnexpectedFilter(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');

$this->expectException(InvalidArgumentException::class);

Expand All @@ -73,23 +73,23 @@ public function testAddUnexpectedFilter(): void
public function testAddRangeFilter(): void
{
// only min bound
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$filter = new RangeFilter(Query::OBJECT_ID, 1);
$query->addFilter($filter);

$filters = $query->getFilters();
self::assertSame([$filter], $filters[Query::OBJECT_ID], 'Range filter with min bound only is added.');

// only max bound
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$filter = new RangeFilter(Query::OBJECT_ID, null, 1);
$query->addFilter($filter);

$filters = $query->getFilters();
self::assertSame([$filter], $filters[Query::OBJECT_ID], 'Range filter with max bound only is added.');

// min and max bound
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$filter = new RangeFilter(Query::OBJECT_ID, 5, 15);
$query->addFilter($filter);

Expand All @@ -104,23 +104,23 @@ public function testAddDateRangeFilter(): void
$max = new DateTimeImmutable('+1 day');

// only min bound
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$filter = new DateRangeFilter(Query::CREATED_AT, $min);
$query->addFilter($filter);

$filters = $query->getFilters();
self::assertSame([$filter], $filters[Query::CREATED_AT], 'Date range filter with min bound only is added.');

// only max bound
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$filter = new DateRangeFilter(Query::CREATED_AT, null, $max);
$query->addFilter($filter);

$filters = $query->getFilters();
self::assertSame([$filter], $filters[Query::CREATED_AT], 'Date range filter with max bound only is added.');

// min and max bound
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$filter = new DateRangeFilter(Query::CREATED_AT, $min, $max);
$query->addFilter($filter);

Expand All @@ -130,15 +130,15 @@ public function testAddDateRangeFilter(): void

public function testNoOrderByByDefault(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');

self::assertSame([], $query->getOrderBy(), 'No ORDER BY by default.');
}

#[\PHPUnit\Framework\Attributes\Depends('testNoOrderByByDefault')]
public function testAddOrderBy(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$query->addOrderBy(Query::TRANSACTION_HASH, 'ASC');

$orderBy = $query->getOrderBy();
Expand All @@ -162,7 +162,7 @@ public function testAddOrderBy(): void
#[\PHPUnit\Framework\Attributes\Depends('testAddOrderBy')]
public function testAddUnexpectedOrderBy(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');

$this->expectException(InvalidArgumentException::class);

Expand All @@ -171,15 +171,15 @@ public function testAddUnexpectedOrderBy(): void

public function testNoLimitNoOffsetByDefault(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');

self::assertSame([0, 0], $query->getLimit(), 'No LIMIT by default.');
}

#[\PHPUnit\Framework\Attributes\Depends('testNoLimitNoOffsetByDefault')]
public function testLimitWithoutOffset(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$query->limit(10);

self::assertSame([10, 0], $query->getLimit(), 'LIMIT without offset is OK.');
Expand All @@ -188,7 +188,7 @@ public function testLimitWithoutOffset(): void
#[\PHPUnit\Framework\Attributes\Depends('testNoLimitNoOffsetByDefault')]
public function testWithLimitAndOffset(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$query->limit(10, 50);

self::assertSame([10, 50], $query->getLimit(), 'LIMIT with offset is OK.');
Expand All @@ -197,7 +197,7 @@ public function testWithLimitAndOffset(): void
#[\PHPUnit\Framework\Attributes\Depends('testNoLimitNoOffsetByDefault')]
public function testLimitNegativeLimit(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');

$this->expectException(InvalidArgumentException::class);
$query->limit(-1, 50);
Expand All @@ -206,7 +206,7 @@ public function testLimitNegativeLimit(): void
#[\PHPUnit\Framework\Attributes\Depends('testNoLimitNoOffsetByDefault')]
public function testLimitNegativeOffset(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');

$this->expectException(InvalidArgumentException::class);
$query->limit(0, -50);
Expand All @@ -216,7 +216,7 @@ public function testLimitNegativeOffset(): void
#[\PHPUnit\Framework\Attributes\Depends('testAddOrderBy')]
public function testBuildQueryBuilderDefault(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');
$queryBuilder = $reflectedMethod->invokeArgs($query, []);

Expand All @@ -230,7 +230,7 @@ public function testBuildQueryBuilderDefault(): void
#[\PHPUnit\Framework\Attributes\Depends('testBuildQueryBuilderDefault')]
public function testBuildQueryBuilderSimpleFilter(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

// test SQL query with 1 filter
Expand Down Expand Up @@ -267,7 +267,7 @@ public function testBuildQueryBuilderSimpleFilter(): void
#[\PHPUnit\Framework\Attributes\Depends('testBuildQueryBuilderDefault')]
public function testBuildQueryBuilderOrderBy(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

// test SQL query with 1 ORDER BY
Expand All @@ -286,7 +286,7 @@ public function testBuildQueryBuilderOrderBy(): void
#[\PHPUnit\Framework\Attributes\Depends('testBuildQueryBuilderDefault')]
public function testBuildQueryBuilderLimit(): void
{
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

// test SQL query with LIMIT
Expand All @@ -306,7 +306,7 @@ public function testBuildQueryBuilderLimit(): void
public function testBuildQueryBuilderRangeFilter(): void
{
// test SQL query with a range filter, min bound only
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

$expectedQuery = 'SELECT * FROM author_audit at WHERE object_id >= :min_object_id';
Expand All @@ -315,7 +315,7 @@ public function testBuildQueryBuilderRangeFilter(): void
self::assertSame($expectedQuery, $queryBuilder->getSQL(), 'SQL query is OK with a range filter with min bound only.');

// test SQL query with a range filter, max bound only
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

$expectedQuery = 'SELECT * FROM author_audit at WHERE object_id <= :max_object_id';
Expand All @@ -324,7 +324,7 @@ public function testBuildQueryBuilderRangeFilter(): void
self::assertSame($expectedQuery, $queryBuilder->getSQL(), 'SQL query is OK with a range filter with max bound only.');

// test SQL query with a range filter with both bounds
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

$expectedQuery = 'SELECT * FROM author_audit at WHERE object_id >= :min_object_id AND object_id <= :max_object_id';
Expand All @@ -340,7 +340,7 @@ public function testBuildQueryBuilderDateRangeFilter(): void
$max = new DateTimeImmutable('+1 day');

// test SQL query with a date range filter, min bound only
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

$expectedQuery = 'SELECT * FROM author_audit at WHERE object_id >= :min_object_id';
Expand All @@ -349,7 +349,7 @@ public function testBuildQueryBuilderDateRangeFilter(): void
self::assertSame($expectedQuery, $queryBuilder->getSQL(), 'SQL query is OK with a range filter with min bound only.');

// test SQL query with a date range filter, max bound only
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

$expectedQuery = 'SELECT * FROM author_audit at WHERE object_id <= :max_object_id';
Expand All @@ -358,7 +358,7 @@ public function testBuildQueryBuilderDateRangeFilter(): void
self::assertSame($expectedQuery, $queryBuilder->getSQL(), 'SQL query is OK with a range filter with max bound only.');

// test SQL query with a date range filter with both bounds
$query = new Query('author_audit', $this->createConnection());
$query = new Query('author_audit', $this->createConnection(), 'UTC');
$reflectedMethod = $this->reflectMethod($query, 'buildQueryBuilder');

$expectedQuery = 'SELECT * FROM author_audit at WHERE object_id >= :min_object_id AND object_id <= :max_object_id';
Expand Down
2 changes: 1 addition & 1 deletion tests/Provider/Doctrine/Persistence/Reader/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testGetAudits(): void
self::assertIsString($audits[0]->getUserFqdn());
self::assertSame('main', $audits[0]->getUserFirewall());
self::assertIsString($audits[0]->getIp());
self::assertMatchesRegularExpression('#\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}#', $audits[0]->getCreatedAt());
self::assertInstanceOf(DateTimeImmutable::class, $audits[0]->getCreatedAt());

$expected = [
'Inserted DH\\Auditor\\Tests\\Provider\\Doctrine\\Fixtures\\Entity\\Standard\\Blog\\Author#3: [email: [email protected], fullname: Luke Skywalker]',
Expand Down

0 comments on commit a3e5492

Please sign in to comment.