Skip to content

Commit c7e1397

Browse files
committed
fix tests and introduce WarmUpService
1 parent b9c6d1c commit c7e1397

File tree

7 files changed

+68
-15
lines changed

7 files changed

+68
-15
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"autoload": {
3333
"psr-4": {
3434
"ApacheBorys\\Retry\\BasicTransport\\": "src/BasicTransport",
35-
"ApacheBorys\\Retry\\BasicExecutor\\": "src/BasicExecutor"
35+
"ApacheBorys\\Retry\\BasicExecutor\\": "src/BasicExecutor",
36+
"ApacheBorys\\Retry\\Common\\": "src/Common"
3637
}
3738
},
3839
"autoload-dev": {

src/BasicTransport/Migration/DbPdoTransportMigration.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function version(): int
115115
return 1;
116116
}
117117

118-
public function support(): array
118+
public static function support(): array
119119
{
120120
return [DbPdoTransport::class];
121121
}
@@ -127,9 +127,14 @@ public function wasExecuted(): bool
127127
$this->compileDbAndTableName($this->migrationTableName),
128128
$this->version()
129129
);
130-
$res = $this->pdo->query($sql);
131130

132-
return $res->fetchColumn() != 0;
131+
try {
132+
$res = $this->pdo->query($sql);
133+
134+
return $res->fetchColumn() != 0;
135+
} catch (\PDOException $e) {
136+
return false;
137+
}
133138
}
134139

135140
private function compileDbAndTableName(string $tableName): string

src/BasicTransport/Migration/MigrationInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function rollback(): bool;
1212
public function version(): int;
1313

1414
/** @return string[] Transport classes what support current migration */
15-
public function support(): array;
15+
public static function support(): array;
1616

1717
public function wasExecuted(): bool;
1818
}

src/BasicTransport/Migration/Migrator.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ private function getAllMigrationsForTransport(array $context, string $transportC
2424
$migrations = [];
2525

2626
foreach (get_declared_classes() as $className) {
27-
if (in_array(MigrationInterface::class, class_implements($className))) {
27+
if (
28+
in_array(MigrationInterface::class, class_implements($className), true) &&
29+
in_array($transportClass, $className::support(), true)
30+
) {
2831
$migrations[] = $className;
2932
}
3033
}
@@ -37,13 +40,6 @@ static function (string $migration) use ($context) {
3740
$migrations
3841
);
3942

40-
$migrations = array_filter(
41-
$migrations,
42-
static function (MigrationInterface $migration) use ($transportClass) {
43-
return in_array($transportClass, $migration->support());
44-
}
45-
);
46-
4743
$result = [];
4844
foreach ($migrations as $migration) {
4945
$result[$migration->version()] = $migration;

src/BasicTransport/Migration/MongoDbTransportMigration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function version(): int
6767
return 1;
6868
}
6969

70-
public function support(): array
70+
public static function support(): array
7171
{
7272
return [MongoDbTransport::class];
7373
}

src/Common/WarmUpService.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ApacheBorys\Retry\Common;
5+
6+
class WarmUpService
7+
{
8+
public const FILES_FOR_IGNORE = [
9+
__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'BasicExecutor' . DIRECTORY_SEPARATOR . 'test.php',
10+
];
11+
12+
public function registerAllClasses(): void
13+
{
14+
$srcLocation = __DIR__ . DIRECTORY_SEPARATOR . '..';
15+
$testsLocation = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tests';
16+
17+
$filesForIgnore = array_map(static function (string $path) { return realpath($path); }, self::FILES_FOR_IGNORE);
18+
19+
$this->loadDir($srcLocation, $filesForIgnore);
20+
$this->loadDir($testsLocation, $filesForIgnore);
21+
}
22+
23+
/**
24+
* @param string[] $filesForIgnore
25+
*/
26+
private function loadDir(string $loc, array $filesForIgnore): void
27+
{
28+
foreach (scandir($loc) as $item) {
29+
if ($item === '..' || $item === '.') {
30+
continue;
31+
}
32+
33+
$fullPath = $loc . DIRECTORY_SEPARATOR . $item;
34+
35+
if (is_dir($fullPath)) {
36+
$this->loadDir($fullPath, $filesForIgnore);
37+
}
38+
39+
if (in_array(realpath($fullPath), $filesForIgnore,true)) {
40+
continue;
41+
}
42+
43+
if (pathinfo($fullPath)['extension'] ?? '' === 'php') {
44+
require_once $fullPath;
45+
}
46+
}
47+
}
48+
}

tests/BasicTransport/TransportTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace ApacheBorys\Retry\BasicTransport\Tests;
55

6-
use ApacheBorys\Retry\BasicTransport\Migration\MigrationInterface;
6+
use ApacheBorys\Retry\Common\WarmUpService;
77
use ApacheBorys\Retry\Entity\Config;
88
use ApacheBorys\Retry\Entity\Message;
99
use ApacheBorys\Retry\Interfaces\Transport;
@@ -31,6 +31,9 @@ public static function setUpBeforeClass(): void
3131
{
3232
parent::setUpBeforeClass();
3333

34+
$warmUp = new WarmUpService();
35+
$warmUp->registerAllClasses();
36+
3437
foreach (self::getTestsForTransports() as $transport) {
3538
$transport->setUpBeforeClass();
3639
}

0 commit comments

Comments
 (0)