Commit 672b1b4d authored by Dainis Abols's avatar Dainis Abols
Browse files

LessonsTest (dabas maja + zinatnu maja)

parent fa3d1f2f
<?php
declare(strict_types=1);
namespace Lu\LuApi\Functional;
use Lu\LuApi\Helpers\DataHelper;
use Vendor\MyExtension\Service\AbstractSomethingService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Test for lessons routes
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @since 22.02.2022
*/
class LessonsTest extends UnitTestCase
{
protected $resetSingletonInstances = true;
/**
* Public API URL
*
* @var string
*/
private string $url = 'https://www.lu.lv/api/?r=';
/**
* Lesson item structure
*
* @param array $c
*
* @return bool
*/
private function lessonStructure(array $c): bool
{
return isset($c['id']) && isset($c['type']) && isset($c['name']) && isset($c['time']) && isset($c['room_id']) && isset($c['room_url']) && isset($c['room_number']) && isset($c['course_id']) && isset($c['lecturer']);
}
/**
* Lessons (JSON) test
*
* @test
*/
public function lessonsJSONTest()
{
// Read url
$url = $this->url.'dabas-maja/search';
$result = (new DataHelper())->fetchUrl($url);
// Assert url read
$code = !empty($result) ? $result->getStatusCode() : 500;
$this->assertEquals(200, $code, 'Lessons Url read failed ['.$url.']');
// Assert json return
$content = @$result->getBody()->getContents();
$this->assertJson($content, 'Lessons Url response is not JSON');
// Check top level structure
$content = json_decode($content, true);
$this->assertTrue(count($content) === 3, 'Lessons structure count does not match');
$structure = function ($c) {
return isset($c['view']) && isset($c['date']) && isset($c['items']) && is_array($c['items']);
};
$this->assertTrue($structure($content), 'Lessons structure does not match');
// Check items object
$this->assertNotEmpty($content['items'], 'Lessons has no items received');
// Check data object structure
$this->assertTrue(count($content['items'][0]) === 9, 'Lessons items count does not match');
$this->assertTrue($this->lessonStructure($content['items'][0]), 'Lessons items structure does not match');
}
/**
* Single room lessons (JSON) test
*
* @depends lessonsJSONTest
* @test
*/
public function lessonsSingleRoomJSONTest()
{
// Get room url
$url = $this->url.'zinatnu-maja/search';
$result = (new DataHelper())->fetchUrl($url);
$content = @$result->getBody()->getContents();
$content = json_decode($content, true);
$room_url = $content['items'][0]['room_url'];
// Read url
$result = (new DataHelper())->fetchUrl($room_url);
// Assert url read
$code = !empty($result) ? $result->getStatusCode() : 500;
$this->assertEquals(200, $code, 'Room Url read failed ['.$room_url.']');
// Assert json return
$content = @$result->getBody()->getContents();
$this->assertJson($content, 'Room Url response is not JSON');
// Check top level structure
$content = json_decode($content, true);
$this->assertTrue(count($content) === 6, 'Room structure count does not match');
$structure = function ($c) {
return isset($c['view']) && isset($c['date']) && isset($c['auditorium']) && isset($c['namesday']) && isset($c['qrurl']) && isset($c['items']) && is_array($c['items']);
};
$this->assertTrue($structure($content), 'Room structure does not match');
// Check items object
$this->assertNotEmpty($content['items'], 'Room has no items received');
// Check data object structure
$this->assertTrue(count($content['items'][0]) === 9, 'Room lessons items count does not match');
$this->assertTrue($this->lessonStructure($content['items'][0]), 'Room lessons items structure does not match');
}
/**
* Lessons (HTML) test
*
* @test
*/
public function lessonsHTMLTest()
{
// Read url
$url = $this->url.'dabas-maja/search&visual=1';
$result = (new DataHelper())->fetchUrl($url);
// Assert url read
$code = !empty($result) ? $result->getStatusCode() : 500;
$this->assertEquals(200, $code, 'Lessons HTML Url read failed ['.$url.']');
// Assert html return
$content = @$result->getBody()->getContents();
$this->assertTrue(strpos($content, 'body class="kopejais-saraksts"') !== false, 'Lessons HTML content invalid');
}
/**
* Single room lessons (JSON) test
*
* @depends lessonsHTMLTest
* @test
*/
public function lessonsSingleRoomHTMLTest()
{
// Get room url
$url = $this->url.'zinatnu-maja/search';
$result = (new DataHelper())->fetchUrl($url);
$content = @$result->getBody()->getContents();
$content = json_decode($content, true);
$room_url = $content['items'][0]['room_url'];
// Read url
$result = (new DataHelper())->fetchUrl($room_url.'&visual=1');
// Assert url read
$code = !empty($result) ? $result->getStatusCode() : 500;
$this->assertEquals(200, $code, 'Lessons HTML Url read failed ['.$url.']');
// Assert html return
$content = @$result->getBody()->getContents();
$this->assertTrue(strpos($content, 'body class="auditorijas-saraksts"') !== false, 'Lessons HTML content invalid');
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment