Commit 92b880d6 authored by Dainis Abols's avatar Dainis Abols
Browse files

Unit tests (DataHelperTest, GeneratorTest, ReturnPrettyTest)

parent 124d7563
<?php
declare(strict_types=1);
namespace Lu\LuApi\Tests\Unit;
use Lu\LuApi\Helpers\DataHelper;
use Vendor\MyExtension\Service\AbstractSomethingService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* DatHelper tests
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @since 23.02.2022
*/
class DataHelperTest extends UnitTestCase
{
protected $resetSingletonInstances = true;
/**
* LocalConfiguration file name and relative location
*
* @var string
*/
private string $LocalConfiguration = 'public/typo3conf/LocalConfiguration.php';
/**
* Add database data
*
* @return void
*/
private function initDB()
{
$conf = @include $this->LocalConfiguration;
$GLOBALS['TYPO3_CONF_VARS']['DB'] = $conf['DB'];
}
/**
* Test date format method
*
* @test
*/
public function longLatvianDateTest(): void
{
$helper = new DataHelper();
$this->assertIsString($helper->longLatvianDate(new \DateTime('2022-02-23')), 'Date format result not a string');
$this->assertEquals('Trešdiena 2022. gada 23. februāris', $helper->longLatvianDate(new \DateTime('2022-02-23')), 'Date format result does not match');
}
/**
* Test namedays method
*
* @test
*/
public function getNamesdaysTest(): void
{
$helper = new DataHelper();
$this->initDB();
$this->assertIsString($helper->getNamesdays(new \DateTime('2000-06-24')), 'Namedays format result not a string');
$this->assertEquals('Jānis', $helper->getNamesdays(new \DateTime('2000-06-24')), 'Namedays format result does not match');
}
/**
* Test isDate method
*
* @test
*/
public function isDateTest(): void
{
$helper = new DataHelper();
$this->assertTrue($helper->isDate('2000-06-24'), 'Date falidation failed');
$this->assertFalse($helper->isDate('pirmdiena'), 'Date validation succefully failed');
}
/**
* Test JSON decode method
*
* @test
*/
public function APIDecodeTest()
{
$helper = new DataHelper();
$jsonTrue = '{"success":true,"code":200,"data":[1,2,3]}';
$jsonFalse = '{"success":false}';
$this->assertIsArray($helper->APIDecode($jsonTrue), 'JSON Data decode failed');
$this->assertNull($helper->APIDecode($jsonFalse), 'JSON Data decode failed');
}
/**
* Test url fetch method
*
* @test
*/
public function fetchUrlTest()
{
$helper = new DataHelper();
$this->assertEquals(200, ($helper->fetchUrl('https://www.lu.lv/'))->getStatusCode(), 'Url fetch failed');
$this->assertNull($helper->fetchUrl('sptth://www.lu.lv/'), 'Url fetch succefully failed');
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace Lu\LuApi\Tests\Unit;
use Lu\LuApi\Generators\QRCodeGenerator;
use Lu\LuApi\Generators\Randomizer;
use Vendor\MyExtension\Service\AbstractSomethingService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* QR COde generator tests
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @since 23.02.2022
*/
class GeneratorTest extends UnitTestCase
{
protected $resetSingletonInstances = true;
/**
* Test qrcode generator method
*
* @test
*/
public function qrCodeTest(): void
{
// Set url
$_REQUEST['r'] = '0/1/https://www.lu.lv/';
// Fetch full output
ob_start();
QRCodeGenerator::create(true);
$img = ob_get_contents();
// Needed to avoid risky warning for test
ob_end_flush();
// Store temp file
$img_location = '/tmp/'.uniqid().'.png';
file_put_contents($img_location, $img);
// Assert image size 400x400
$result = @getimagesize($img_location);
$size = function ($x, $y) use ($result) {
return $result[0] === $x && $result[1] === $y;
};
$this->assertTrue($size(400, 400), 'QRCode Response image size does not match');
}
/**
* Test random string generator method
*
* @test
*/
public function randomStringTest(): void
{
// Generate global string
$string = (new Randomizer())->getRandomString(64);
$this->assertJson($string, 'Random string [general] return is not JSON');
$string = json_decode($string)->data;
$this->assertIsString($string, 'Random string [general] is not a string');
$this->assertEquals(64, strlen($string), 'Random string [general] has invalid length');
// Generate alphabetic string
$string = (new Randomizer())->getRandomString(64, true);
$this->assertJson($string, 'Random string [alphabetic] return is not JSON');
$string = json_decode($string)->data;
$this->assertIsString($string, 'Random string [alphabetic] is not a string');
$this->assertEquals(64, strlen($string), 'Random string [alphabetic] has invalid length');
// Generate numberic string
$string = (new Randomizer())->getRandomString(64, false, true);
$this->assertJson($string, 'Random string [numberic] return is not JSON');
$string = json_decode($string)->data;
$this->assertIsString($string, 'Random string [numberic] is not a string');
$this->assertEquals(64, strlen($string), 'Random string [numberic] has invalid length');
}
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace Lu\LuApi\Tests\Unit;
use Lu\LuApi\Helpers\ReturnPretty;
use Vendor\MyExtension\Service\AbstractSomethingService;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
/**
* Return Pretty JSON tests
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @owner University of Latvia
* @since 23.02.2022
*/
class ReturnPrettyTest extends UnitTestCase
{
protected $resetSingletonInstances = true;
/**
* Test date format method
*
* @test
*/
public function jsonReturnTest(): void
{
$data = [1, 2, 3 => [4, 5]];
$this->assertJson(ReturnPretty::json($data, 200, true), 'JSON not returned');
$this->assertEquals('{
"0": 1,
"1": 2,
"3": [
4,
5
]
}', ReturnPretty::json($data, 200, true), 'JSON returned does noth match');
$this->assertJson(ReturnPretty::json($data), 'JSON not returned');
$this->assertEquals('{
"code": 200,
"success": true,
"data": {
"0": 1,
"1": 2,
"3": [
4,
5
]
}
}', ReturnPretty::json($data), 'JSON not returned');
}
}
\ No newline at end of file
Add to composer, when tests finished!
```
"autoload-dev": {
"psr-4": {
"Lu\\LuApi\\Tests\\": "Tests"
}
},
```
\ 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