test: add unit tests

This commit is contained in:
2025-11-11 00:07:59 +03:00
parent cd818d3356
commit 4b80fcbda8
21 changed files with 423 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use InvalidArgumentException;
use Openguru\OpenCartFramework\Support\Arr;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\QueryBuilder\Builder;
use Openguru\OpenCartFramework\QueryBuilder\Connections\MySqlConnection;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\Container\Container;
use Openguru\OpenCartFramework\Exceptions\ContainerDependencyResolutionException;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use App\Filters\ProductAttribute;
use App\Filters\ProductCategories;

View File

@@ -0,0 +1,86 @@
<?php
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\Support\ExecutionTimeProfiler;
use PHPUnit\Framework\TestCase;
class ExecutionTimeProfilerTest extends TestCase
{
public function testStartInitializesProfiler(): void
{
$profiler = new ExecutionTimeProfiler();
$profiler->start();
$result = $profiler->stop();
$this->assertArrayHasKey('total_execution_time_ms', $result);
$this->assertArrayHasKey('checkpoints', $result);
$this->assertIsFloat($result['total_execution_time_ms']);
$this->assertGreaterThanOrEqual(0, $result['total_execution_time_ms']);
}
public function testAddCheckpointRecordsCheckpoint(): void
{
$profiler = new ExecutionTimeProfiler();
$profiler->start();
usleep(10000); // 10ms
$profiler->addCheckpoint('test_checkpoint');
$result = $profiler->stop();
$this->assertCount(2, $result['checkpoints']); // start + test_checkpoint + End
$this->assertEquals('test_checkpoint', $result['checkpoints'][0]['label']);
$this->assertArrayHasKey('elapsed_since_previous_ms', $result['checkpoints'][0]);
}
public function testStopReturnsCorrectStructure(): void
{
$profiler = new ExecutionTimeProfiler();
$profiler->start();
$profiler->addCheckpoint('checkpoint1');
$profiler->addCheckpoint('checkpoint2');
$result = $profiler->stop();
$this->assertArrayHasKey('total_execution_time_ms', $result);
$this->assertArrayHasKey('checkpoints', $result);
$this->assertIsArray($result['checkpoints']);
$this->assertCount(3, $result['checkpoints']); // checkpoint1, checkpoint2, End
}
public function testMultipleCheckpoints(): void
{
$profiler = new ExecutionTimeProfiler();
$profiler->start();
$profiler->addCheckpoint('first');
usleep(5000);
$profiler->addCheckpoint('second');
usleep(5000);
$profiler->addCheckpoint('third');
$result = $profiler->stop();
$this->assertCount(4, $result['checkpoints']);
$this->assertEquals('first', $result['checkpoints'][0]['label']);
$this->assertEquals('second', $result['checkpoints'][1]['label']);
$this->assertEquals('third', $result['checkpoints'][2]['label']);
$this->assertEquals('End', $result['checkpoints'][3]['label']);
}
public function testElapsedTimeIsCalculated(): void
{
$profiler = new ExecutionTimeProfiler();
$profiler->start();
usleep(10000); // 10ms
$profiler->addCheckpoint('test');
$result = $profiler->stop();
$this->assertGreaterThan(0, $result['checkpoints'][0]['elapsed_since_previous_ms']);
$this->assertIsFloat($result['checkpoints'][0]['elapsed_since_previous_ms']);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use ArrayIterator;
use InvalidArgumentException;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use InvalidArgumentException;
use Tests\TestCase;

View File

@@ -0,0 +1,57 @@
<?php
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\Http\JsonResponse;
use PHPUnit\Framework\TestCase;
class JsonResponseTest extends TestCase
{
public function testConstructorSetsDataAndCode(): void
{
$data = ['key' => 'value'];
$code = 201;
$response = new JsonResponse($data, $code);
$this->assertEquals($data, $response->getData());
$this->assertEquals($code, $response->getCode());
}
public function testDefaultCodeIs200(): void
{
$data = ['key' => 'value'];
$response = new JsonResponse($data);
$this->assertEquals(JsonResponse::HTTP_OK, $response->getCode());
}
public function testGetDataReturnsCorrectData(): void
{
$data = ['message' => 'success', 'items' => [1, 2, 3]];
$response = new JsonResponse($data);
$this->assertEquals($data, $response->getData());
}
public function testGetCodeReturnsCorrectCode(): void
{
$response = new JsonResponse([], 404);
$this->assertEquals(404, $response->getCode());
}
public function testResponseInheritsFromResponse(): void
{
$response = new JsonResponse([]);
$this->assertInstanceOf(\Openguru\OpenCartFramework\Http\Response::class, $response);
}
public function testCanUseHttpConstants(): void
{
$response = new JsonResponse([], JsonResponse::HTTP_CREATED);
$this->assertEquals(201, $response->getCode());
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use Mockery as m;
use Openguru\OpenCartFramework\QueryBuilder\Builder;

View File

@@ -0,0 +1,46 @@
<?php
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\Support\PaginationHelper;
use PHPUnit\Framework\TestCase;
class PaginationHelperTest extends TestCase
{
public function testCalculateLastPageWithExactDivision(): void
{
$result = PaginationHelper::calculateLastPage(100, 10);
$this->assertEquals(10, $result);
}
public function testCalculateLastPageWithRemainder(): void
{
$result = PaginationHelper::calculateLastPage(101, 10);
$this->assertEquals(11, $result);
}
public function testCalculateLastPageWithSingleRecord(): void
{
$result = PaginationHelper::calculateLastPage(1, 10);
$this->assertEquals(1, $result);
}
public function testCalculateLastPageWithZeroRecords(): void
{
$result = PaginationHelper::calculateLastPage(0, 10);
$this->assertEquals(0, $result);
}
public function testCalculateLastPageWithLargeNumbers(): void
{
$result = PaginationHelper::calculateLastPage(1000, 25);
$this->assertEquals(40, $result);
}
public function testCalculateLastPageWithOnePerPage(): void
{
$result = PaginationHelper::calculateLastPage(5, 1);
$this->assertEquals(5, $result);
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\QueryBuilder\QueryResult;
use PHPUnit\Framework\TestCase;
class QueryResultTest extends TestCase
{
public function testConstructorSetsProperties(): void
{
$row = ['id' => 1, 'name' => 'Test'];
$rows = [['id' => 1], ['id' => 2]];
$numRows = 2;
$result = new QueryResult($row, $rows, $numRows);
$this->assertEquals($row, $result->getRow());
$this->assertEquals($rows, $result->getRows());
$this->assertEquals($numRows, $result->getNumRows());
}
public function testGetRowReturnsSingleRow(): void
{
$row = ['id' => 1, 'name' => 'Test'];
$result = new QueryResult($row, [], 0);
$this->assertEquals($row, $result->getRow());
}
public function testGetRowsReturnsAllRows(): void
{
$rows = [
['id' => 1, 'name' => 'Test1'],
['id' => 2, 'name' => 'Test2'],
];
$result = new QueryResult(null, $rows, 2);
$this->assertEquals($rows, $result->getRows());
}
public function testGetNumRowsReturnsCount(): void
{
$result = new QueryResult(null, [], 5);
$this->assertEquals(5, $result->getNumRows());
}
public function testCanHandleNullRow(): void
{
$result = new QueryResult(null, [], 0);
$this->assertNull($result->getRow());
}
public function testCanHandleEmptyRows(): void
{
$result = new QueryResult(null, [], 0);
$this->assertEquals([], $result->getRows());
$this->assertEquals(0, $result->getNumRows());
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\QueryBuilder\RawExpression;
use PHPUnit\Framework\TestCase;
class RawExpressionTest extends TestCase
{
public function testConstructorSetsValue(): void
{
$value = 'NOW()';
$expression = new RawExpression($value);
$this->assertEquals($value, $expression->getValue());
}
public function testGetValueReturnsCorrectValue(): void
{
$value = 'COUNT(*)';
$expression = new RawExpression($value);
$this->assertEquals($value, $expression->getValue());
}
public function testCanHandleComplexExpressions(): void
{
$value = 'DATE_FORMAT(created_at, "%Y-%m-%d")';
$expression = new RawExpression($value);
$this->assertEquals($value, $expression->getValue());
}
public function testCanHandleEmptyString(): void
{
$expression = new RawExpression('');
$this->assertEquals('', $expression->getValue());
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\Http\Request;
use PHPUnit\Framework\TestCase;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use App\Filters\ProductModel;
use Openguru\OpenCartFramework\CriteriaBuilder\Criterion;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\Config\Settings;
use Tests\TestCase;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\Support\Str;
use PHPUnit\Framework\TestCase;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use InvalidArgumentException;
use Openguru\OpenCartFramework\QueryBuilder\Builder;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit;
namespace Tests\Unit\Framework;
use Openguru\OpenCartFramework\Translator\Translator;
use Tests\TestCase;

View File

@@ -0,0 +1,114 @@
<?php
namespace Tests\Unit\Framework;
use InvalidArgumentException;
use Openguru\OpenCartFramework\Support\Utils;
use PHPUnit\Framework\TestCase;
class UtilsTest extends TestCase
{
public function testUcsplitSplitsStringByUppercase(): void
{
$result = Utils::ucsplit('HelloWorld');
$this->assertEquals(['Hello', 'World'], $result);
}
public function testUcsplitWithMultipleWords(): void
{
$result = Utils::ucsplit('HelloWorldTest');
$this->assertEquals(['Hello', 'World', 'Test'], $result);
}
public function testUcsplitWithSingleWord(): void
{
$result = Utils::ucsplit('Hello');
$this->assertEquals(['Hello'], $result);
}
public function testUcsplitWithEmptyString(): void
{
$result = Utils::ucsplit('');
$this->assertEquals([], $result);
}
public function testSafeJsonDecodeWithValidJson(): void
{
$json = '{"key": "value", "number": 123}';
$result = Utils::safeJsonDecode($json);
$this->assertIsArray($result);
$this->assertEquals('value', $result['key']);
$this->assertEquals(123, $result['number']);
}
public function testSafeJsonDecodeWithArray(): void
{
$json = '[1, 2, 3]';
$result = Utils::safeJsonDecode($json);
$this->assertIsArray($result);
$this->assertEquals([1, 2, 3], $result);
}
public function testSafeJsonDecodeThrowsExceptionOnInvalidJson(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('JSON error:');
Utils::safeJsonDecode('invalid json {');
}
public function testArrayFlattenWithNestedArrays(): void
{
$array = [1, [2, 3], [4, [5, 6]]];
$result = Utils::arrayFlatten($array);
$this->assertEquals([1, 2, 3, 4, 5, 6], $result);
}
public function testArrayFlattenWithDepthLimit(): void
{
$array = [1, [2, [3, 4]]];
$result = Utils::arrayFlatten($array, 1);
$this->assertEquals([1, 2, [3, 4]], $result);
}
public function testArrayFlattenWithFlatArray(): void
{
$array = [1, 2, 3];
$result = Utils::arrayFlatten($array);
$this->assertEquals([1, 2, 3], $result);
}
public function testArrayFlattenWithEmptyArray(): void
{
$result = Utils::arrayFlatten([]);
$this->assertEquals([], $result);
}
public function testStrContainsWithExistingNeedle(): void
{
$this->assertTrue(Utils::strContains('hello world', 'world'));
}
public function testStrContainsWithNonExistingNeedle(): void
{
$this->assertFalse(Utils::strContains('hello world', 'foo'));
}
public function testStrContainsWithEmptyNeedle(): void
{
$this->assertTrue(Utils::strContains('hello world', ''));
}
public function testStrContainsCaseSensitive(): void
{
$this->assertTrue(Utils::strContains('Hello World', 'Hello'));
$this->assertFalse(Utils::strContains('Hello World', 'hello'));
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit\Validator;
namespace Tests\Unit\Framework\Validator;
use Openguru\OpenCartFramework\Validator\ErrorBag;
use Tests\TestCase;

View File

@@ -1,6 +1,6 @@
<?php
namespace Tests\Unit\Validator;
namespace Tests\Unit\Framework\Validator;
use Openguru\OpenCartFramework\Validator\ValidationRuleNotFoundException;
use Openguru\OpenCartFramework\Validator\ValidationRules\Required;