Skip to content

Commit

Permalink
Added test for sequential collection
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianschwarz committed Apr 23, 2023
1 parent 4fec6a7 commit dec2393
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .idea/deploymentchecks.base.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/phpunit.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ It's very easy to run multiple checks:
```php
<?php declare(strict_types=1);

use de\codenamephp\deploymentchecks\base\Check\Collection\SequentialCollection;
use de\codenamephp\deploymentchecks\base\Check\Collection\SequentialCheckCollection;
use de\codenamephp\deploymentchecks\base\Check\Result\WithExitCodeInterface;
use de\codenamephp\deploymentchecks\base\ExitCode\DefaultExitCodes;
use de\codenamephp\deploymentchecks\http\Check\HttpCheck;
Expand All @@ -74,7 +74,7 @@ use GuzzleHttp\Psr7\Request;

require_once __DIR__ . '/../vendor/autoload.php';

$result = (new SequentialCollection(
$result = (new SequentialCheckCollection(
new HttpCheck(
new Request('GET', 'https://localhost'),
'Frontpage should be available',
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
"composer-require-checker": "Checks for missing required composer packages",
"infection": "Creates mutation tests to discover missing test coverage",
"ci-all": "Runs all ci tools in sequence"
},
"require-dev": {
"mockery/mockery": "^1.5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

/**
* Collects multiple checks and executes them sequentially and adds their results to a result collection
*
* @psalm-api
*/
final class SequentialCollection implements CheckInterface {
final class SequentialCheckCollection implements CheckInterface {

public readonly array $checks;

Expand Down
67 changes: 67 additions & 0 deletions test/Unit/Check/Collection/SequentialCheckCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);
/*
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace de\codenamephp\deploymentchecks\base\test\Unit\Check\Collection;

use de\codenamephp\deploymentchecks\base\Check\CheckInterface;
use de\codenamephp\deploymentchecks\base\Check\Collection\SequentialCheckCollection;
use de\codenamephp\deploymentchecks\base\Check\Result\Collection\ResultCollection;
use de\codenamephp\deploymentchecks\base\Check\Result\Collection\ResultCollectionInterface;
use de\codenamephp\deploymentchecks\base\Check\Result\ResultInterface;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;

final class SequentialCheckCollectionTest extends TestCase {

use MockeryPHPUnitIntegration;

public function test__construct() : void {
$check1 = $this->createMock(CheckInterface::class);
$check2 = $this->createMock(CheckInterface::class);
$check3 = $this->createMock(CheckInterface::class);

$collection = new SequentialCheckCollection($check1, $check2, $check3);

self::assertSame([$check1, $check2, $check3], $collection->checks);
self::assertInstanceOf(ResultCollection::class, $collection->resultCollection);
}

public function testRun() : void {
$result1 = $this->createMock(ResultInterface::class);
$check1 = $this->createMock(CheckInterface::class);
$check1->expects(self::once())->method('run')->willReturn($result1);

$result2 = $this->createMock(ResultInterface::class);
$check2 = $this->createMock(CheckInterface::class);
$check2->expects(self::once())->method('run')->willReturn($result2);

$result3 = $this->createMock(ResultInterface::class);
$check3 = $this->createMock(CheckInterface::class);
$check3->expects(self::once())->method('run')->willReturn($result3);

$resultCollection = Mockery::mock(ResultCollectionInterface::class);
$resultCollection->expects('add')->once()->with($result1);
$resultCollection->expects('add')->once()->with($result2);
$resultCollection->expects('add')->once()->with($result3);

$collection = new SequentialCheckCollection($check1, $check2, $check3);
$collection->resultCollection = $resultCollection;

self::assertSame($resultCollection, $collection->run());
}
}

0 comments on commit dec2393

Please sign in to comment.