From 14d5c3befd73bf93edf7da537f980bcf66cb44c6 Mon Sep 17 00:00:00 2001 From: Asad Ali Date: Tue, 17 Dec 2024 09:22:24 +0500 Subject: [PATCH] fix file wrapper tests and refactored code Signed-off-by: Asad Ali --- src/Utils/CoreHelper.php | 6 +-- tests/UtilsTest.php | 98 ++++++++++++++++++++-------------------- 2 files changed, 50 insertions(+), 54 deletions(-) diff --git a/src/Utils/CoreHelper.php b/src/Utils/CoreHelper.php index 156ff38..4f3b408 100644 --- a/src/Utils/CoreHelper.php +++ b/src/Utils/CoreHelper.php @@ -255,13 +255,11 @@ public static function stringify( string $processedProperties = '' ): string { $formattedProperties = array_map([self::class, 'stringifyProperty'], array_keys($properties), $properties); - if (!empty($processedProperties)) { - $processedProperties = substr($processedProperties, strpos($processedProperties, '[') + 1, -1); - $formattedProperties[] = $processedProperties; + $formattedProperties[] = substr($processedProperties, strpos($processedProperties, '[') + 1, -1); } - $formattedPropertiesString = implode(', ', array_filter($formattedProperties)); + $formattedPropertiesString = implode(', ', array_filter($formattedProperties)); return ltrim("$prefix [$formattedPropertiesString]"); } diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 8047d50..ad459d2 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -2,11 +2,11 @@ namespace Core\Tests; -use Core\Tests\Mocking\MockHelper; use Core\Tests\Mocking\Other\Customer; use Core\Tests\Mocking\Other\MockClass; use Core\Tests\Mocking\Other\Order; use Core\Tests\Mocking\Other\Person; +use Core\Tests\Mocking\Types\MockFileWrapper; use Core\Utils\CoreHelper; use Core\Utils\DateHelper; use Core\Utils\XmlDeserializer; @@ -187,7 +187,7 @@ public function testCoreHelperConvertToNullableString() $this->assertEquals("false", CoreHelper::convertToNullableString("false")); } - public function testToString() + public function testToStringWithInheritanceAndNesting() { $customer = new Customer(); $customer->name = 'John Doe'; @@ -210,19 +210,31 @@ public function testToString() $order->total = 250.75; $order->delivered = true; - $expected = 'Placed Order [orderId: 123, sender: Customer [email: john.doe@example.com, ' . + $expected = 'Order [orderId: 123, sender: Customer [email: john.doe@example.com, ' . 'name: John Doe, additionalProperties: [age: 21]], similarOrders: [Order [orderId: 345], ' . 'Order [orderId: 567, sender: Customer [email: john.doe@example.com, name: John Doe, ' . 'additionalProperties: [age: 21]]]], total: 250.75, delivered: true]'; - $actual = "Placed $order"; - $this->assertEquals($expected, $actual); + $this->assertEquals($expected, $order); } - public function testToStringWithNonArrayAndNonModelProperties() + public function testToStringWithFileType() { - $fileWrapper = MockHelper::getFileWrapper(); + $fileWrapper = MockFileWrapper::createFromPath('some\path\test.txt', 'text/plain', 'My Text'); + $person = new Person(); + $person->additionalProperties = [ + 'file' => $fileWrapper + ]; + + $expected = 'Person [additionalProperties: [file: MockFileWrapper [realFilePath: some\path\test.txt,' . + ' mimeType: text/plain, filename: My Text]]]'; + + $this->assertEquals($expected, $person); + } + + public function testToStringWithStdClass() + { $object = new stdClass(); $object->name = "John"; $object->age = 30; @@ -230,74 +242,60 @@ public function testToStringWithNonArrayAndNonModelProperties() $person = new Person(); $person->additionalProperties = [ 'stdClass' => $object, - 'file' => $fileWrapper + 'stdClassArray' => [$object, $object], + 'stdClassMap' => ['keyA' => $object, 'keyB' => $object] ]; - $expected = 'My Person [additionalProperties: [stdClass: [name: John, age: 30], file: MockFileWrapper' . - ' [realFilePath: C:\Users\Asad Apimatic\vs-workspace\core-lib-php\tests\Mocking\Other\testFile.txt,' . - ' mimeType: text/plain, filename: My Text]]]'; - $actual = "My $person"; + $expected = 'Person [additionalProperties: [stdClass: [name: John, age: 30], stdClassArray: ' . + '[[name: John, age: 30], [name: John, age: 30]], stdClassMap: [keyA: [name: John, age: 30], ' . + 'keyB: [name: John, age: 30]]]]'; - $this->assertEquals($expected, $actual); + $this->assertEquals($expected, $person); } public function testCoreHelperStringify() { - $expectedStringNotation = 'Model [prop1: true, prop2: 90, prop3: my string 1, additionalProperties: ' . - '[additional1: [A, B, false, true], additional2: other string, additional3: false], ' . - 'parentProp1: 1.1, parentProp2: some string]'; + $expectedStringNotation = 'Model [prop1: true, prop2: 90, prop3: my string 3, prop4: [23, 24.4]]'; $this->assertEquals($expectedStringNotation, CoreHelper::stringify( 'Model', [ 'prop1' => true, 'prop2' => 90, - 'prop3' => 'my string 1', - 'additionalProperties' => - [ - 'additional1' => [ 'A', 'B', false, true ], - 'additional2' => 'other string', - 'additional3' => false, - ] - ], - CoreHelper::stringify( - 'Parent', - [ - 'parentProp1' => 1.1, - 'parentProp2' => 'some string' - ] - ) + 'prop3' => 'my string 3', + 'prop4' => [23, 24.4] + ] )); } - public function testCoreHelperStringifyWithoutAdditionalProperties() + public function testCoreHelperStringifyWithProcessedProperties() { - $expectedStringNotation = 'Model [prop1: true, prop2: 90.234, prop3: my string 2, parentProp1: 1.0, ' . - 'parentProp2: some string]'; + $expectedStringNotation = 'Model [prop1: true, prop2: 90, prop3: my string 1, ' . + 'parentProp1: 1.1, parentProp2: some string, additionalProperties: ' . + '[additional1: [A, B, false, true], additional2: other string, additional3: false]]'; - $this->assertEquals($expectedStringNotation, CoreHelper::stringify( - 'Model', + $processedProperties = CoreHelper::stringify( + 'Parent', [ - 'prop1' => true, - 'prop2' => 90.234, - 'prop3' => 'my string 2' - ], - 'Parent [parentProp1: 1.0, parentProp2: some string]' - )); - } - - public function testCoreHelperStringifyWithoutPostfix() - { - $expectedStringNotation = 'Model [prop1: true, prop2: 90, prop3: my string 3, prop4: [23, 24.4]]'; + 'parentProp1' => 1.1, + 'parentProp2' => 'some string', + 'additionalProperties' => + [ + 'additional1' => [ 'A', 'B', false, true ], + 'additional2' => 'other string', + 'additional3' => false, + ] + ] + ); $this->assertEquals($expectedStringNotation, CoreHelper::stringify( 'Model', [ 'prop1' => true, 'prop2' => 90, - 'prop3' => 'my string 3', - 'prop4' => [23, 24.4] - ] + 'prop3' => 'my string 1' + ], + $processedProperties )); }