Skip to content

Commit c6a6e0e

Browse files
chore: Improve performance during tests
1 parent 1f39289 commit c6a6e0e

File tree

10 files changed

+49
-122
lines changed

10 files changed

+49
-122
lines changed

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Configuration extends \Minz\Configuration
4040
*/
4141
public static array $application;
4242

43+
public static function isEnvironment(string $environment): bool
44+
{
45+
return self::$environment === $environment;
46+
}
47+
4348
public static function areRegistrationsOpened(): bool
4449
{
4550
return self::$application['registrations_opened'];

src/jobs/scheduled/InactivityNotifier.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public function perform(): void
5151
$user->save();
5252
}
5353

54-
$sleep_seconds = random_int(2, 5);
55-
sleep($sleep_seconds);
54+
if (!\App\Configuration::isEnvironment('test')) {
55+
$sleep_seconds = random_int(2, 5);
56+
sleep($sleep_seconds);
57+
}
5658
}
5759
}
5860
}

src/utils/view_helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function format_publication_frequency(int $frequency_per_year): string
109109

110110
function is_environment(string $environment): bool
111111
{
112-
return \App\Configuration::$environment === $environment;
112+
return \App\Configuration::isEnvironment($environment);
113113
}
114114

115115
function get_app_configuration(string $key): mixed

tests/cli/FeedsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public function testSyncUsesCache(): void
215215
$expected_name = $this->fake('sentence');
216216
/** @var string */
217217
$expected_title = $this->fake('sentence');
218+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
218219
$hash = \SpiderBits\Cache::hash($feed_url);
219220
$raw_response = <<<XML
220221
HTTP/2 200 OK

tests/cli/TopicsTest.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,6 @@ public function testCreateCreatesATopicAndRendersCorrectly(): void
5656
$this->assertSame(1, models\Topic::count());
5757
}
5858

59-
public function testCreateDownloadsImageIfPassed(): void
60-
{
61-
/** @var string */
62-
$label = $this->fake('word');
63-
$image_url = 'https://flus.fr/carnet/card.png';
64-
65-
$response = $this->appRun('CLI', '/topics/create', [
66-
'label' => $label,
67-
'image_url' => $image_url,
68-
]);
69-
70-
$topic = models\Topic::take();
71-
$this->assertNotNull($topic);
72-
$image_filename = $topic->image_filename;
73-
$this->assertNotNull($image_filename);
74-
$media_path = \App\Configuration::$application['media_path'];
75-
$subpath = utils\Belt::filenameToSubpath($image_filename);
76-
$card_filepath = "{$media_path}/cards/{$subpath}/{$image_filename}";
77-
$cover_filepath = "{$media_path}/covers/{$subpath}/{$image_filename}";
78-
$large_filepath = "{$media_path}/large/{$subpath}/{$image_filename}";
79-
$this->assertTrue(file_exists($card_filepath));
80-
$this->assertTrue(file_exists($cover_filepath));
81-
$this->assertTrue(file_exists($large_filepath));
82-
}
83-
8459
public function testCreateFailsIfLabelIsTooLong(): void
8560
{
8661
$label_max_size = models\Topic::LABEL_MAX_SIZE;
@@ -131,37 +106,6 @@ public function testUpdateChangesLabelIfPassed(): void
131106
$this->assertSame($new_label, $topic->label);
132107
}
133108

134-
public function testUpdateChangesImageFilenameIfPassed(): void
135-
{
136-
/** @var string */
137-
$old_image_filename = $this->fakeUnique('sha256');
138-
$old_image_filename = $old_image_filename . '.jpg';
139-
$image_url = 'https://flus.fr/carnet/card.png';
140-
$topic = TopicFactory::create([
141-
'image_filename' => $old_image_filename,
142-
]);
143-
144-
$response = $this->appRun('CLI', '/topics/update', [
145-
'id' => $topic->id,
146-
'image_url' => $image_url,
147-
]);
148-
149-
$this->assertResponseCode($response, 200);
150-
$this->assertResponseContains($response, 'has been updated');
151-
$topic = $topic->reload();
152-
$this->assertNotNull($topic->image_filename);
153-
$this->assertNotSame($old_image_filename, $topic->image_filename);
154-
$media_path = \App\Configuration::$application['media_path'];
155-
$image_filename = $topic->image_filename;
156-
$subpath = utils\Belt::filenameToSubpath($image_filename);
157-
$card_filepath = "{$media_path}/cards/{$subpath}/{$image_filename}";
158-
$cover_filepath = "{$media_path}/covers/{$subpath}/{$image_filename}";
159-
$large_filepath = "{$media_path}/large/{$subpath}/{$image_filename}";
160-
$this->assertTrue(file_exists($card_filepath));
161-
$this->assertTrue(file_exists($cover_filepath));
162-
$this->assertTrue(file_exists($large_filepath));
163-
}
164-
165109
public function testUpdateDoesNothingIfLabelIsEmpty(): void
166110
{
167111
/** @var string */

tests/cli/UrlsTest.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class UrlsTest extends \PHPUnit\Framework\TestCase
66
{
77
use \Minz\Tests\ApplicationHelper;
8+
use \Minz\Tests\InitializerHelper;
89
use \Minz\Tests\ResponseAsserts;
910
use \tests\FakerHelper;
1011
use \tests\FilesystemHelper;
@@ -64,18 +65,6 @@ public function testShowFailsWithInvalidUrl(): void
6465
$this->assertResponseEquals($response, '`not an url` is not a valid URL.');
6566
}
6667

67-
public function testShowFailsWithUnresolvableUrl(): void
68-
{
69-
$url = 'http://unresolvable-url';
70-
71-
$response = $this->appRun('CLI', '/urls/show', [
72-
'url' => $url,
73-
]);
74-
75-
$this->assertResponseCode($response, 500);
76-
$this->assertResponseContains($response, 'Could not resolve host: unresolvable-url');
77-
}
78-
7968
public function testUncacheClearsCacheOfGivenUrl(): void
8069
{
8170
$cache_path = \App\Configuration::$application['cache_path'];

tests/controllers/links/SearchesTest.php

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,49 +90,6 @@ public function testShowDisplaysFeedCollections(): void
9090
$this->assertResponseContains($response, $name);
9191
}
9292

93-
public function testShowHidesDuplicatedFeeds(): void
94-
{
95-
$user = $this->login();
96-
$support_user = models\User::supportUser();
97-
/** @var string */
98-
$name = $this->fake('sentence');
99-
/** @var string */
100-
$feed_url_rss = $this->fakeUnique('url');
101-
/** @var string */
102-
$feed_url_atom = $this->fakeUnique('url');
103-
$collection_rss = CollectionFactory::create([
104-
'type' => 'feed',
105-
'user_id' => $support_user->id,
106-
'is_public' => true,
107-
'name' => $name,
108-
'feed_url' => $feed_url_rss,
109-
]);
110-
$collection_atom = CollectionFactory::create([
111-
'type' => 'feed',
112-
'user_id' => $support_user->id,
113-
'is_public' => true,
114-
'name' => $name,
115-
'feed_url' => $feed_url_atom,
116-
]);
117-
/** @var string */
118-
$link_url = $this->fake('url');
119-
$link = LinkFactory::create([
120-
'user_id' => $user->id,
121-
'url' => $link_url,
122-
'url_feeds' => [$feed_url_rss, $feed_url_atom],
123-
]);
124-
125-
$response = $this->appRun('GET', '/links/search', [
126-
'url' => $link_url,
127-
]);
128-
129-
$this->assertResponseCode($response, 200);
130-
$this->assertResponseContains($response, $collection_rss->id);
131-
// Only the first feed is considered if name are the same but feed
132-
// types differ.
133-
$this->assertResponseNotContains($response, $collection_atom->id);
134-
}
135-
13693
public function testShowRedirectsIfNotConnected(): void
13794
{
13895
/** @var string */

tests/controllers/my/AccountTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ public function testShowRendersIfSubscriptionIsOverdue(): void
7575
'subscription_expired_at' => $expired_at,
7676
'validated_at' => \Minz\Time::now(),
7777
]);
78+
$subscriptions_host = \App\Configuration::$application['subscriptions_host'];
79+
$subscription_api_url = "{$subscriptions_host}/api/account/expired-at?account_id={$account_id}";
80+
$this->mockHttpWithResponse($subscription_api_url, <<<TEXT
81+
HTTP/2 200
82+
Content-type: application/json
83+
84+
{
85+
"expired_at": "{$expired_at->format(\Minz\Database\Column::DATETIME_FORMAT)}"
86+
}
87+
TEXT
88+
);
7889

7990
$response = $this->appRun('GET', '/my/account');
8091

tests/jobs/scheduled/FeedsSyncTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public function testPerformUsesCache(): void
179179
'collection_id' => $collection->id,
180180
'user_id' => $user->id,
181181
]);
182+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
182183
$hash = \SpiderBits\Cache::hash($feed_url);
183184
$raw_response = <<<XML
184185
HTTP/2 200 OK
@@ -237,6 +238,7 @@ public function testPerformSavesPublishedDate(): void
237238
'collection_id' => $collection->id,
238239
'user_id' => $user->id,
239240
]);
241+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
240242
$hash = \SpiderBits\Cache::hash($feed_url);
241243
$raw_response = <<<XML
242244
HTTP/2 200 OK
@@ -386,6 +388,7 @@ public function testPerformDuplicatesLinkUrlIfNotInCollection(): void
386388
'feed_entry_id' => null,
387389
'created_at' => \Minz\Time::now(),
388390
]);
391+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
389392
$hash = \SpiderBits\Cache::hash($feed_url);
390393
$raw_response = <<<XML
391394
HTTP/2 200 OK
@@ -489,6 +492,7 @@ public function testPerformUsesIdAsLinkIfEntryHasNoLink(): void
489492
'collection_id' => $collection->id,
490493
'user_id' => $user->id,
491494
]);
495+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
492496
$hash = \SpiderBits\Cache::hash($feed_url);
493497
$raw_response = <<<XML
494498
HTTP/2 200 OK
@@ -546,6 +550,7 @@ public function testPerformIgnoresEntriesWithNoLink(): void
546550
'collection_id' => $collection->id,
547551
'user_id' => $user->id,
548552
]);
553+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
549554
$hash = \SpiderBits\Cache::hash($feed_url);
550555
$raw_response = <<<XML
551556
HTTP/2 200 OK
@@ -595,6 +600,7 @@ public function testPerformIgnoresEntriesWithInvalidUrl(): void
595600
'user_id' => $user->id,
596601
]);
597602
$link_url = 'invalid://example.com';
603+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
598604
$hash = \SpiderBits\Cache::hash($feed_url);
599605
$raw_response = <<<XML
600606
HTTP/2 200 OK
@@ -659,6 +665,7 @@ public function testPerformIgnoresEntriesIfUrlExistsInCollection(): void
659665
'collection_id' => $collection->id,
660666
'link_id' => $link->id,
661667
]);
668+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
662669
$hash = \SpiderBits\Cache::hash($feed_url);
663670
$raw_response = <<<XML
664671
HTTP/2 200 OK
@@ -712,6 +719,7 @@ public function testPerformIgnoresEntriesIfOverKeepMaximum(): void
712719
FollowedCollectionFactory::create([
713720
'collection_id' => $collection->id,
714721
]);
722+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
715723
$hash = \SpiderBits\Cache::hash($feed_url);
716724
$raw_response = <<<XML
717725
HTTP/2 200 OK
@@ -779,6 +787,7 @@ public function testPerformIgnoresEntriesIfOlderThanKeepPeriod(): void
779787
FollowedCollectionFactory::create([
780788
'collection_id' => $collection->id,
781789
]);
790+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
782791
$hash = \SpiderBits\Cache::hash($feed_url);
783792
$raw_response = <<<XML
784793
HTTP/2 200 OK
@@ -833,6 +842,7 @@ public function testPerformTakesEntriesIfRecentEnoughWhenKeepPeriodIsSet(): void
833842
FollowedCollectionFactory::create([
834843
'collection_id' => $collection->id,
835844
]);
845+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
836846
$hash = \SpiderBits\Cache::hash($feed_url);
837847
$raw_response = <<<XML
838848
HTTP/2 200 OK
@@ -893,6 +903,7 @@ public function testPerformTakesEntriesIfOlderThanKeepPeriodUntilMinimum(): void
893903
FollowedCollectionFactory::create([
894904
'collection_id' => $collection->id,
895905
]);
906+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
896907
$hash = \SpiderBits\Cache::hash($feed_url);
897908
$raw_response = <<<XML
898909
HTTP/2 200 OK
@@ -963,6 +974,7 @@ public function testPerformForcesEntryIdIfMissing(): void
963974
'collection_id' => $collection->id,
964975
'user_id' => $user->id,
965976
]);
977+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
966978
$hash = \SpiderBits\Cache::hash($feed_url);
967979
$raw_response = <<<XML
968980
HTTP/2 200 OK
@@ -1027,6 +1039,7 @@ public function testPerformUpdatesUrlIfEntryIdIsIdentical(): void
10271039
'collection_id' => $collection->id,
10281040
'user_id' => $user->id,
10291041
]);
1042+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
10301043
$hash = \SpiderBits\Cache::hash($feed_url);
10311044
$raw_response = <<<XML
10321045
HTTP/2 200 OK
@@ -1080,6 +1093,7 @@ public function testPerformAbsolutizesLinks(): void
10801093
'collection_id' => $collection->id,
10811094
'user_id' => $user->id,
10821095
]);
1096+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
10831097
$hash = \SpiderBits\Cache::hash($feed_url);
10841098
$raw_response = <<<XML
10851099
HTTP/2 200 OK
@@ -1189,6 +1203,7 @@ public function testPerformHandlesLongMultiByteFeedTitle(): void
11891203
'collection_id' => $collection->id,
11901204
'user_id' => $user->id,
11911205
]);
1206+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
11921207
$hash = \SpiderBits\Cache::hash($feed_url);
11931208
$raw_response = <<<XML
11941209
HTTP/2 200 OK
@@ -1230,6 +1245,7 @@ public function testPerformSavesTheLinksUrlReplies(): void
12301245
'collection_id' => $collection->id,
12311246
'user_id' => $user->id,
12321247
]);
1248+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
12331249
$hash = \SpiderBits\Cache::hash($feed_url);
12341250
$raw_response = <<<XML
12351251
HTTP/2 200 OK
@@ -1360,6 +1376,7 @@ public function testPerformFetchesFeedIfLockedAfterAnHour(): void
13601376
'collection_id' => $collection->id,
13611377
'user_id' => $user->id,
13621378
]);
1379+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
13631380
$hash = \SpiderBits\Cache::hash($feed_url);
13641381
$raw_response = <<<XML
13651382
HTTP/2 200 OK
@@ -1404,7 +1421,7 @@ public function testPerformHandlesWrongEncoding(): void
14041421
<rss version="2.0">
14051422
<channel>
14061423
<title>My feed with an àccent</title>
1407-
<link>https://example.com</link>
1424+
<link>https://flus.fr/carnet</link>
14081425
</channel>
14091426
</rss>
14101427
XML;
@@ -1425,6 +1442,7 @@ public function testPerformHandlesWrongEncoding(): void
14251442
'collection_id' => $collection->id,
14261443
'user_id' => $user->id,
14271444
]);
1445+
$this->mockHttpWithFixture('https://flus.fr/carnet/', 'responses/flus.fr_carnet_index.html');
14281446
$hash = \SpiderBits\Cache::hash($feed_url);
14291447
$raw_response = <<<XML
14301448
HTTP/2 200 OK

0 commit comments

Comments
 (0)