2019-12-26 23:00:29 +13:00
|
|
|
<?php
|
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
namespace Tests\Unit\Event;
|
2019-12-26 23:00:29 +13:00
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
use Appwrite\Event\Event;
|
2022-12-21 00:07:49 +13:00
|
|
|
use Appwrite\URL\URL;
|
2022-03-29 21:30:57 +13:00
|
|
|
use InvalidArgumentException;
|
2019-12-26 23:00:29 +13:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-06-29 08:45:36 +12:00
|
|
|
use Utopia\App;
|
2022-12-21 00:07:49 +13:00
|
|
|
use Utopia\DSN\DSN;
|
|
|
|
use Utopia\Queue;
|
2019-12-26 23:00:29 +13:00
|
|
|
|
2019-12-26 23:20:09 +13:00
|
|
|
class EventTest extends TestCase
|
2019-12-26 23:00:29 +13:00
|
|
|
{
|
2022-08-01 22:22:04 +12:00
|
|
|
protected ?Event $object = null;
|
|
|
|
protected string $queue = '';
|
2019-12-26 23:00:29 +13:00
|
|
|
|
2020-10-01 10:52:28 +13:00
|
|
|
public function setUp(): void
|
2019-12-26 23:00:29 +13:00
|
|
|
{
|
2020-06-29 08:45:36 +12:00
|
|
|
$redisHost = App::getEnv('_APP_REDIS_HOST', '');
|
|
|
|
$redisPort = App::getEnv('_APP_REDIS_PORT', '');
|
2022-03-29 21:30:57 +13:00
|
|
|
\Resque::setBackend($redisHost . ':' . $redisPort);
|
|
|
|
|
2022-12-21 00:07:49 +13:00
|
|
|
$fallbackForRedis = URL::unparse([
|
|
|
|
'scheme' => 'redis',
|
|
|
|
'host' => App::getEnv('_APP_REDIS_HOST', 'redis'),
|
|
|
|
'port' => App::getEnv('_APP_REDIS_PORT', '6379'),
|
|
|
|
'user' => App::getEnv('_APP_REDIS_USER', ''),
|
|
|
|
'pass' => App::getEnv('_APP_REDIS_PASS', ''),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$dsn = App::getEnv('_APP_CONNECTIONS_QUEUE', $fallbackForRedis);
|
|
|
|
$dsn = explode('=', $dsn);
|
|
|
|
$dsn = $dsn[1] ?? '';
|
|
|
|
|
|
|
|
$dsn = new DSN($dsn);
|
|
|
|
|
|
|
|
$connection = new Queue\Connection\Redis($dsn->getHost(), $dsn->getPort());
|
|
|
|
|
2019-12-26 23:00:29 +13:00
|
|
|
$this->queue = 'v1-tests' . uniqid();
|
2022-12-21 00:07:49 +13:00
|
|
|
$this->object = new Event($this->queue, 'TestsV1', $connection);
|
2019-12-26 23:00:29 +13:00
|
|
|
}
|
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
public function testQueue(): void
|
2020-12-07 21:51:12 +13:00
|
|
|
{
|
|
|
|
$this->assertEquals($this->queue, $this->object->getQueue());
|
|
|
|
|
|
|
|
$this->object->setQueue('demo');
|
2022-03-29 21:30:57 +13:00
|
|
|
|
2020-12-07 21:51:12 +13:00
|
|
|
$this->assertEquals('demo', $this->object->getQueue());
|
2022-03-29 21:30:57 +13:00
|
|
|
|
2020-12-07 21:51:12 +13:00
|
|
|
$this->object->setQueue($this->queue);
|
|
|
|
}
|
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
public function testClass(): void
|
2020-12-07 21:51:12 +13:00
|
|
|
{
|
|
|
|
$this->assertEquals('TestsV1', $this->object->getClass());
|
|
|
|
|
|
|
|
$this->object->setClass('TestsV2');
|
2022-03-29 21:30:57 +13:00
|
|
|
|
2020-12-07 21:51:12 +13:00
|
|
|
$this->assertEquals('TestsV2', $this->object->getClass());
|
2022-03-29 21:30:57 +13:00
|
|
|
|
2020-12-07 21:51:12 +13:00
|
|
|
$this->object->setClass('TestsV1');
|
|
|
|
}
|
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
public function testParams(): void
|
2019-12-26 23:00:29 +13:00
|
|
|
{
|
|
|
|
$this->object
|
2020-10-30 11:20:32 +13:00
|
|
|
->setParam('eventKey1', 'eventValue1')
|
2022-03-29 21:30:57 +13:00
|
|
|
->setParam('eventKey2', 'eventValue2');
|
2019-12-26 23:00:29 +13:00
|
|
|
|
|
|
|
$this->object->trigger();
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
$this->assertEquals('eventValue1', $this->object->getParam('eventKey1'));
|
|
|
|
$this->assertEquals('eventValue2', $this->object->getParam('eventKey2'));
|
2020-10-30 11:20:32 +13:00
|
|
|
$this->assertEquals(null, $this->object->getParam('eventKey3'));
|
2019-12-26 23:00:29 +13:00
|
|
|
$this->assertEquals(\Resque::size($this->queue), 1);
|
|
|
|
}
|
2020-07-05 01:06:23 +12:00
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
public function testReset(): void
|
2020-07-05 01:06:23 +12:00
|
|
|
{
|
|
|
|
$this->object
|
2020-10-30 11:20:32 +13:00
|
|
|
->setParam('eventKey1', 'eventValue1')
|
2022-03-29 21:30:57 +13:00
|
|
|
->setParam('eventKey2', 'eventValue2');
|
2020-07-05 01:06:23 +12:00
|
|
|
|
2020-10-30 11:20:32 +13:00
|
|
|
$this->assertEquals('eventValue1', $this->object->getParam('eventKey1'));
|
|
|
|
$this->assertEquals('eventValue2', $this->object->getParam('eventKey2'));
|
2020-07-05 01:06:23 +12:00
|
|
|
|
|
|
|
$this->object->reset();
|
|
|
|
|
2020-10-30 11:20:32 +13:00
|
|
|
$this->assertEquals(null, $this->object->getParam('eventKey1'));
|
|
|
|
$this->assertEquals(null, $this->object->getParam('eventKey2'));
|
|
|
|
$this->assertEquals(null, $this->object->getParam('eventKey3'));
|
2020-07-05 01:06:23 +12:00
|
|
|
}
|
2022-03-29 21:30:57 +13:00
|
|
|
|
2022-08-01 22:22:04 +12:00
|
|
|
public function testGenerateEvents(): void
|
2022-03-29 21:30:57 +13:00
|
|
|
{
|
2022-04-04 18:30:07 +12:00
|
|
|
$event = Event::generateEvents('users.[userId].create', [
|
|
|
|
'userId' => 'torsten'
|
|
|
|
]);
|
|
|
|
$this->assertCount(4, $event);
|
|
|
|
$this->assertContains('users.torsten.create', $event);
|
|
|
|
$this->assertContains('users.torsten', $event);
|
|
|
|
$this->assertContains('users.*.create', $event);
|
|
|
|
$this->assertContains('users.*', $event);
|
2022-03-29 21:30:57 +13:00
|
|
|
|
|
|
|
$event = Event::generateEvents('users.[userId].update.email', [
|
|
|
|
'userId' => 'torsten'
|
|
|
|
]);
|
|
|
|
$this->assertCount(6, $event);
|
|
|
|
$this->assertContains('users.torsten.update.email', $event);
|
|
|
|
$this->assertContains('users.torsten.update', $event);
|
|
|
|
$this->assertContains('users.torsten', $event);
|
|
|
|
$this->assertContains('users.*.update.email', $event);
|
|
|
|
$this->assertContains('users.*.update', $event);
|
|
|
|
$this->assertContains('users.*', $event);
|
|
|
|
|
|
|
|
$event = Event::generateEvents('collections.[collectionId].documents.[documentId].create', [
|
|
|
|
'collectionId' => 'chapters',
|
|
|
|
'documentId' => 'prolog',
|
|
|
|
]);
|
2022-04-19 04:21:45 +12:00
|
|
|
$this->assertCount(10, $event);
|
|
|
|
|
2022-03-29 21:30:57 +13:00
|
|
|
$this->assertContains('collections.chapters.documents.prolog.create', $event);
|
|
|
|
$this->assertContains('collections.chapters.documents.prolog', $event);
|
|
|
|
$this->assertContains('collections.chapters.documents.*.create', $event);
|
|
|
|
$this->assertContains('collections.chapters.documents.*', $event);
|
2022-04-19 04:21:45 +12:00
|
|
|
$this->assertContains('collections.chapters', $event);
|
2022-03-29 21:30:57 +13:00
|
|
|
$this->assertContains('collections.*.documents.prolog.create', $event);
|
|
|
|
$this->assertContains('collections.*.documents.prolog', $event);
|
|
|
|
$this->assertContains('collections.*.documents.*.create', $event);
|
|
|
|
$this->assertContains('collections.*.documents.*', $event);
|
2022-04-19 04:21:45 +12:00
|
|
|
$this->assertContains('collections.*', $event);
|
2022-03-29 21:30:57 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$event = Event::generateEvents('databases.[databaseId].collections.[collectionId].documents.[documentId].create', [
|
|
|
|
'databaseId' => 'chaptersDB',
|
|
|
|
'collectionId' => 'chapters',
|
|
|
|
'documentId' => 'prolog',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertCount(22, $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.chapters.documents.prolog.create', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.chapters.documents.prolog', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.chapters.documents.*.create', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.chapters.documents.*', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.chapters', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.*.documents.prolog.create', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.*.documents.prolog', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.*', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB', $event);
|
|
|
|
$this->assertContains('databases.*.collections.chapters.documents.prolog.create', $event);
|
|
|
|
$this->assertContains('databases.*.collections.chapters.documents.prolog', $event);
|
|
|
|
$this->assertContains('databases.*.collections.chapters', $event);
|
|
|
|
$this->assertContains('databases.*.collections.*.documents.*.create', $event);
|
|
|
|
$this->assertContains('databases.*.collections.*.documents.*', $event);
|
|
|
|
$this->assertContains('databases.*.collections.*', $event);
|
|
|
|
$this->assertContains('databases.*', $event);
|
|
|
|
$this->assertContains('databases.*.collections.*.documents.prolog', $event);
|
|
|
|
$this->assertContains('databases.*.collections.*.documents.prolog.create', $event);
|
|
|
|
$this->assertContains('databases.*.collections.chapters.documents.*', $event);
|
|
|
|
$this->assertContains('databases.*.collections.chapters.documents.*.create', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.*.documents.*', $event);
|
|
|
|
$this->assertContains('databases.chaptersDB.collections.*.documents.*.create', $event);
|
|
|
|
|
|
|
|
|
2022-03-29 21:30:57 +13:00
|
|
|
try {
|
|
|
|
$event = Event::generateEvents('collections.[collectionId].documents.[documentId].create', [
|
|
|
|
'collectionId' => 'chapters'
|
|
|
|
]);
|
|
|
|
$this->fail();
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$this->assertInstanceOf(InvalidArgumentException::class, $th, 'An invalid exception was thrown');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$event = Event::generateEvents('collections.[collectionId].documents.[documentId].create');
|
|
|
|
$this->fail();
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
$this->assertInstanceOf(InvalidArgumentException::class, $th, 'An invalid exception was thrown');
|
|
|
|
}
|
|
|
|
}
|
2020-10-01 10:52:28 +13:00
|
|
|
}
|