Skip to content

Commit 02177c3

Browse files
committed
Tests finished
1 parent 2aafb8b commit 02177c3

File tree

3 files changed

+119
-10
lines changed

3 files changed

+119
-10
lines changed

tests/Unit/FixtureDataTest.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,59 @@
22

33
namespace Sammyjo20\Saloon\Tests\Unit;
44

5+
use Sammyjo20\Saloon\Data\FixtureData;
6+
use Sammyjo20\Saloon\Http\MockResponse;
7+
58
test('you can create a fixture data object from a file string', function () {
6-
//
9+
$data = [
10+
'statusCode' => 200,
11+
'headers' => [
12+
'Content-Type' => 'application/json'
13+
],
14+
'data' => [
15+
'name' => 'Sam'
16+
],
17+
];
18+
19+
$fixtureData = FixtureData::fromFile(json_encode($data));
20+
21+
expect($fixtureData->statusCode)->toEqual($data['statusCode']);
22+
expect($fixtureData->headers)->toEqual($data['headers']);
23+
expect($fixtureData->data)->toEqual($data['data']);
724
});
825

926
test('you can create a mock response from fixture data', function () {
10-
//
27+
$data = [
28+
'statusCode' => 200,
29+
'headers' => [
30+
'Content-Type' => 'application/json'
31+
],
32+
'data' => [
33+
'name' => 'Sam'
34+
],
35+
];
36+
37+
$fixtureData = FixtureData::fromFile(json_encode($data));
38+
$mockResponse = $fixtureData->toMockResponse();
39+
40+
expect($mockResponse)->toEqual(new MockResponse($data['data'], $data['statusCode'], $data['headers']));
1141
});
1242

1343
test('you can json serialize the fixture data or convert it into a file', function () {
14-
//
44+
$data = [
45+
'statusCode' => 200,
46+
'headers' => [
47+
'Content-Type' => 'application/json'
48+
],
49+
'data' => [
50+
'name' => 'Sam'
51+
],
52+
];
53+
54+
$fixtureData = FixtureData::fromFile(json_encode($data));
55+
56+
$serialized = json_encode($fixtureData);
57+
58+
expect($serialized)->toEqual(json_encode($data));
59+
expect($fixtureData->toFile())->toEqual($serialized);
1560
});
Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
<?php
22

3+
use Sammyjo20\Saloon\Clients\MockClient;
4+
use Sammyjo20\Saloon\Exceptions\DirectoryNotFoundException;
5+
use Sammyjo20\Saloon\Exceptions\FixtureMissingException;
6+
use Sammyjo20\Saloon\Helpers\MockConfig;
7+
use Sammyjo20\Saloon\Http\MockResponse;
8+
use Sammyjo20\Saloon\Tests\Fixtures\Requests\UserRequest;
9+
310
test('you can change the default fixture path', function () {
4-
//
11+
expect(MockConfig::getFixturePath())->toEqual('tests/Fixtures/Saloon');
12+
13+
MockConfig::setFixturePath('saloon-requests/responses');
14+
15+
expect(MockConfig::getFixturePath())->toEqual('saloon-requests/responses');
516
});
617

718
test('if the fixture path is invalid it will throw an exception', function () {
8-
//
9-
});
19+
MockConfig::setFixturePath('saloon-requests/responses');
20+
21+
new MockClient([
22+
MockResponse::fixture('example'),
23+
]);
24+
})->throws(DirectoryNotFoundException::class, 'The directory "saloon-requests/responses" does not exist or is not a valid directory.');
1025

1126
test('you can throw an exception if the fixture does not exist', function () {
12-
//
13-
});
27+
MockConfig::setFixturePath('tests/Fixtures/Saloon');
28+
29+
expect(MockConfig::isThrowingOnMissingFixtures())->toBeFalse();
30+
31+
MockConfig::throwOnMissingFixtures();
32+
33+
$mockClient = new MockClient([
34+
MockResponse::fixture('example'),
35+
]);
36+
37+
UserRequest::make()->send($mockClient);
38+
})->throws(FixtureMissingException::class, 'The fixture "example.json" could not be found in storage.');
1439

1540

tests/Unit/StorageTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,64 @@
11
<?php
22

3-
test('it will throw an exception if the base directory does not exist', function () {
3+
use League\Flysystem\Filesystem;
4+
use League\Flysystem\Local\LocalFilesystemAdapter;
5+
use Sammyjo20\Saloon\Exceptions\DirectoryNotFoundException;
6+
use Sammyjo20\Saloon\Helpers\Storage;
47

5-
});
8+
test('it will throw an exception if the base directory does not exist', function () {
9+
new Storage('example');
10+
})->throws(DirectoryNotFoundException::class, 'The directory "example" does not exist or is not a valid directory.');
611

712
test('you can check if a file exists', function () {
13+
$storage = new Storage('tests');
814

15+
expect($storage->exists('Pest.php'))->toBeTrue();
16+
expect($storage->missing('Pest.php'))->toBeFalse();
917
});
1018

1119
test('you can check if a file is missing', function () {
20+
$storage = new Storage('tests');
1221

22+
expect($storage->exists('HelloWorld.php'))->toBeFalse();
23+
expect($storage->missing('HelloWorld.php'))->toBeTrue();
1324
});
1425

1526
test('you can retrieve a file from storage', function () {
27+
$storage = new Storage('tests');
1628

29+
$file = $storage->get('Pest.php');
30+
31+
expect($file)->toEqual(file_get_contents('tests/Pest.php'));
1732
});
1833

1934
test('you can put a file in storage', function () {
35+
$filesystem = new Filesystem(new LocalFilesystemAdapter('tests/Fixtures/Saloon'));
36+
$filesystem->deleteDirectory('/');
37+
$filesystem->createDirectory('/');
38+
39+
$storage = new Storage('tests/Fixtures/Saloon');
40+
41+
expect($storage->exists('example.txt'))->toBeFalse();
2042

43+
$storage->put('example.txt', 'Hello World');
44+
45+
expect($storage->exists('example.txt'))->toBeTrue();
46+
47+
expect($storage->get('example.txt'))->toEqual('Hello World');
2148
});
2249

2350
test('it will create a file with nested folders', function () {
51+
$filesystem = new Filesystem(new LocalFilesystemAdapter('tests/Fixtures/Saloon'));
52+
$filesystem->deleteDirectory('/');
53+
$filesystem->createDirectory('/');
54+
55+
$storage = new Storage('tests/Fixtures/Saloon');
56+
57+
expect($storage->exists('some/other/directories/example.txt'))->toBeFalse();
58+
59+
$storage->put('some/other/directories/example.txt', 'Hello World');
60+
61+
expect($storage->exists('some/other/directories/example.txt'))->toBeTrue();
2462

63+
expect($storage->get('some/other/directories/example.txt'))->toEqual('Hello World');
2564
});

0 commit comments

Comments
 (0)