Skip to content

Commit 70fa805

Browse files
Added SequentialCollection and updated readme
1 parent c5a2183 commit 70fa805

2 files changed

Lines changed: 107 additions & 5 deletions

File tree

README.md

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,67 @@ code and leaves the rest to the build system that was running the checks:
3737
```php
3838
<?php declare(strict_types=1);
3939

40+
use de\codenamephp\deploymentchecks\base\Check\Result\WithExitCodeInterface;
41+
use de\codenamephp\deploymentchecks\base\ExitCode\DefaultExitCodes;
42+
use de\codenamephp\deploymentchecks\http\Check\HttpCheck;
43+
use de\codenamephp\deploymentchecks\http\Check\Test\StatusCode;
44+
use GuzzleHttp\Psr7\Request;
45+
46+
require_once __DIR__ . '/../vendor/autoload.php';
47+
48+
$result = (new HttpCheck(
49+
new Request('GET', 'https://localhost/'),
50+
'Frontpage should be available',
51+
new StatusCode(200),
52+
))->run();
53+
54+
exit($result instanceof WithExitCodeInterface ? $result->exitCode() : ($result->successful() ? DefaultExitCodes::SUCCESSFUL->value : DefaultExitCodes::ERROR->value));
55+
```
56+
57+
This example uses the http package to perform http requests and run tests on them. It creates a single check with one test,
58+
runs it and checks the result. If it is a result with an exit code, the exit code is used. If not, the result is checked for success and
59+
the default exit codes are used.
60+
61+
Since most CI/CD systems will run the script and check the exit code, this should already be enough to get the system to fail if one of the checks fail.
62+
63+
It's very easy to run multiple checks:
64+
65+
```php
66+
<?php declare(strict_types=1);
67+
68+
use \de\codenamephp\deploymentchecks\base\Check\Collection\SequentialCollection;
69+
use de\codenamephp\deploymentchecks\base\Check\Result\WithExitCodeInterface;
70+
use de\codenamephp\deploymentchecks\base\ExitCode\DefaultExitCodes;
71+
use de\codenamephp\deploymentchecks\http\Check\HttpCheck;
72+
use de\codenamephp\deploymentchecks\http\Check\Test\StatusCode;
73+
use GuzzleHttp\Psr7\Request;
74+
75+
require_once __DIR__ . '/../vendor/autoload.php';
76+
77+
$result = (new SequentialCollection(
78+
new HttpCheck(
79+
new Request('GET', 'https://localhost'),
80+
'Frontpage should be available',
81+
new StatusCode(200),
82+
),
83+
new HttpCheck(
84+
new Request('GET', 'https://localhost/admin'),
85+
'Admin login page should be available',
86+
new StatusCode(401),
87+
)
88+
))->run();
89+
90+
exit($result instanceof WithExitCodeInterface ? $result->exitCode() : ($result->successful() ? DefaultExitCodes::SUCCESSFUL->value : DefaultExitCodes::ERROR->value));
91+
```
92+
93+
Not much has changed. The only difference is that the checks are now wrapped in a collection that will run them sequentially. If one fails, the result will be
94+
unsuccessful.
95+
96+
It's also very easy to run checks in parallel. The next example uses the async package to run the checks in parallel:
97+
98+
```php
99+
<?php declare(strict_types=1);
100+
40101
use de\codenamephp\deploymentchecks\async\Collection\AsyncCheckCollection;
41102
use de\codenamephp\deploymentchecks\base\Check\Result\WithExitCodeInterface;
42103
use de\codenamephp\deploymentchecks\base\ExitCode\DefaultExitCodes;
@@ -62,8 +123,4 @@ $result = (new AsyncCheckCollection(new \Spatie\Async\Pool(),
62123
exit($result instanceof WithExitCodeInterface ? $result->exitCode() : ($result->successful() ? DefaultExitCodes::SUCCESSFUL->value : DefaultExitCodes::ERROR->value));
63124
```
64125

65-
This example uses the http package to perform http requests and run tests on them together with the async package to the requests are tested in parallel.
66-
At the end the result is checked. If it is a result with an exit code, the exit code is used. If not, the result is checked for success and
67-
the default exit codes are used.
68-
69-
Since most CI/CD systems will run the script and check the exit code, this should already be enough to get the system to fail if one of the checks fail.
126+
Again, not much has changed. The only difference is that the checks are now wrapped in a collection that will run them in parallel.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\base\Check\Collection;
19+
20+
use de\codenamephp\deploymentchecks\base\Check\CheckInterface;
21+
use de\codenamephp\deploymentchecks\base\Check\Result\Collection\ResultCollection;
22+
use de\codenamephp\deploymentchecks\base\Check\Result\Collection\ResultCollectionInterface;
23+
use de\codenamephp\deploymentchecks\base\Check\Result\ResultInterface;
24+
25+
/**
26+
* Collects multiple checks and executes them sequentially and adds their results to a result collection
27+
*/
28+
final class SequentialCollection implements CheckInterface {
29+
30+
public readonly array $checks;
31+
32+
public ResultCollectionInterface $resultCollection;
33+
34+
public function __construct(CheckInterface ...$checks) {
35+
$this->checks = $checks;
36+
$this->resultCollection = new ResultCollection();
37+
}
38+
39+
public function run() : ResultInterface {
40+
foreach($this->checks as $check) {
41+
$this->resultCollection->add($check->run());
42+
}
43+
return $this->resultCollection;
44+
}
45+
}

0 commit comments

Comments
 (0)