Implement more unit tests

This commit is contained in:
Dennis Eichhorn 2018-12-25 16:17:19 +01:00
parent 336495db02
commit 15d2b5f7dd
37 changed files with 487 additions and 52 deletions

View File

@ -27,7 +27,7 @@ use phpOMS\DataStorage\Database\Schema\Exception\TableException;
class DatabaseExceptionFactory
{
/**
* Constructor.
* Create exception class string based on exception.
*
* @param \PDOException $e Exception
*
@ -46,7 +46,7 @@ class DatabaseExceptionFactory
}
/**
* Constructor.
* Create exception message based on exception.
*
* @param \PDOException $e Exception
*

View File

@ -62,13 +62,9 @@ final class EventManager
public function __construct(Dispatcher $dispatcher = null)
{
$this->dispatcher = $dispatcher ?? new class {
public function dispatch($func, $data)
public function dispatch($func, ...$data)
{
if (\is_array($data)) {
$func(...$data);
} else {
$func($data);
}
$func(...$data);
}
};
}

View File

@ -95,11 +95,8 @@ final class Router
foreach ($this->routes as $route => $destination) {
foreach ($destination as $d) {
if ($this->match($route, $d['verb'], $request, $verb)) {
if (!isset($d['permission'])
|| !isset($account)
|| (isset($d['permission'])
&& isset($account)
&& $account->hasPermission($d['permission']['type'], $orgId, $app, $d['permission']['module'], $d['permission']['state']))
if (!isset($d['permission'], $account)
|| $account->hasPermission($d['permission']['type'], $orgId, $app, $d['permission']['module'], $d['permission']['state'])
) {
$bound[] = ['dest' => $d['dest']];
} else {

View File

@ -129,7 +129,7 @@ abstract class C128Abstract
*/
public function __construct(string $content = '', int $width = 100, int $height = 20, int $orientation = OrientationType::HORIZONTAL)
{
$this->content = $content;
$this->setContent($content);
$this->setDimension($width, $height);
$this->setOrientation($orientation);
}
@ -345,7 +345,7 @@ abstract class C128Abstract
$location + $this->margin,
0 + $this->margin,
$cur_size + $this->margin,
$dimensions['height'] - $this->margin,
$dimensions['height'] - $this->margin - 1,
($position % 2 == 0 ? $white : $black)
);
} else {
@ -353,7 +353,7 @@ abstract class C128Abstract
$image,
0 + $this->margin,
$location + $this->margin,
$dimensions['width'] - $this->margin,
$dimensions['width'] - $this->margin - 1,
$cur_size + $this->margin,
($position % 2 == 0 ? $white : $black)
);
@ -401,11 +401,11 @@ abstract class C128Abstract
$dimensions = ['width' => 0, 'height' => 0];
if ($this->orientation === OrientationType::HORIZONTAL) {
$dimensions['width'] = $codeLength + $this->margin * 2;
$dimensions['width'] = $codeLength + $this->margin * 2 + 1;
$dimensions['height'] = $this->dimension['height'];
} else {
$dimensions['width'] = $this->dimension['width'];
$dimensions['height'] = $codeLength + $this->margin;
$dimensions['height'] = $codeLength + $this->margin * 2 + 1;
}
return $dimensions;

View File

@ -74,10 +74,6 @@ class C25 extends C128Abstract
*/
public function __construct(string $content = '', int $width = 100, int $height = 20, int $orientation = OrientationType::HORIZONTAL)
{
if (!ctype_digit($content)) {
throw new \InvalidArgumentException($content);
}
parent::__construct($content, $width, $height, $orientation);
}
@ -92,7 +88,7 @@ class C25 extends C128Abstract
*/
public function setContent(string $content) : void
{
if (!ctype_digit($content)) {
if (!\ctype_digit($content)) {
throw new \InvalidArgumentException($content);
}

View File

@ -17,7 +17,7 @@ namespace phpOMS\Utils\Barcode;
use phpOMS\Stdlib\Base\Enum;
/**
* Account type enum.
* Orientation type enum.
*
* @package phpOMS\Utils\Barcode
* @license OMS License 1.0

View File

@ -40,17 +40,17 @@ final class TestUtils
/**
* Set private object member
*
* @param object|string $obj Object to modify
* @param string $name Member name to modify
* @param mixed $value Value to set
* @param object $obj Object to modify
* @param string $name Member name to modify
* @param mixed $value Value to set
*
* @return bool The function returns true after setting the member
*
* @since 1.0.0
*/
public static function setMember($obj, string $name, $value) : bool
public static function setMember(object $obj, string $name, $value) : bool
{
$reflectionClass = new \ReflectionClass(\is_string($obj) ? $obj : \get_class($obj));
$reflectionClass = new \ReflectionClass(\get_class($obj));
if (!$reflectionClass->hasProperty($name)) {
return false;
@ -62,11 +62,7 @@ final class TestUtils
$reflectionProperty->setAccessible(true);
}
if (\is_string($obj)) {
$reflectionProperty->setValue($value);
} elseif (\is_object($obj)) {
$reflectionProperty->setValue($obj, $value);
}
$reflectionProperty->setValue($obj, $value);
if (!$accessible) {
$reflectionProperty->setAccessible(false);

View File

@ -84,4 +84,15 @@ class EventManagerTest extends \PHPUnit\Framework\TestCase
$event->trigger('group1');
self::assertEquals(1, $event->count());
}
public function testImportEvents()
{
$event = new EventManager();
self::assertFalse($event->importFromFile(__DIR__ . '/invalid.php'));
self::assertTrue($event->importFromFile(__DIR__ . '/events.php'));
self::assertEquals(2, $event->count());
self::assertTrue($event->trigger('SomeName1', '', [1, 2, 3]));
self::assertTrue($event->trigger('SomeName2', '', 4));
}
}

13
tests/Event/events.php Normal file
View File

@ -0,0 +1,13 @@
<?php return [
'SomeName1' => [
'callback' => [
0 => function($v1, $v2, $v3) {},
1 => function($v1, $v2, $v3) {},
],
],
'SomeName2' => [
'callback' => [
0 => function($v4) {},
],
],
];

View File

@ -0,0 +1,57 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
namespace phpOMS\tests\Utils\Barcode;
use phpOMS\Utils\Barcode\C128Abstract;
use phpOMS\Utils\Barcode\OrientationType;
class C128AbstractTest extends \PHPUnit\Framework\TestCase
{
protected $obj = null;
protected function setUp()
{
$this->obj = new class extends C128Abstract {};
}
public function testSetGet()
{
$this->obj->setContent('abc');
self::assertEquals('abc', $this->obj->getContent());
}
/**
* @expectedException \OutOfBoundsException
*/
public function testInvalidDimensionWidth()
{
$this->obj->setDimension(-2, 1);
}
/**
* @expectedException \OutOfBoundsException
*/
public function testInvalidDimensionHeight()
{
$this->obj->setDimension(1, -2);
}
/**
* @expectedException \phpOMS\Stdlib\Base\Exception\InvalidEnumValue
*/
public function testInvalidOrientation()
{
$this->obj->setOrientation(99);
}
}

View File

@ -14,6 +14,7 @@
namespace phpOMS\tests\Utils\Barcode;
use phpOMS\Utils\Barcode\C128a;
use phpOMS\Utils\Barcode\OrientationType;
class C128aTest extends \PHPUnit\Framework\TestCase
{
@ -26,7 +27,7 @@ class C128aTest extends \PHPUnit\Framework\TestCase
}
}
public function testImage()
public function testImagePng()
{
$path = __DIR__ . '/c128a.png';
if (\file_exists($path)) {
@ -39,6 +40,33 @@ class C128aTest extends \PHPUnit\Framework\TestCase
self::assertTrue(\file_exists($path));
}
public function testImageJpg()
{
$path = __DIR__ . '/c128a.jpg';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C128a('ABCDEFG0123()+-', 200, 50);
$img->saveToJpgFile($path);
self::assertTrue(\file_exists($path));
}
public function testOrientationAndMargin()
{
$path = __DIR__ . '/c128a_vertical.png';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C128a('ABCDEFG0123()+-', 50, 200, OrientationType::VERTICAL);
$img->setMargin(2);
$img->saveToPngFile($path);
self::assertTrue(\file_exists($path));
}
public function testValidString()
{
self::assertTrue(C128a::isValidString('ABCDEFG0123+-'));

View File

@ -14,6 +14,7 @@
namespace phpOMS\tests\Utils\Barcode;
use phpOMS\Utils\Barcode\C128b;
use phpOMS\Utils\Barcode\OrientationType;
class C128bTest extends \PHPUnit\Framework\TestCase
{
@ -26,7 +27,7 @@ class C128bTest extends \PHPUnit\Framework\TestCase
}
}
public function testImage()
public function testImagePng()
{
$path = __DIR__ . '/c128b.png';
if (\file_exists($path)) {
@ -39,6 +40,33 @@ class C128bTest extends \PHPUnit\Framework\TestCase
self::assertTrue(\file_exists($path));
}
public function testImageJpg()
{
$path = __DIR__ . '/c128b.jpg';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C128b('ABcdeFG0123+-!@?', 200, 50);
$img->saveToJpgFile($path);
self::assertTrue(\file_exists($path));
}
public function testOrientationAndMargin()
{
$path = __DIR__ . '/c128b_vertical.png';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C128b('ABcdeFG0123+-!@?', 50, 200, OrientationType::VERTICAL);
$img->setMargin(2);
$img->saveToPngFile($path);
self::assertTrue(\file_exists($path));
}
public function testValidString()
{
self::assertTrue(C128b::isValidString('ABCDE~FG0123+-'));

View File

@ -14,6 +14,7 @@
namespace phpOMS\tests\Utils\Barcode;
use phpOMS\Utils\Barcode\C128c;
use phpOMS\Utils\Barcode\OrientationType;
class C128cTest extends \PHPUnit\Framework\TestCase
{
@ -26,7 +27,7 @@ class C128cTest extends \PHPUnit\Framework\TestCase
}
}
public function testImage()
public function testImagePng()
{
$path = __DIR__ . '/c128c.png';
if (\file_exists($path)) {
@ -38,4 +39,31 @@ class C128cTest extends \PHPUnit\Framework\TestCase
self::assertTrue(\file_exists($path));
}
public function testImageJpg()
{
$path = __DIR__ . '/c128c.jpg';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C128c('412163', 200, 50);
$img->saveToJpgFile($path);
self::assertTrue(\file_exists($path));
}
public function testOrientationAndMargin()
{
$path = __DIR__ . '/c128c_vertical.png';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C128c('412163', 50, 200, OrientationType::VERTICAL);
$img->setMargin(2);
$img->saveToPngFile($path);
self::assertTrue(\file_exists($path));
}
}

View File

@ -14,6 +14,7 @@
namespace phpOMS\tests\Utils\Barcode;
use phpOMS\Utils\Barcode\C25;
use phpOMS\Utils\Barcode\OrientationType;
class C25Test extends \PHPUnit\Framework\TestCase
{
@ -26,7 +27,7 @@ class C25Test extends \PHPUnit\Framework\TestCase
}
}
public function testImage()
public function testImagePng()
{
$path = __DIR__ . '/c25.png';
if (\file_exists($path)) {
@ -39,9 +40,44 @@ class C25Test extends \PHPUnit\Framework\TestCase
self::assertTrue(\file_exists($path));
}
public function testImageJpg()
{
$path = __DIR__ . '/c25.jpg';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C25('1234567890', 150, 50);
$img->saveToJpgFile($path);
self::assertTrue(\file_exists($path));
}
public function testOrientationAndMargin()
{
$path = __DIR__ . '/c25_vertical.png';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C25('1234567890', 50, 200, OrientationType::VERTICAL);
$img->setMargin(2);
$img->saveToPngFile($path);
self::assertTrue(\file_exists($path));
}
public function testValidString()
{
self::assertTrue(C25::isValidString('1234567890'));
self::assertFalse(C25::isValidString('1234567A890'));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidOrientation()
{
$img = new C25('45f!a?12');
}
}

View File

@ -14,6 +14,7 @@
namespace phpOMS\tests\Utils\Barcode;
use phpOMS\Utils\Barcode\C39;
use phpOMS\Utils\Barcode\OrientationType;
class C39Test extends \PHPUnit\Framework\TestCase
{
@ -26,7 +27,7 @@ class C39Test extends \PHPUnit\Framework\TestCase
}
}
public function testImage()
public function testImagePng()
{
$path = __DIR__ . '/c39.png';
if (\file_exists($path)) {
@ -39,6 +40,33 @@ class C39Test extends \PHPUnit\Framework\TestCase
self::assertTrue(\file_exists($path));
}
public function testImageJpg()
{
$path = __DIR__ . '/c39.jpg';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C39('ABCDEFG0123+-', 150, 50);
$img->saveToJpgFile($path);
self::assertTrue(\file_exists($path));
}
public function testOrientationAndMargin()
{
$path = __DIR__ . '/c39_vertical.png';
if (\file_exists($path)) {
\unlink($path);
}
$img = new C39('ABCDEFG0123+-', 50, 150, OrientationType::VERTICAL);
$img->setMargin(2);
$img->saveToPngFile($path);
self::assertTrue(\file_exists($path));
}
public function testValidString()
{
self::assertTrue(C39::isValidString('ABCDEFG0123+-'));

View File

@ -14,6 +14,7 @@
namespace phpOMS\tests\Utils\Barcode;
use phpOMS\Utils\Barcode\Codebar;
use phpOMS\Utils\Barcode\OrientationType;
class CodebarTest extends \PHPUnit\Framework\TestCase
{
@ -26,7 +27,7 @@ class CodebarTest extends \PHPUnit\Framework\TestCase
}
}
public function testImage()
public function testImagePng()
{
$path = __DIR__ . '/codebar.png';
if (\file_exists($path)) {
@ -39,6 +40,33 @@ class CodebarTest extends \PHPUnit\Framework\TestCase
self::assertTrue(\file_exists($path));
}
public function testImageJpg()
{
$path = __DIR__ . '/codebar.jpg';
if (\file_exists($path)) {
\unlink($path);
}
$img = new Codebar('412163', 200, 50);
$img->saveToJpgFile($path);
self::assertTrue(\file_exists($path));
}
public function testOrientationAndMargin()
{
$path = __DIR__ . '/ccodebar_vertical.png';
if (\file_exists($path)) {
\unlink($path);
}
$img = new Codebar('412163', 50, 200, OrientationType::VERTICAL);
$img->setMargin(2);
$img->saveToPngFile($path);
self::assertTrue(\file_exists($path));
}
public function testValidString()
{
self::assertTrue(Codebar::isValidString('412163'));

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 156 B

After

Width:  |  Height:  |  Size: 156 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 157 B

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 127 B

After

Width:  |  Height:  |  Size: 129 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 B

BIN
tests/Utils/Barcode/c25.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 135 B

After

Width:  |  Height:  |  Size: 134 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 B

BIN
tests/Utils/Barcode/c39.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 153 B

After

Width:  |  Height:  |  Size: 153 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 131 B

After

Width:  |  Height:  |  Size: 131 B

View File

@ -159,4 +159,180 @@ class MeasurementTest extends \PHPUnit\Framework\TestCase
}
}
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidTemperatureFrom()
{
Measurement::convertTemperature(1.1, 'invalid', TemperatureType::CELSIUS);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidTemperatureTo()
{
Measurement::convertTemperature(1.1, TemperatureType::CELSIUS, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidWeightFrom()
{
Measurement::convertWeight(1.1, 'invalid', WeightType::KILOGRAM);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidWeightTo()
{
Measurement::convertWeight(1.1, WeightType::KILOGRAM, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidLengthFrom()
{
Measurement::convertLength(1.1, 'invalid', LengthType::METERS);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidLengthTo()
{
Measurement::convertLength(1.1, LengthType::METERS, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidAreaFrom()
{
Measurement::convertArea(1.1, 'invalid', AreaType::SQUARE_METERS);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidAreaTo()
{
Measurement::convertArea(1.1, AreaType::SQUARE_METERS, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidVolumeFrom()
{
Measurement::convertVolume(1.1, 'invalid', VolumeType::LITER);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidVolumeTo()
{
Measurement::convertVolume(1.1, VolumeType::LITER, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidSpeedFrom()
{
Measurement::convertSpeed(1.1, 'invalid', SpeedType::KILOMETERS_PER_HOUR);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidSpeedTo()
{
Measurement::convertSpeed(1.1, SpeedType::KILOMETERS_PER_HOUR, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidTimeFrom()
{
Measurement::convertTime(1.1, 'invalid', TimeType::HOURS);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidTimeTo()
{
Measurement::convertTime(1.1, TimeType::HOURS, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidAngleFrom()
{
Measurement::convertAngle(1.1, 'invalid', AngleType::RADIAN);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidAngleTo()
{
Measurement::convertAngle(1.1, AngleType::RADIAN, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidPressureFrom()
{
Measurement::convertPressure(1.1, 'invalid', PressureType::BAR);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidPressureTo()
{
Measurement::convertPressure(1.1, PressureType::BAR, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidEnergyPowerFrom()
{
Measurement::convertEnergy(1.1, 'invalid', EnergyPowerType::JOULS);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidEnergyPowerTo()
{
Measurement::convertEnergy(1.1, EnergyPowerType::JOULS, 'invalid');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidFileSizeFrom()
{
Measurement::convertFileSize(1.1, 'invalid', FileSizeType::KILOBYTE);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidFileSizeTo()
{
Measurement::convertFileSize(1.1, FileSizeType::KILOBYTE, 'invalid');
}
}

View File

@ -0,0 +1,22 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
namespace phpOMS\tests\Utils;
class TestUtilsClass
{
private $a = 1;
protected $b = 2;
public $c = 3;
public $d = '4';
}

View File

@ -21,35 +21,30 @@ class TestUtilsTest extends \PHPUnit\Framework\TestCase
{
public function testGet()
{
$class = new class {
private $a = 1;
protected $b = 2;
public $c = 3;
};
$class = new TestUtilsClass();
self::assertEquals(1, TestUtils::getMember($class, 'a'));
self::assertEquals(2, TestUtils::getMember($class, 'b'));
self::assertEquals(3, TestUtils::getMember($class, 'c'));
self::assertEquals('4', TestUtils::getMember($class, 'd'));
self::assertNull(TestUtils::getMember($class, 'd'));
self::assertNull(TestUtils::getMember($class, 'e'));
}
public function testSet()
{
$class = new class {
private $a = 1;
protected $b = 2;
public $c = 3;
};
$class = new TestUtilsClass();
self::assertTrue(TestUtils::setMember($class, 'a', 4));
self::assertTrue(TestUtils::setMember($class, 'b', 5));
self::assertTrue(TestUtils::setMember($class, 'c', 6));
self::assertTrue(TestUtils::setMember($class, 'd', '7'));
self::assertEquals(4, TestUtils::getMember($class, 'a'));
self::assertEquals(5, TestUtils::getMember($class, 'b'));
self::assertEquals(6, TestUtils::getMember($class, 'c'));
self::assertEquals('7', TestUtils::getMember($class, 'd'));
self::assertFalse(TestUtils::setMember($class, 'd', 7));
self::assertFalse(TestUtils::setMember($class, 'e', 8));
}
}