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