Skip to content

Commit d63d85e

Browse files
committed
fix #3 add more test cases
1 parent f6cae27 commit d63d85e

4 files changed

Lines changed: 74 additions & 2 deletions

File tree

src/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Devscast\Pexels;
66

7+
use Devscast\Pexels\Data\Collection;
78
use Devscast\Pexels\Data\CollectionMedia;
89
use Devscast\Pexels\Data\Collections;
910
use Devscast\Pexels\Data\Photo;
@@ -305,7 +306,7 @@ private function getMappedData(string $type, array $data): Photos|Videos|Collect
305306
$data['videos']
306307
),
307308
$mapped instanceof Collections =>$mapped->collections = array_map(
308-
fn ($m) => $this->serializer->denormalize($m, type: Collections::class),
309+
fn ($m) => $this->serializer->denormalize($m, type: Collection::class),
309310
$data['collections']
310311
),
311312
$mapped instanceof CollectionMedia => $mapped->media = array_map(function ($m) {

src/Data/Resource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ abstract class Resource
3030
* @var string The Pexels URL where the resource is located.
3131
*/
3232
public string $url;
33+
34+
/**
35+
* @var string|null The type of the resource. Possible values are photo and video.
36+
*/
37+
public ?string $type = null;
3338
}

tests/ClientTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use Devscast\Pexels\Data\Photo;
1212
use Devscast\Pexels\Data\Photos;
1313
use Devscast\Pexels\Data\Videos;
14+
use Devscast\Pexels\Data\Collection;
1415
use Devscast\Pexels\Data\Collections;
1516
use Devscast\Pexels\Data\CollectionMedia;
1617
use Symfony\Component\HttpClient\MockHttpClient;
17-
use Symfony\Contracts\HttpClient\HttpClientInterface;
1818
use Symfony\Component\HttpClient\Response\MockResponse;
1919

2020
/**
@@ -48,6 +48,7 @@ public function testSearchPhotos(): void
4848
$photos = $pexels->searchPhotos('westie dog');
4949

5050
$this->assertInstanceOf(Photos::class, $photos);
51+
$this->assertContainsOnlyInstancesOf(Photo::class, $photos->photos);
5152
}
5253

5354
public function testSearchVideos(): void
@@ -57,6 +58,7 @@ public function testSearchVideos(): void
5758
$videos = $pexels->searchVideos('westie dog');
5859

5960
$this->assertInstanceOf(Videos::class, $videos);
61+
$this->assertContainsOnlyInstancesOf(Video::class, $videos->videos);
6062
}
6163

6264
public function testPhoto(): void
@@ -84,6 +86,7 @@ public function testPopularVideos(): void
8486
$videos = $pexels->popularVideos();
8587

8688
$this->assertInstanceOf(Videos::class, $videos);
89+
$this->assertContainsOnlyInstancesOf(Video::class, $videos->videos);
8790
}
8891

8992
public function curatedPhotos(): void
@@ -93,6 +96,7 @@ public function curatedPhotos(): void
9396
$photos = $pexels->curatedPhotos();
9497

9598
$this->assertInstanceOf(Photos::class, $photos);
99+
$this->assertContainsOnlyInstancesOf(Photo::class, $photos->photos);
96100
}
97101

98102
public function testFeaturedCollections(): void
@@ -102,6 +106,7 @@ public function testFeaturedCollections(): void
102106
$collections = $pexels->featuredCollections();
103107

104108
$this->assertInstanceOf(Collections::class, $collections);
109+
$this->assertContainsOnlyInstancesOf(Collection::class, $collections->collections);
105110
}
106111

107112
public function testCollections(): void
@@ -111,6 +116,7 @@ public function testCollections(): void
111116
$collections = $pexels->collections();
112117

113118
$this->assertInstanceOf(Collections::class, $collections);
119+
$this->assertContainsOnlyInstancesOf(Collection::class, $collections->collections);
114120
}
115121

116122
public function testCollection(): void
@@ -120,5 +126,14 @@ public function testCollection(): void
120126
$collections = $pexels->collection("33");
121127

122128
$this->assertInstanceOf(CollectionMedia::class, $collections);
129+
foreach ($collections->media as $media) {
130+
if ('photo' === $media->type) {
131+
$this->assertInstanceOf(Photo::class, $media);
132+
}
133+
134+
if ('video' === $media->type) {
135+
$this->assertInstanceOf(Video::class, $media);
136+
}
137+
}
123138
}
124139
}

tests/MapperTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devscast\Pexels\Tests;
6+
7+
use stdClass;
8+
use Devscast\Pexels\Mapper;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* Class MapperTest.
13+
*
14+
* @author bernard-ng <bernard@devscast.tech>
15+
*/
16+
final class MapperTest extends TestCase
17+
{
18+
public function testToObjectMethod(): void
19+
{
20+
// Test case: Test toObject method
21+
$data = ['name' => 'John', 'age' => 30];
22+
$object = new stdClass();
23+
24+
$resultObject = Mapper::toObject($object, $data);
25+
26+
// Assertions for toObject() method
27+
$this->assertInstanceOf(stdClass::class, $resultObject);
28+
$this->assertEquals('John', $resultObject->name);
29+
$this->assertEquals(30, $resultObject->age);
30+
}
31+
32+
public function testToArrayMethod(): void
33+
{
34+
// Test case: Test toArray method
35+
$data = new class {
36+
public string $name;
37+
public int $age;
38+
};
39+
$data->name = 'Alice';
40+
$data->age = 25;
41+
42+
$resultArray = Mapper::toArray([], $data);
43+
44+
// Assertions for toArray() method
45+
$this->assertIsArray($resultArray);
46+
$this->assertArrayHasKey('name', $resultArray);
47+
$this->assertEquals('Alice', $resultArray['name']);
48+
$this->assertArrayHasKey('age', $resultArray);
49+
$this->assertEquals(25, $resultArray['age']);
50+
}
51+
}

0 commit comments

Comments
 (0)