Skip to content

Commit

Permalink
fix file wrapper tests and refactored code
Browse files Browse the repository at this point in the history
Signed-off-by: Asad Ali <[email protected]>
  • Loading branch information
asadali214 committed Dec 17, 2024
1 parent eb97c18 commit 14d5c3b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
6 changes: 2 additions & 4 deletions src/Utils/CoreHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
}

Expand Down
98 changes: 48 additions & 50 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand All @@ -210,94 +210,92 @@ public function testToString()
$order->total = 250.75;
$order->delivered = true;

$expected = 'Placed Order [orderId: 123, sender: Customer [email: [email protected], ' .
$expected = 'Order [orderId: 123, sender: Customer [email: [email protected], ' .
'name: John Doe, additionalProperties: [age: 21]], similarOrders: [Order [orderId: 345], ' .
'Order [orderId: 567, sender: Customer [email: [email protected], 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;

$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
));
}

Expand Down

0 comments on commit 14d5c3b

Please sign in to comment.