From 4b80fcbda88493b166cf3ffcf8f6760890552c4c Mon Sep 17 00:00:00 2001 From: Nikita Kiselev Date: Tue, 11 Nov 2025 00:07:59 +0300 Subject: [PATCH] test: add unit tests --- .../tests/Unit/{ => Framework}/ArrTest.php | 2 +- .../Unit/{ => Framework}/BuilderTest.php | 2 +- .../Unit/{ => Framework}/ContainerTest.php | 2 +- .../{ => Framework}/CriteriaBuilderTest.php | 2 +- .../Framework/ExecutionTimeProfilerTest.php | 86 +++++++++++++ .../{ => Framework}/GenericCollectionTest.php | 2 +- .../Unit/{ => Framework}/HelpersTest.php | 2 +- .../tests/Unit/Framework/JsonResponseTest.php | 57 +++++++++ .../Unit/{ => Framework}/MySqlGrammarTest.php | 2 +- .../Unit/Framework/PaginationHelperTest.php | 46 +++++++ .../tests/Unit/Framework/QueryResultTest.php | 64 ++++++++++ .../Unit/Framework/RawExpressionTest.php | 41 +++++++ .../Unit/{ => Framework}/RequestTest.php | 2 +- .../{ => Framework}/RuleSerializerTest.php | 2 +- .../Unit/{ => Framework}/SettingsTest.php | 2 +- .../tests/Unit/{ => Framework}/StrTest.php | 2 +- .../tests/Unit/{ => Framework}/TableTest.php | 2 +- .../Unit/{ => Framework}/TranslatorTest.php | 2 +- .../tests/Unit/Framework/UtilsTest.php | 114 ++++++++++++++++++ .../Validator/ErrorBagTest.php | 2 +- .../Validator/ValidatorTest.php | 2 +- 21 files changed, 423 insertions(+), 15 deletions(-) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/ArrTest.php (99%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/BuilderTest.php (99%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/ContainerTest.php (99%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/CriteriaBuilderTest.php (99%) create mode 100644 module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/ExecutionTimeProfilerTest.php rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/GenericCollectionTest.php (99%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/HelpersTest.php (97%) create mode 100644 module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/JsonResponseTest.php rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/MySqlGrammarTest.php (99%) create mode 100644 module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/PaginationHelperTest.php create mode 100644 module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/QueryResultTest.php create mode 100644 module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/RawExpressionTest.php rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/RequestTest.php (97%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/RuleSerializerTest.php (97%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/SettingsTest.php (98%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/StrTest.php (98%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/TableTest.php (97%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/TranslatorTest.php (96%) create mode 100644 module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/UtilsTest.php rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/Validator/ErrorBagTest.php (91%) rename module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/{ => Framework}/Validator/ValidatorTest.php (98%) diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/ArrTest.php b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/ArrTest.php similarity index 99% rename from module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/ArrTest.php rename to module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/ArrTest.php index 8ecef41..28ae537 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/ArrTest.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/ArrTest.php @@ -1,6 +1,6 @@ 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']); + } +} + diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/GenericCollectionTest.php b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/GenericCollectionTest.php similarity index 99% rename from module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/GenericCollectionTest.php rename to module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/GenericCollectionTest.php index a2fb6e5..b141593 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/GenericCollectionTest.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/GenericCollectionTest.php @@ -1,6 +1,6 @@ '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()); + } +} + diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/MySqlGrammarTest.php b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/MySqlGrammarTest.php similarity index 99% rename from module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/MySqlGrammarTest.php rename to module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/MySqlGrammarTest.php index 18cc146..0c1a456 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/MySqlGrammarTest.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/MySqlGrammarTest.php @@ -1,6 +1,6 @@ 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); + } +} + diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/QueryResultTest.php b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/QueryResultTest.php new file mode 100644 index 0000000..e242938 --- /dev/null +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/QueryResultTest.php @@ -0,0 +1,64 @@ + 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()); + } +} + diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/RawExpressionTest.php b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/RawExpressionTest.php new file mode 100644 index 0000000..44a9bba --- /dev/null +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/RawExpressionTest.php @@ -0,0 +1,41 @@ +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()); + } +} + diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/RequestTest.php b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/RequestTest.php similarity index 97% rename from module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/RequestTest.php rename to module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/RequestTest.php index 154f70d..e5f48ac 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/RequestTest.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/RequestTest.php @@ -1,6 +1,6 @@ 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')); + } +} + diff --git a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Validator/ErrorBagTest.php b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/Validator/ErrorBagTest.php similarity index 91% rename from module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Validator/ErrorBagTest.php rename to module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/Validator/ErrorBagTest.php index 34896ca..931e9f2 100755 --- a/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Validator/ErrorBagTest.php +++ b/module/oc_telegram_shop/upload/oc_telegram_shop/tests/Unit/Framework/Validator/ErrorBagTest.php @@ -1,6 +1,6 @@