ReviewOS

also looking at this

meemalabs/meema-client-php

add test case

#37
Merged glennmichael123 wants to merge test-environment into main
8 files +106 -88
.env.exampleadded+3-0
Changes to .env.example
@@ -0,0 +1,3 @@
1BASE_URL="http://meema-api.test/api/"
2BASE_URL_TEST="https://test-api.mee.ma/api/"
3PUBLISHABLE_KEY=
.github/workflows/run-tests.ymlmodified+12-4
Changes to .github/workflows/run-tests.yml
@@ -1,7 +1,6 @@
11name: Run Tests
22
33on:
4 push:
54 pull_request:
65
76jobs:
@@ -10,8 +9,8 @@ jobs:
109 strategy:
1110 fail-fast: false
1211 matrix:
13 php: [7.4, 8.0]
14 dependency-version: [prefer-lowest, prefer-stable]
12 php: [7.4]
13 dependency-version: [prefer-stable]
1514
1615 name: P ${{ matrix.php }} - ${{ matrix.dependency-version }}
1716
@@ -33,4 +32,13 @@ jobs:
3332
3433 - name: Install dependencies
3534 run: |
36 composer require "orchestra/testbench" --no-interaction --no-update
35 composer install
36 - name: Make envfile
37 uses: SpicyPizza/create-envfile@v1
38 with:
39 envkey_BASE_URL: "http://meema-api.test/api/"
40 envkey_BASE_URL_TEST: "https://test-api.mee.ma/api/"
41 envkey_PUBLISHABLE_KEY: ${{ secrets.PUBLISHABLE_KEY }}
42 file_name: .env
43 - name: Execute tests
44 run: vendor/bin/pest
src/Client.phpmodified+6-3
Changes to src/Client.php
@@ -26,13 +26,16 @@ class Client
2626 *
2727 * @param string $accessKey
2828 */
29 public function __construct($accessKey)
29 public function __construct($accessKey, $baseUrl = null)
3030 {
3131 $this->initializeEnv();
3232
3333 $this->accessKey = $accessKey;
34
35 $url = $baseUrl ?? $_ENV['BASE_URL'];
36
3437 $this->client = new GuzzleClient([
35 'base_uri' => $_ENV['BASE_URL'] ?? 'https://api.mee.ma',
38 'base_uri' => $url ?? 'https://api.mee.ma',
3639 ]);
3740 }
3841
@@ -56,7 +59,7 @@ class Client
5659 public function request($method, $path, $data = null)
5760 {
5861 $content = $this->client->request($method, $path, [
59 'json' => $data,
62 'query' => $data,
6063 'headers' => [
6164 'Content-Type' => 'application/json',
6265 'Authorization' => "Bearer {$this->accessKey}",
src/Models/Folder.phpmodified+6-4
Changes to src/Models/Folder.php
@@ -58,12 +58,14 @@ class Folder
5858 return $this->all();
5959 }
6060
61 if (! is_int($id)) {
62 throw new InvalidFormatException();
63 }
64
6561 $ids = is_array($id) ? $id : func_get_args();
6662
63 foreach ($ids as $id) {
64 if (! is_int($id)) {
65 throw new InvalidFormatException();
66 }
67 }
68
6769 return $this->client->request('GET', 'folders', ['folder_ids' => $ids]);
6870 }
6971
src/Models/Media.phpmodified+6-4
Changes to src/Models/Media.php
@@ -60,12 +60,14 @@ class Media
6060 return $this->all();
6161 }
6262
63 if (! is_int($id)) {
64 throw new InvalidFormatException();
65 }
66
6763 $ids = is_array($id) ? $id : func_get_args();
6864
65 foreach ($ids as $id) {
66 if (! is_int($id)) {
67 throw new InvalidFormatException();
68 }
69 }
70
6971 return $this->client->request('GET', 'media', ['media_ids' => $ids]);
7072 }
7173
tests/ClientTest.phpmodified+2-2
Changes to tests/ClientTest.php
@@ -21,9 +21,9 @@ class ClientTest extends TestCase
2121
2222 public function initializeClient()
2323 {
24 $teamId = 'pk_live|2|1|M3g6t5NxE5pdSWq833zG0BFf0LbBO2j9bX8FP4ft';
24 $teamId = env('PUBLISHABLE_KEY');
2525
26 $client = new Client($teamId);
26 $client = new Client($teamId, env('BASE_URL_TEST'));
2727
2828 $this->client = $client;
2929
tests/MeemaFolderTest.phpmodified+12-12
Changes to tests/MeemaFolderTest.php
@@ -32,14 +32,14 @@ it('can find a single folder', function () {
3232 $this->assertTrue(count($folders) === 1);
3333});
3434
35// it('can create a folder', function () {
36// $name = 'test folder';
35it('can create a folder', function () {
36 $name = 'test folder';
3737
38// $media = $this->client->folders()->create($name);
38 $media = $this->client->folders()->create($name);
3939
40// $this->assertTrue(is_array($media));
41// $this->assertTrue($media['data']['name'] === $name);
42// });
40 $this->assertTrue(is_array($media));
41 $this->assertTrue($media['data']['name'] === $name);
42});
4343
4444it('can update a folder', function () {
4545 $name = 'test folder';
@@ -50,14 +50,14 @@ it('can update a folder', function () {
5050 $this->assertTrue($folders['data']['name'] === $name);
5151});
5252
53it('can search a folder', function () {
54 $query = 'Invoice';
53// it('can search a folder', function () {
54// $query = 'Invoice';
5555
56 $folders = $this->client->folders()->search($query);
56// $folders = $this->client->folders()->search($query);
5757
58 $this->assertTrue(is_array($folders));
59 $this->assertTrue(str_contains($folders['data'][0]['name'], $query));
60});
58// $this->assertTrue(is_array($folders));
59// $this->assertTrue(str_contains($folders['data'][0]['name'], $query));
60// });
6161
6262it('can archive a folder', function () {
6363 $folders = $this->client->folders()->archive(1);
tests/MeemaMediaTest.phpmodified+59-59
Changes to tests/MeemaMediaTest.php
@@ -7,39 +7,39 @@ beforeEach(function () {
77 $this->initializeClient();
88});
99
10// it('can be fetch all media', function () {
11// $media = $this->client->media()->get();
10it('can be fetch all media', function () {
11 $media = $this->client->media()->get();
1212
13// $this->assertTrue(is_array($media));
14// $this->assertTrue(count($media) > 0);
15// });
13 $this->assertTrue(is_array($media));
14 $this->assertTrue(count($media) > 0);
15});
1616
17// it('can be fetch specific group of media', function () {
18// $ids = [1,2,3];
17it('can be fetch specific group of media', function () {
18 $ids = [1, 2, 3];
1919
20// $media = $this->client->media()->get($ids);
20 $media = $this->client->media()->get($ids);
2121
22// $this->assertTrue(is_array($media));
23// $this->assertTrue(count($media['data']) === count($ids));
24// });
22 $this->assertTrue(is_array($media));
23 $this->assertTrue(count($media['data']) === count($ids));
24});
2525
26// it('can find a single media', function () {
27// $id = 1;
26it('can find a single media', function () {
27 $id = 1;
2828
29// $media = $this->client->media()->find($id)->toArray();
29 $media = $this->client->media()->find($id)->toArray();
3030
31// $this->assertTrue(is_array($media));
32// $this->assertTrue(count($media) === 1);
33// });
31 $this->assertTrue(is_array($media));
32 $this->assertTrue(count($media) === 1);
33});
3434
35// it('can update a media', function () {
36// $name = 'test media';
35it('can update a media', function () {
36 $name = 'test media';
3737
38// $media = $this->client->media()->update(1, $name);
38 $media = $this->client->media()->update(1, $name);
3939
40// $this->assertTrue(is_array($media));
41// $this->assertTrue($media['data']['name'] === $name);
42// });
40 $this->assertTrue(is_array($media));
41 $this->assertTrue($media['data']['name'] === $name);
42});
4343
4444// it('can search a media', function () {
4545// $query = 'michael';
@@ -50,56 +50,56 @@ beforeEach(function () {
5050// $this->assertTrue(str_contains($media['data'][0]['name'], $query));
5151// });
5252
53// it('can archive a media', function () {
54// $query = 'michael';
53it('can archive a media', function () {
54 $query = 'michael';
5555
56// $media = $this->client->media()->archive(1);
56 $media = $this->client->media()->archive(1);
5757
58// $this->assertTrue(is_array($media));
59// $this->assertTrue((bool) $media['data']['is_archived']);
60// });
58 $this->assertTrue(is_array($media));
59 $this->assertTrue((bool) $media['data']['is_archived']);
60});
6161
62// it('can unarchive a media', function () {
63// $query = 'michael';
62it('can unarchive a media', function () {
63 $query = 'michael';
6464
65// $media = $this->client->media()->unarchive(1);
65 $media = $this->client->media()->unarchive(1);
6666
67// $this->assertTrue(is_array($media));
68// $this->assertFalse((bool) $media['data']['is_archived']);
69// });
67 $this->assertTrue(is_array($media));
68 $this->assertFalse((bool) $media['data']['is_archived']);
69});
7070
71// it('can make a media private', function () {
72// $query = 'michael';
71it('can make a media private', function () {
72 $query = 'michael';
7373
74// $media = $this->client->media()->makePrivate(1);
74 $media = $this->client->media()->makePrivate(1);
7575
76// $this->assertTrue(is_array($media));
77// $this->assertFalse((bool) $media['data']['is_public']);
78// });
76 $this->assertTrue(is_array($media));
77 $this->assertFalse((bool) $media['data']['is_public']);
78});
7979
80// it('can make a media public', function () {
81// $query = 'michael';
80it('can make a media public', function () {
81 $query = 'michael';
8282
83// $media = $this->client->media()->makePublic(1);
83 $media = $this->client->media()->makePublic(1);
8484
85// $this->assertTrue(is_array($media));
86// $this->assertTrue((bool) $media['data']['is_public']);
87// });
85 $this->assertTrue(is_array($media));
86 $this->assertTrue((bool) $media['data']['is_public']);
87});
8888
89// it('can duplicate a media', function () {
90// $media = $this->client->media()->find(1);
89it('can duplicate a media', function () {
90 $media = $this->client->media()->find(1);
9191
92// $duplicated = $media->duplicate();
92 $duplicated = $this->client->media()->duplicate(1);
9393
94// $this->assertTrue(is_array($duplicated));
95// $this->assertTrue($duplicated['data']['name'] === $media->toArray()['data']['name']);
96// });
94 $this->assertTrue(is_array($duplicated));
95 $this->assertTrue($duplicated['data']['name'] === $media->toArray()['data']['name']);
96});
9797
98// it('can delete a media', function () {
99// $media = $this->client->media()->get();
98it('can delete a media', function () {
99 $media = $this->client->media()->get();
100100
101// $media = array_reverse($media['data']);
102// $response = $this->client->media()->delete($media[0]['id']);
101 $media = array_reverse($media['data']);
102 $response = $this->client->media()->delete((int) $media[0]['upload_id']);
103103
104// $this->assertTrue(is_null($response));
105// });
104 $this->assertTrue(is_null($response));
105});