ldc-cocotte/tests/Integration/WithApp/Helpers/ToolboxHelpercoreTest.php
2026-10-27 09:58:12 +01:00

118 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Exceptions\Helpers\UnsupportedStructureConversionException;
use Illuminate\Support\Facades\Storage;
use Tests\Integration\WithApp\Helpers\ToolboxHelperCoreHarness;
it('returns false when replace is array but search is string', function (): void {
// GIVEN
$count = 0;
// WHEN
$result = ToolboxHelperCoreHarness::mbReplace('a', ['b'], 'aaa', $count);
// THEN
expect($result)->toBeFalse();
});
it('replaces recursively in nested array subject', function (): void {
// GIVEN
$count = 0;
$subject = ['a.b', ['a.b', 'z']];
// WHEN
$result = ToolboxHelperCoreHarness::mbReplace('.', '-', $subject, $count);
// THEN
expect($result)->toBe(['a-b', ['a-b', 'z']])
->and($count)->toBe(0);
});
it('replaces with search array and scalar replacement', function (): void {
// GIVEN
$count = 0;
// WHEN
$result = ToolboxHelperCoreHarness::mbReplace(['ab', 'cd'], '_', 'ab-cd', $count);
// THEN
expect($result)->toBe('_-_');
});
it('replaces with paired search and replace arrays', function (): void {
// GIVEN
$count = 0;
// WHEN
$result = ToolboxHelperCoreHarness::mbReplace(['ab', 'cd'], ['X', 'Y'], 'ab-cd', $count);
// THEN
expect($result)->toBe('X-Y');
});
it('converts nested object to array and array to object', function (): void {
// GIVEN
$object = (object) [
'name' => 'demo',
'nested' => (object) [
'value' => 10,
],
];
// WHEN
$asArray = ToolboxHelperCoreHarness::objectToArray($object);
$asObject = ToolboxHelperCoreHarness::arrayToObject($asArray);
// THEN
expect($asArray)->toBe([
'name' => 'demo',
'nested' => ['value' => 10],
])
->and($asObject)->toBeObject()
->and($asObject->nested)->toBeObject()
->and($asObject->nested->value)->toBe(10);
});
it('throws for unsupported scalar structure conversion', function (): void {
// WHEN / THEN
expect(fn () => ToolboxHelperCoreHarness::objectToArray('x'))
->toThrow(UnsupportedStructureConversionException::class)
->and(fn () => ToolboxHelperCoreHarness::arrayToObject(12))
->toThrow(UnsupportedStructureConversionException::class);
});
it('resolves full path and mime type without storage disk', function (): void {
// GIVEN
$tmpFile = tempnam(sys_get_temp_dir(), 'tbx-');
expect($tmpFile)->toBeString();
file_put_contents((string) $tmpFile, 'hello');
// WHEN
$fullPath = ToolboxHelperCoreHarness::exposeGetUploadedFileFullPath((string) $tmpFile, false);
$mimeType = ToolboxHelperCoreHarness::exposeGetUploadedFileMimeType((string) $tmpFile, false);
// THEN
expect($fullPath)->toBe(realpath((string) $tmpFile) ?: (string) $tmpFile)
->and($mimeType)->toBeString();
unlink((string) $tmpFile);
});
it('resolves full path and mime type through storage disk', function (): void {
// GIVEN
$path = 'tests/toolbox-core/demo.txt';
Storage::disk()->put($path, 'hello world');
// WHEN
$fullPath = ToolboxHelperCoreHarness::exposeGetUploadedFileFullPath($path, true);
$mimeType = ToolboxHelperCoreHarness::exposeGetUploadedFileMimeType($path, true);
// THEN
expect($fullPath)->toBeString()
->and($fullPath)->toContain('demo.txt')
->and($mimeType)->toBeString();
Storage::disk()->delete($path);
});