Skip to content

Commit f09aa1e

Browse files
author
Sebastian Fix
committed
Improved package
1 parent b5998e6 commit f09aa1e

7 files changed

Lines changed: 32 additions & 28 deletions

File tree

src/DTO/ConfigWithCredentials.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public function __construct(
1717
public ?string $cacheDriver = null,
1818
public int $tokenTtlSeconds = 3600,
1919
) {
20-
if ($this->tokenTtlSeconds < 1) {
21-
throw new InvalidArgumentException('Config [tokenTtlSeconds] must be at least 1 second.');
22-
}
20+
$this->tokenTtlSeconds = max(1, $this->tokenTtlSeconds);
2321
}
2422

2523
/**
@@ -119,10 +117,6 @@ private static function optionalPositiveInt(array $data, string $key, int $defau
119117
throw new InvalidArgumentException("Config [{$key}] must be a positive integer.");
120118
}
121119

122-
if ($int < 1) {
123-
throw new InvalidArgumentException("Config [{$key}] must be at least 1 second.");
124-
}
125-
126-
return $int;
120+
return max(1, $int);
127121
}
128122
}

src/DTO/DownloadedFile.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public function __construct(
2121
*/
2222
public static function fromArray(array $data): self
2323
{
24+
$size = Arr::get($data, 'size');
25+
2426
return new self(
2527
name: Arr::get($data, 'name'),
2628
extension: Arr::get($data, 'extension'),
27-
size: Arr::get($data, 'size') !== null ? (int) Arr::get($data, 'size') : null,
29+
size: $size !== null ? (int) $size : null,
2830
contentType: Arr::get($data, 'contentType'),
2931
content: Arr::get($data, 'content', ''),
3032
);

tests/Feature/Connectors/MFilesConnectorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CodebarAg\MFiles\DTO\ConfigWithCredentials;
77
use CodebarAg\MFiles\Helpers\CacheKeyManager;
88
use CodebarAg\MFiles\Requests\LogInToVaultRequest;
9+
use Illuminate\Support\Arr;
910
use Saloon\Http\Faking\MockResponse;
1011
use Saloon\Laravel\Facades\Saloon;
1112

@@ -54,7 +55,7 @@
5455

5556
$headers = $connector->defaultHeaders();
5657

57-
expect($headers['X-Authentication'])->toBe($token);
58+
expect(Arr::get($headers, 'X-Authentication'))->toBe($token);
5859
Saloon::assertSentCount(1);
5960
})->group('connectors');
6061

tests/TestCase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodebarAg\MFiles\Tests;
44

55
use CodebarAg\MFiles\MFilesServiceProvider;
6+
use Illuminate\Support\Arr;
67
use Orchestra\Testbench\TestCase as Orchestra;
78
use Saloon\Laravel\SaloonServiceProvider;
89

@@ -23,8 +24,8 @@ protected function getPackageProviders($app): array
2324

2425
public function getEnvironmentSetUp($app): void
2526
{
26-
$app['config']->set('database.default', 'sqlite');
27-
$app['config']->set('database.connections.sqlite', [
27+
Arr::get($app, 'config')->set('database.default', 'sqlite');
28+
Arr::get($app, 'config')->set('database.connections.sqlite', [
2829
'driver' => 'sqlite',
2930
'database' => ':memory:',
3031
'prefix' => '',

tests/Unit/DTO/ConfigWithCredentialsFromArrayTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,30 @@
5050
]);
5151
})->throws(InvalidArgumentException::class, 'Config [url] must be a non-empty string.');
5252

53-
test('fromArray rejects non positive tokenTtlSeconds', function () {
54-
ConfigWithCredentials::fromArray([
53+
test('fromArray clamps tokenTtlSeconds below 1 to 1', function () {
54+
$dto = ConfigWithCredentials::fromArray([
5555
'url' => 'https://a.test',
5656
'vaultGuid' => 'v',
5757
'username' => 'u',
5858
'password' => 'p',
5959
'tokenTtlSeconds' => 0,
6060
]);
61-
})->throws(InvalidArgumentException::class, 'at least 1 second');
6261

63-
test('constructor rejects tokenTtlSeconds below 1', function () {
64-
new ConfigWithCredentials(
62+
expect($dto->tokenTtlSeconds)->toBe(1);
63+
})->group('dto');
64+
65+
test('constructor clamps tokenTtlSeconds below 1 to 1', function () {
66+
$dto = new ConfigWithCredentials(
6567
url: 'https://a.test',
6668
vaultGuid: 'v',
6769
username: 'u',
6870
password: 'p',
6971
cacheDriver: null,
7072
tokenTtlSeconds: 0,
7173
);
72-
})->throws(InvalidArgumentException::class, 'Config [tokenTtlSeconds] must be at least 1 second.');
74+
75+
expect($dto->tokenTtlSeconds)->toBe(1);
76+
})->group('dto');
7377

7478
test('fromArray treats empty cacheDriver as null', function () {
7579
$dto = ConfigWithCredentials::fromArray([

tests/Unit/DTO/MFilesErrorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use CodebarAg\MFiles\DTO\MFilesError;
6+
use Illuminate\Support\Arr;
67

78
it('creates instance with all properties', function () {
89
$error = new MFilesError(
@@ -137,5 +138,5 @@
137138

138139
$array = $error->toArray();
139140

140-
expect($array['stack'])->toBeNull();
141+
expect(Arr::get($array, 'stack'))->toBeNull();
141142
});

tests/Unit/DTO/ObjectPropertiesTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use CodebarAg\MFiles\DTO\ObjectProperties;
1111
use CodebarAg\MFiles\Enums\MFDataTypeEnum;
1212
use CodebarAg\MFiles\Tests\TestCase;
13+
use Illuminate\Support\Arr;
1314
use Illuminate\Support\Collection;
1415

1516
class ObjectPropertiesTest extends TestCase
@@ -221,15 +222,15 @@ public function test_to_array_returns_correct_structure_with_collections(): void
221222

222223
$array = $objectProperties->toArray();
223224

224-
$this->assertEquals(2, $array['classId']);
225-
$this->assertEquals(456, $array['objectId']);
226-
$this->assertEquals(1, $array['objectTypeId']);
227-
$this->assertEquals(2, $array['objectVersionId']);
228-
$this->assertEquals('2023-01-01T10:00:00+00:00', $array['lastModifiedAt']);
229-
$this->assertIsArray($array['properties']);
230-
$this->assertIsArray($array['files']);
231-
$this->assertEquals(1, count($array['properties']));
232-
$this->assertEquals(1, count($array['files']));
225+
$this->assertEquals(2, Arr::get($array, 'classId'));
226+
$this->assertEquals(456, Arr::get($array, 'objectId'));
227+
$this->assertEquals(1, Arr::get($array, 'objectTypeId'));
228+
$this->assertEquals(2, Arr::get($array, 'objectVersionId'));
229+
$this->assertEquals('2023-01-01T10:00:00+00:00', Arr::get($array, 'lastModifiedAt'));
230+
$this->assertIsArray(Arr::get($array, 'properties'));
231+
$this->assertIsArray(Arr::get($array, 'files'));
232+
$this->assertEquals(1, count(Arr::get($array, 'properties', [])));
233+
$this->assertEquals(1, count(Arr::get($array, 'files', [])));
233234
}
234235

235236
public function test_properties_are_readonly(): void

0 commit comments

Comments
 (0)