Skip to content

Commit

Permalink
feat: Separate tests underneath two main parents - ApiTest / UITest t…
Browse files Browse the repository at this point in the history
…o be able to use shared recordmanager
  • Loading branch information
KminekMatej committed Sep 27, 2024
1 parent ed9ebef commit db9b4c6
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 116 deletions.
8 changes: 5 additions & 3 deletions app/module/autotest/app/attendance/AttendanceStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use Tymy\Bootstrap;
use Tymy\Module\Attendance\Model\Attendance;
use Tymy\Module\Autotest\ApiTest;
use Tymy\Module\Autotest\Entity\Assert;
use Tymy\Module\Autotest\RequestCase;

use function count;

require getenv("ROOT_DIR") . '/app/Bootstrap.php';
$container = Bootstrap::boot();
Expand All @@ -19,7 +21,7 @@
* @RequestMapping(value = "/attendanceStatusSet/{id}", method = RequestMethod.PUT)
* @RequestMapping(value = "/attendanceStatusSet/{id}", method = RequestMethod.DELETE)
*/
class AttendanceStatusTest extends RequestCase
class AttendanceStatusTest extends ApiTest
{
protected function getBasePath(): string
{
Expand Down Expand Up @@ -174,7 +176,7 @@ private function getStatusSetId(): int
return $allStatusSets[random_int(0, (is_countable($allStatusSets) ? count($allStatusSets) : 0) - 1)]["id"];
}

public function createRecord(): void
public function createRecord(): array
{
//use creator from recorManager - can create status or statusSet
}
Expand Down
12 changes: 6 additions & 6 deletions app/module/autotest/app/attendance/AttendanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

use Tymy\Bootstrap;
use Tymy\Module\Attendance\Model\Attendance;
use Tymy\Module\Autotest\ApiTest;
use Tymy\Module\Autotest\Entity\Assert;
use Tymy\Module\Autotest\RequestCase;

require getenv("ROOT_DIR") . '/app/Bootstrap.php';
$container = Bootstrap::boot();

/**
* Description of AttendanceTest
*/
class AttendanceTest extends RequestCase
class AttendanceTest extends ApiTest
{
private ?int $eventId = null;

Expand All @@ -28,7 +28,7 @@ public function testPost(): void
{
$this->authorizeAdmin();
//pre
$this->eventId = $this->recordManager->createEvent();
$this->eventId = $this->recordManager->createEvent()["id"];
$mocked = $this->mockRecord();

$this->authorizeUser();
Expand Down Expand Up @@ -102,7 +102,7 @@ public function testPost(): void
public function testViewPlanForbidden(): void
{
$this->authorizeAdmin();
$eventId = $this->recordManager->createEvent(null, ["resultRightName" => null]);
$eventId = $this->recordManager->createEvent(null, ["resultRightName" => null])["id"];

$this->authorizeUser();

Expand All @@ -123,7 +123,7 @@ public function testViewPlanForbidden(): void
public function testPermissivePost(): void
{
$this->authorizeAdmin();
$this->eventId = $this->recordManager->createEvent(null, ["viewRightName" => "ADMINONLY", "planRightName" => "ADMINONLY", "resultRightName" => "ADMINMEMBER"]);
$this->eventId = $this->recordManager->createEvent(null, ["viewRightName" => "ADMINONLY", "planRightName" => "ADMINONLY", "resultRightName" => "ADMINMEMBER"])["id"];
$mocked = $this->mockRecord();

$this->authorizeUser();
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testUnknownEvent(): void
$this->request($this->getBasePath(), "POST", $mocked)->expect(404); //event doesnt exist
}

public function createRecord(): void
public function createRecord(): array
{
//attendance are never created solely, always just posted to some event
}
Expand Down
6 changes: 3 additions & 3 deletions app/module/autotest/app/authentication/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
namespace Tymy\Module\Autotest\Authentication;

use Tymy\Bootstrap;
use Tymy\Module\Autotest\RequestCase;
use Tymy\Module\Autotest\ApiTest;

require getenv("ROOT_DIR") . '/app/Bootstrap.php';
$container = Bootstrap::boot();

/**
* Description of IsTest
*/
class IsTest extends RequestCase
class IsTest extends ApiTest
{
public function testUnauthorized(): void
{
Expand All @@ -33,7 +33,7 @@ protected function mockChanges(): array
return [];
}

public function createRecord(): void
public function createRecord(): array
{
//not used in this test
}
Expand Down
15 changes: 7 additions & 8 deletions app/module/autotest/app/debt/DebtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use Nette\Utils\DateTime;
use Tymy\Bootstrap;
use Tymy\Module\Autotest\ApiTest;
use Tymy\Module\Autotest\Entity\Assert;
use Tymy\Module\Autotest\RequestCase;
use Tymy\Module\Core\Model\BaseModel;
use Tymy\Module\Debt\Model\Debt;

require getenv("ROOT_DIR") . '/app/Bootstrap.php';
Expand All @@ -17,7 +16,7 @@
/**
* Description of DebtTest
*/
class DebtTest extends RequestCase
class DebtTest extends ApiTest
{
public function getModule(): string
{
Expand Down Expand Up @@ -70,8 +69,8 @@ public function testTeamDebts(): void
{
//admin can create team debt
$this->authorizeAdmin();
$recordId = $this->recordManager->createDebt(null, ["payeeId" => null, "caption" => "Poplatky 2020", "note" => null]); //create debt for team
$origData = $this->request($this->getBasePath() . "/" . $recordId)->expect(200, "array");
$origData = $this->recordManager->createDebt(null, ["payeeId" => null, "caption" => "Poplatky 2020", "note" => null]); //create debt for team
$recordId = $origData["id"];

//another admin of team debts can see that debt
$this->authorizeAdmin($this->config["user_member_login"], $this->config["user_member_pwd"]);
Expand Down Expand Up @@ -105,7 +104,7 @@ public function testTeamDebts(): void
Assert::true($found, "Debt id $recordId not found in users list");

$chResponse = $this->request($this->getBasePath() . "/" . $recordId, "PUT", $this->mockChanges())->expect(200, "array");//debtor can edit, but the only field that gets edited is paymentSent
Assert::equal($origData->getData()["amount"], $chResponse->getData()["amount"]);//amount didnt change
Assert::equal($origData["amount"], $chResponse->getData()["amount"]);//amount didnt change
$now = new DateTime();
$this->request($this->getBasePath() . "/" . $recordId, "PUT", ["paymentSent" => $this->toJsonDate($now)])->expect(200, "array");
sleep(1);//sleep for one second, to make sure that current datetime is now different than $now variable. So we can check that the paymentSent would be actually changed if something changes it
Expand Down Expand Up @@ -159,7 +158,7 @@ public function testTeamOwesMe(): void
"payeeId" => $this->config["user_test_id"],
"debtorId" => 0,
"caption" => "Tým mi dluží přeplatek za finále MČR, id: " . $this->user->getId()
]);
])["id"];

//admin can mark it as paymentSent
$this->authorizeAdmin($this->config["user_member_login"], $this->config["user_member_pwd"]);
Expand All @@ -183,7 +182,7 @@ public function testCRUDPlural(): void
$this->deleteRecord($recordId);
}

public function createRecord(): int
public function createRecord(): array
{
return $this->recordManager->createDebt();
}
Expand Down
94 changes: 94 additions & 0 deletions app/module/autotest/app/discussion/DiscussionPresenterTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Tymy\Module\Autotest\Discussion;

use Nette;
use Tester\Assert;
use Tester\DomQuery;
use Tymy\Bootstrap;
use Tymy\Module\Autotest\UITest;

use function count;

require getenv("ROOT_DIR") . '/app/Bootstrap.php';
$container = Bootstrap::boot();

class DiscussionPresenterTest extends UITest
{

function testActionDefault()
{
$this->authorizeUser();
$request = new Nette\Application\Request($this->getPresenterName(), 'GET', array('action' => 'default'));
$response = $this->presenter->run($request);

Assert::type('Nette\Application\Responses\TextResponse', $response);

$html = (string) $response->getSource();
$dom = DomQuery::fromHtml($html);

//has navbar
Assert::true($dom->has('div#snippet-navbar-nav'));
//has breadcrumbs
Assert::true($dom->has('div.container'));
Assert::true($dom->has('ol.breadcrumb'));
Assert::equal(count($dom->find('ol.breadcrumb li.breadcrumb-item a[href]')), 1);
Assert::equal(count($dom->find('ol.breadcrumb li.breadcrumb-item')), 2); //last item aint link

Assert::true($dom->has('div.container.discussions'));
Assert::true(count($dom->find('div.container.discussions div.row')) >= 1);
}

function testActionDiscussion($discussionName, $canWrite)
{
$this->authorizeAdmin();
$discussion = $this->recordManager->createDiscussion();
$discussionName = $discussion["name"];

$this->authorizeUser();
$request = new Nette\Application\Request($this->getPresenterName(), 'GET', ['action' => 'discussion', 'discussion' => $discussionName]);
$response = $this->presenter->run($request);

Assert::type('Nette\Application\Responses\TextResponse', $response);

$re = '/&(?!(?:apos|quot|[gl]t|amp);|#)/';

$dom = NULL;
$html = (string)$response->getSource();
//replace unescaped ampersands in html to prevent tests from failing
$html = preg_replace($re, "&amp;", $html);

$dom = DomQuery::fromHtml($html);
//has navbar
Assert::true($dom->has('div#snippet-navbar-nav'));

//has breadcrumbs

Assert::true($dom->has('div.container div.row div.col ol.breadcrumb'));
Assert::equal(count($dom->find('ol.breadcrumb li.breadcrumb-item a[href]')), 2);
Assert::equal(count($dom->find('ol.breadcrumb li.breadcrumb-item')), 3); //last item aint link

Assert::equal(count($dom->find('div.container.my-2 div.row.justify-content-md-center')), $canWrite ? 2 : 1);
if($canWrite){
Assert::true($dom->has('div.container.my-2 div.row.justify-content-md-center div.col-md-10 textarea#addPost'));
Assert::true($dom->has('div.container.my-2 div.row.justify-content-md-center div.col-md-10 div.addPost form.form-inline input.form-control.mr-sm-2'));
Assert::true($dom->has('div.container.my-2 div.row.justify-content-md-center div.col-md-10 div.addPost form.form-inline span.mr-auto input.form-control.btn.btn-outline-success.mr-sm-2'));
Assert::true($dom->has('div.container.my-2 div.row.justify-content-md-center div.col-md-10 div.addPost form.form-inline button.btn.btn-primary'));
}

Assert::true($dom->has('div.container.discussion#snippet--discussion'));
Assert::true(count($dom->find('div.container.discussion#snippet--discussion div.row'))<=20);
}

protected function getPresenterName(): string
{
return "Discussion:Default";
}

public function getModule(): string
{
return "Discussion";
}
}

(new DiscussionPresenterTest($container))->run();
19 changes: 9 additions & 10 deletions app/module/autotest/app/discussion/DiscussionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@

namespace Tymy\Module\Autotest\Discussion;

use Tymy\Bootstrap;
use Nette\Utils\DateTime;
use Tymy\Bootstrap;
use Tymy\Module\Autotest\ApiTest;
use Tymy\Module\Autotest\Entity\Assert;
use Tymy\Module\Core\Model\BaseModel;
use Tymy\Module\Discussion\Model\Discussion;
use Tymy\Module\Autotest\Entity\Assert;
use Tymy\Module\Autotest\RequestCase;
use Tymy\Module\Autotest\SimpleResponse;

require getenv("ROOT_DIR") . '/app/Bootstrap.php';
$container = Bootstrap::boot();

/**
* Description of DiscussionTest
*/
class DiscussionTest extends RequestCase
class DiscussionTest extends ApiTest
{
public function testGet(): void
{
Expand All @@ -44,7 +43,7 @@ public function testCRUDNotPermittedDiscussion(): void
"writeRightName" => "ADMINONLY",
"deleteRightName" => "ADMINONLY",
"stickyRightName" => "ADMINMEMBER",
]);
])["id"];

$pid = $this->request($this->getBasePath() . "/$dId/post", "POST", ["post" => "ADMIN first post " . random_int(0, 10000)])->expect(201)->getData()["id"];

Expand Down Expand Up @@ -98,7 +97,7 @@ public function testPostDiscussion(): void
"writeRightName" => "ADMINONLY",
"deleteRightName" => "ADMINONLY",
"stickyRightName" => "ADMINMEMBER",
]);
])["id"];

$pid = $this->request($this->getBasePath() . "/$dId/post", "POST", ["post" => "ADMIN first post " . random_int(0, 10000)])->expect(201)->getData()["id"];

Expand Down Expand Up @@ -200,7 +199,7 @@ public function testSticky(): void
$dId = $this->recordManager->createDiscussion(null, [
"deleteRightName" => "ADMINONLY",
"stickyRightName" => "ADMINMEMBER",
]);
])["id"];

$this->request($this->getBasePath() . "/$dId/post", "POST", ["post" => "ADMIN first post " . random_int(0, 10000)])->expect(201)->getData();

Expand All @@ -215,7 +214,7 @@ public function testSticky(): void
public function testCRUD(): void
{
$this->authorizeAdmin();
$recordId = $this->createRecord();
$recordId = $this->createRecord()["id"];

$this->request($this->getBasePath() . "/" . $recordId)->expect(200, "array");

Expand All @@ -224,7 +223,7 @@ public function testCRUD(): void
$this->deleteRecord($recordId);
}

public function createRecord(): int
public function createRecord(): array
{
return $this->recordManager->createDiscussion();
}
Expand Down
6 changes: 3 additions & 3 deletions app/module/autotest/app/event/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Tymy\Module\Autotest\Event;

use Tymy\Bootstrap;
use Tymy\Module\Autotest\ApiTest;
use Tymy\Module\Autotest\Entity\Assert;
use Tymy\Module\Autotest\RequestCase;
use Tymy\Module\Event\Model\Event;

require getenv("ROOT_DIR") . '/app/Bootstrap.php';
Expand All @@ -15,7 +15,7 @@
/**
* Description of EventTest
*/
class EventTest extends RequestCase
class EventTest extends ApiTest
{
public function getModule(): string
{
Expand Down Expand Up @@ -80,7 +80,7 @@ public function testEventTypes(): void
$this->request("eventTypes")->expect(200, "array");
}

public function createRecord(): int
public function createRecord(): array
{
return $this->recordManager->createEvent();
}
Expand Down
4 changes: 2 additions & 2 deletions app/module/autotest/app/multiaccount/MultiaccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Tymy\Module\Autotest\Multiaccount;

use Tymy\Bootstrap;
use Tymy\Module\Autotest\ApiTest;
use Tymy\Module\Autotest\Entity\Assert;
use Tymy\Module\Autotest\RequestCase;
use Tymy\Module\Multiaccount\Model\TransferKey;

require getenv("ROOT_DIR") . '/app/Bootstrap.php';
Expand All @@ -17,7 +17,7 @@
*
* @skip Skip this autotest, since its ot performable from cli - we would need two tymy instances to test this
*/
class MultiaccountTest extends RequestCase
class MultiaccountTest extends ApiTest
{
public function getModule(): string
{
Expand Down
Loading

0 comments on commit db9b4c6

Please sign in to comment.