Browse Source

test: add integration test for room mapper

pull/63/head
sualko 3 years ago
parent
commit
581479b682
  1. 7
      package.json
  2. 12
      phpunit.integration.xml
  3. 141
      tests/Integration/Db/RoomMapperTest.php

7
package.json

@ -17,8 +17,9 @@
"build": "NODE_ENV=production webpack --progress --hide-modules --config webpack.prod.js",
"dev": "NODE_ENV=development webpack --progress --config webpack.dev.js",
"watch": "NODE_ENV=development webpack --progress --watch --config webpack.dev.js",
"test": "run-p --continue-on-error --print-label test:**",
"test:php:unit": "dotenv ./vendor/bin/phpunit -c phpunit.xml",
"test": "run-s --continue-on-error --print-label test:**",
"test:php:unit": "dotenv ./vendor/bin/phpunit -- -c phpunit.xml",
"test:php:integration": "dotenv ./vendor/bin/phpunit -- -c phpunit.integration.xml",
"fix": "run-p --continue-on-error --print-label lint:fix:*",
"lint": "run-p --continue-on-error --print-label lint:*",
"lint:script": "eslint --ext .tsx,.ts ts",
@ -50,7 +51,7 @@
"husky": {
"hooks": {
"pre-commit": "yarn lint",
"pre-push": "yarn test",
"pre-push": "yarn test:php:unit",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},

12
phpunit.integration.xml

@ -0,0 +1,12 @@
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="integration">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">lib</directory>
</whitelist>
</filter>
</phpunit>

141
tests/Integration/Db/RoomMapperTest.php

@ -0,0 +1,141 @@
<?php
namespace OCA\BigBlueButton\Tests\Integration\Db;
use OC;
use OCA\BigBlueButton\Db\Room;
use OCA\BigBlueButton\Db\RoomMapper;
use OCA\BigBlueButton\Db\RoomShare;
use OCA\BigBlueButton\Db\RoomShareMapper;
use PHPUnit\Framework\TestCase;
use OCP\IDBConnection;
class RoomMapperTest extends TestCase {
/** @var IDBConnection */
private $db;
/** @var RoomMapper */
private $mapper;
/** @var RoomShareMapper */
private $shareMapper;
/** @var string */
private $userId;
/** @var string */
private $uid;
public function setUp(): void
{
parent::setUp();
$this->db = OC::$server->getDatabaseConnection();
$this->mapper = new RoomMapper($this->db);
$this->shareMapper = new RoomShareMapper($this->db);
$this->userId = $this->getRandomString();
$this->uid = $this->getRandomString();
}
public function testInsert()
{
$room = $this->insert($this->uid, $this->userId);
$this->assertEquals($this->uid, $room->getUid());
$this->mapper->delete($room);
}
/**
* @depends testInsert
*/
public function testFind()
{
$newRoom = $this->insert($this->uid, $this->userId);
$room = $this->mapper->find($newRoom->getId());
$this->assertEquals($this->uid, $room->getUid());
$this->mapper->delete($room);
}
/**
* @depends testInsert
*/
public function testFindByUid()
{
$newRoom = $this->insert($this->uid, $this->userId);
$room = $this->mapper->findByUid($this->uid);
$this->assertEquals($newRoom->getId(), $room->getId());
$this->mapper->delete($room);
}
/**
* @depends testInsert
*/
public function testFindAll()
{
$room = $this->insert($this->uid, $this->userId);
$foreignRoom1 = $this->insert($this->getRandomString(), $this->getRandomString());
$foreignRoom2 = $this->insert($this->getRandomString(), $this->getRandomString());
$foreignRoom3 = $this->insert($this->getRandomString(), $this->getRandomString());
$this->assertCount(1, $this->mapper->findAll($this->userId, []));
$shares = [];
$shares[] = $this->insertShare($foreignRoom1->getId(), RoomShare::SHARE_TYPE_USER, $this->userId);
$shares[] = $this->insertShare($foreignRoom1->getId(), RoomShare::SHARE_TYPE_GROUP, 'foo bar');
$shares[] = $this->insertShare($foreignRoom2->getId(), RoomShare::SHARE_TYPE_GROUP, 'foo bar');
$shares[] = $this->insertShare($foreignRoom3->getId(), RoomShare::SHARE_TYPE_USER, $this->getRandomString());
$shares[] = $this->insertShare($foreignRoom3->getId(), RoomShare::SHARE_TYPE_USER, $this->userId, RoomShare::PERMISSION_MODERATOR);
$shares[] = $this->insertShare($foreignRoom3->getId(), RoomShare::SHARE_TYPE_GROUP, 'foo bar', RoomShare::PERMISSION_USER);
$rooms = $this->mapper->findAll($this->userId, ['foo bar']);
$this->assertCount(3, $rooms);
$this->mapper->delete($room);
$this->mapper->delete($foreignRoom1);
$this->mapper->delete($foreignRoom2);
$this->mapper->delete($foreignRoom3);
foreach($shares as $share) {
$this->shareMapper->delete($share);
}
}
private function getRandomString(): string
{
return \OC::$server->getSecureRandom()->generate(18, \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE);
}
private function insert($uid, $userId): Room
{
$room = new Room();
$room->setUid($uid);
$room->setName('random name');
$room->setWelcome('');
$room->setMaxParticipants(0);
$room->setAttendeePassword('1');
$room->setModeratorPassword('2');
$room->setRecord(false);
$room->setUserId($userId);
return $this->mapper->insert($room);
}
private function insertShare($id, $type, $with, $permission = RoomShare::PERMISSION_ADMIN): RoomShare
{
$roomShare = new RoomShare();
$roomShare->setRoomId($id);
$roomShare->setShareType($type);
$roomShare->setShareWith($with);
$roomShare->setPermission($permission);
return $this->shareMapper->insert($roomShare);
}
}
Loading…
Cancel
Save