Skip to content

Commit ba13034

Browse files
Add unit tests for CanUpload trait
Introduce new unit tests for the CanUpload trait to ensure proper handling of setting API endpoints, exception throwing during uploads and removals, and successful removals. Additionally, update composer.json to include fakerphp/faker dependency for test data generation.
1 parent 6d892d6 commit ba13034

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"require-dev": {
3939
"pestphp/pest": "^2.34",
4040
"phpstan/phpstan": "^1.10",
41-
"mockery/mockery": "^1.6"
41+
"mockery/mockery": "^1.6",
42+
"fakerphp/faker": "^1.23"
4243
},
4344
"autoload": {
4445
"psr-4": {

tests/Unit/Traits/CanUploadTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use UnitPhpSdk\Traits\CanUpload;
5+
use UnitPhpSdk\Http\UnitRequest;
6+
use UnitPhpSdk\Exceptions\UnitException;
7+
use UnitPhpSdk\Enums\HttpMethodsEnum;
8+
use Faker\Factory as Faker;
9+
10+
uses(TestCase::class);
11+
12+
beforeEach(function () {
13+
$this->faker = Faker::create();
14+
});
15+
16+
it('sets and gets API endpoint', function () {
17+
$upload = new class { use CanUpload; };
18+
19+
$apiEndpoint = $this->faker->url;
20+
$upload->setApiEndpoint($apiEndpoint);
21+
expect($upload->getApiEndpoint())->toBe($apiEndpoint);
22+
});
23+
24+
// TODO: fix
25+
//it('uploads data successfully', function () {
26+
// $upload = new class {
27+
// use CanUpload;
28+
// public function toArray() {
29+
// return ['key' => $this->faker->word];
30+
// }
31+
// };
32+
//
33+
// $request = Mockery::mock(UnitRequest::class);
34+
// $request->shouldReceive('setMethod')->with(HttpMethodsEnum::PUT->value)->andReturnSelf();
35+
// $request->shouldReceive('send')->with(Mockery::any(), true, ['json' => ['key' => Mockery::any()]])->andReturnSelf();
36+
//
37+
// $apiEndpoint = $this->faker->url;
38+
// $upload->setApiEndpoint($apiEndpoint);
39+
//
40+
// try {
41+
// $upload->upload($request);
42+
// expect(true)->toBeTrue();
43+
// } catch (UnitException $e) {
44+
// expect(false)->toBeTrue();
45+
// }
46+
//});
47+
48+
it('throws exception during upload', function () {
49+
$upload = new class {
50+
use CanUpload;
51+
public function toArray() {
52+
return ['key' => $this->faker->word];
53+
}
54+
};
55+
56+
$request = Mockery::mock(UnitRequest::class);
57+
$request->shouldReceive('setMethod')->with(HttpMethodsEnum::PUT->value)->andReturnSelf();
58+
$request->shouldReceive('send')->andThrow(UnitException::class, 'Upload failed');
59+
60+
$apiEndpoint = $this->faker->url;
61+
$upload->setApiEndpoint($apiEndpoint);
62+
63+
expect(fn() => $upload->upload($request))->toThrow(UnitException::class, 'Upload failed');
64+
});
65+
66+
it('removes data successfully', function () {
67+
$upload = new class {
68+
use CanUpload;
69+
};
70+
71+
$request = Mockery::mock(UnitRequest::class);
72+
$request->shouldReceive('setMethod')->with(HttpMethodsEnum::DELETE->value)->andReturnSelf();
73+
$request->shouldReceive('send')->with(Mockery::any())->andReturnSelf();
74+
75+
$apiEndpoint = $this->faker->url;
76+
$upload->setApiEndpoint($apiEndpoint);
77+
78+
try {
79+
$upload->remove($request);
80+
expect(true)->toBeTrue();
81+
} catch (UnitException $e) {
82+
expect(false)->toBeTrue();
83+
}
84+
});
85+
86+
it('throws exception during remove', function () {
87+
$upload = new class {
88+
use CanUpload;
89+
};
90+
91+
$request = Mockery::mock(UnitRequest::class);
92+
$request->shouldReceive('setMethod')->with(HttpMethodsEnum::DELETE->value)->andReturnSelf();
93+
$request->shouldReceive('send')->andThrow(UnitException::class, 'Remove failed');
94+
95+
$apiEndpoint = $this->faker->url;
96+
$upload->setApiEndpoint($apiEndpoint);
97+
98+
expect(fn() => $upload->remove($request))->toThrow(UnitException::class, 'Remove failed');
99+
});

0 commit comments

Comments
 (0)