-
-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathAbstractTestCase.php
138 lines (123 loc) · 3.92 KB
/
AbstractTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/
/**
* We don't care for unhandled exceptions in tests.
* It is the nature of a test to throw an exception.
* Without this suppression we had 100+ Linter warning in this file which
* don't help anything.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
namespace Tests;
use App\Models\Configs;
use App\Models\Photo;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Testing\TestResponse;
use function Safe\copy;
use function Safe\json_decode;
use function Safe\tempnam;
use Tests\Constants\TestConstants;
use Tests\Traits\CatchFailures;
abstract class AbstractTestCase extends BaseTestCase
{
use CreatesApplication;
use CatchFailures;
/**
* Visit the given URI with a GET request.
*
* Inspired by
* {@link \Illuminate\Foundation\Testing\Concerns\MakesHttpRequests::get()}
* but transmits additional parameters in the query string.
*
* @param string $uri
* @param array $queryParameters
* @param array $headers
*
* @return TestResponse<\Illuminate\Http\JsonResponse>
*/
public function getWithParameters(string $uri, array $queryParameters = [], array $headers = []): TestResponse
{
$server = $this->transformHeadersToServerVars($headers);
$cookies = $this->prepareCookiesForRequest();
return $this->call('GET', $uri, $queryParameters, $cookies, [], $server);
}
/**
* Converts the JSON content of the response into a PHP standard object.
*
* @param TestResponse<\Illuminate\Http\JsonResponse> $response
*
* @return object
*/
protected static function convertJsonToObject(TestResponse $response): object
{
$content = $response->getContent();
self::assertNotFalse($content);
return json_decode($content);
}
/**
* Creates a new "uploaded" file from one of the sample files.
*
* This method creates a copy of the sample file with a random file name
* and without any extension in the system's temporary directory.
* This mimics the exact behaviour of a true upload.
* In particular, the missing file extension is important as we don't
* have this for true uploads either, and without a file extension
* the MIME detector cannot rely on that.
*
* @param string $sampleFilePath the relative path to the sample file;
* use one of the `SAMPLE_FILE_...`-constants
*
* @return UploadedFile
*/
protected static function createUploadedFile(string $sampleFilePath): UploadedFile
{
$tmpFilename = tempnam(sys_get_temp_dir(), 'lychee');
copy(base_path($sampleFilePath), $tmpFilename);
return new UploadedFile(
$tmpFilename,
pathinfo($sampleFilePath, PATHINFO_BASENAME),
TestConstants::SAMPLE_FILES_2_MIME[$sampleFilePath],
UPLOAD_ERR_OK,
true
);
}
protected static function importPath(string $path = ''): string
{
return public_path(TestConstants::PATH_IMPORT_DIR . $path);
}
/**
* @return BaseCollection<int,string> the IDs of recently added photos
*/
protected static function getRecentPhotoIDs(): BaseCollection
{
$strRecent = Carbon::now()
->subDays(Configs::getValueAsInt('recent_age'))
->setTimezone('UTC')
->format('Y-m-d H:i:s');
$recentFilter = function (Builder $query) use ($strRecent) {
$query->where('created_at', '>=', $strRecent);
};
return Photo::query()->select('id')->where($recentFilter)->pluck('id');
}
/**
* Because we are now using hard coded urls for the images size_variants instead of relative.
* We need to drop that prefix in order to access them from public_path().
*
* @param string $url
*
* @return string prefix removed
*/
protected function dropUrlPrefix(string $url): string
{
return str_replace(config('app.url'), '', $url);
}
}