-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update tests and start testing code paths
- Loading branch information
Showing
29 changed files
with
318 additions
and
259 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
coverage.txt | ||
test-coverage | ||
.DS_Store | ||
coverage.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Smolblog\Test\Constraints; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use PHPUnit\Framework\InvalidArgumentException; | ||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
use Smolblog\Framework\Messages\Event; | ||
|
||
class EventIsEquivalent extends Constraint { | ||
public function __construct(private Event $expected) {} | ||
|
||
public function toString(): string { return 'two Events are equivalent'; } | ||
protected function failureDescription($other): string { return $this->toString(); } | ||
|
||
protected function matches($other): bool { | ||
if (!is_a($other, Event::class)) { | ||
throw new InvalidArgumentException('Object is not an Event.'); | ||
} | ||
|
||
$expectedData = $this->expected->toArray(); | ||
unset($expectedData['id']); | ||
unset($expectedData['timestamp']); | ||
|
||
$actualData = $other->toArray(); | ||
unset($actualData['id']); | ||
unset($actualData['timestamp']); | ||
|
||
return $expectedData == $actualData; | ||
} | ||
|
||
protected function fail($other, $description, ?ComparisonFailure $comparisonFailure = null): void | ||
{ | ||
if ($comparisonFailure === null) { | ||
$expectedData = $this->expected->toArray(); | ||
unset($expectedData['id']); | ||
unset($expectedData['timestamp']); | ||
|
||
$actualData = $other->toArray(); | ||
unset($actualData['id']); | ||
unset($actualData['timestamp']); | ||
|
||
$comparisonFailure = new ComparisonFailure( | ||
$this->expected, | ||
$other, | ||
$this->exporter()->export($expectedData), | ||
$this->exporter()->export($actualData), | ||
false, | ||
'Failed asserting that two Events are equivalent.' | ||
); | ||
} | ||
|
||
parent::fail($other, $description, $comparisonFailure); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Smolblog\Test\Constraints; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use PHPUnit\Framework\InvalidArgumentException; | ||
use Psr\Http\Message\MessageInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
|
||
class HttpMessageIsEquivalent extends Constraint { | ||
public function __construct(private RequestInterface|ResponseInterface $expected) {} | ||
|
||
public function toString(): string { return 'two HTTP messages are equivalent'; } | ||
protected function failureDescription($other): string { return $this->toString(); } | ||
|
||
private function makeArray(RequestInterface|ResponseInterface $message): array { | ||
$data = ['type' => '']; | ||
|
||
foreach($message->getHeaders() as $key => $value) { | ||
$lowerKey = strtolower($key); | ||
$data[$lowerKey] = $value; | ||
} | ||
|
||
$bodyString = $message->getBody()->__toString(); | ||
$data['body'] = empty($bodyString) ? null : $bodyString; | ||
|
||
if (is_a($message, RequestInterface::class)) { | ||
$data['type'] .= 'Request'; | ||
$data['url'] = $message->getUri()->__toString(); | ||
$data['method'] = $message->getMethod(); | ||
} | ||
|
||
if (is_a($message, ResponseInterface::class)) { | ||
$data['type'] .= 'Response'; | ||
$data['code'] = $message->getStatusCode(); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
protected function matches($other): bool { | ||
if (!is_a($other, MessageInterface::class)) { | ||
throw new InvalidArgumentException('Object is not an HTTP Message.'); | ||
} | ||
|
||
$expectedData = $this->makeArray($this->expected); | ||
$actualData = $this->makeArray($other); | ||
|
||
return $expectedData == $actualData; | ||
} | ||
|
||
protected function fail($other, $description, ?ComparisonFailure $comparisonFailure = null): void | ||
{ | ||
if ($comparisonFailure === null) { | ||
$expectedData = $this->makeArray($this->expected); | ||
$actualData = $this->makeArray($other); | ||
|
||
$comparisonFailure = new ComparisonFailure( | ||
$this->expected, | ||
$other, | ||
$this->exporter()->export($expectedData), | ||
$this->exporter()->export($actualData), | ||
false, | ||
'Failed asserting that two HTTP messages are equivalent.' | ||
); | ||
} | ||
|
||
parent::fail($other, $description, $comparisonFailure); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Smolblog\Test; | ||
|
||
use SebastianBergmann\CodeCoverage\Node\Directory; | ||
use SebastianBergmann\CodeCoverage\Node\File; | ||
|
||
class CoverageReport { | ||
public static function report() { | ||
$report = require __DIR__ . '/../coverage.php'; | ||
$baseDirCharCount = strlen(dirname(__DIR__) . '/src/'); | ||
|
||
$score = $report->getReport()->numberOfExecutedPaths() / $report->getReport()->numberOfExecutablePaths(); | ||
|
||
echo 'Total coverage: ' . floor($score * 100) . "%\n"; | ||
|
||
if ($score >= 1) { | ||
echo "PASS\n\n"; | ||
return; | ||
} | ||
|
||
echo "FAIL\n\nThe following files have incomplete code coverage:\n"; | ||
foreach(self::getProblemFiles($report->getReport()) as $file) { | ||
echo ' ' . substr($file->pathAsString(), $baseDirCharCount) . "\n"; | ||
} | ||
|
||
echo "\nRun `composer test-coverage` and view the HTML report in the test-coverage folder.\n\n"; | ||
|
||
exit(1); | ||
} | ||
|
||
private static function getProblemFiles(Directory $dir): array { | ||
return array_reduce( | ||
$dir->directories(), | ||
fn($all, $subDir) => array_merge($all, self::getProblemFiles($subDir)), | ||
array_filter( | ||
$dir->files(), | ||
fn($file) => ($file->numberOfExecutablePaths() - $file->numberOfExecutedPaths()) > 0 | ||
), | ||
); | ||
} | ||
|
||
private static function getFileMessage(File $file): string { | ||
$output = $file->pathAsString() . ":\n"; | ||
|
||
foreach (array_filter($file->functions(), fn($fn) => $fn['coverage'] < 100) as $func) { | ||
$output .= ' Function ' . $func['functionName'] . ': ' . ($func['executablePaths'] - $func['executedPaths']) . "\n"; | ||
} | ||
foreach (array_filter($file->traits(), fn($fn) => $fn['coverage'] < 100) as $trait) { | ||
$output .= ' Trait ' . $trait['traitName'] . ":\n" . self::getMethodMessages($trait['methods']); | ||
} | ||
foreach (array_filter($file->classes(), fn($fn) => $fn['coverage'] < 100) as $class) { | ||
$output .= ' Class ' . $class['className'] . ":\n" . self::getMethodMessages($class['methods']); | ||
} | ||
|
||
$output .= "\n"; | ||
return $output; | ||
} | ||
|
||
private static function getMethodMessages(array $methods): string { | ||
$output = ''; | ||
foreach (array_filter($methods, fn($mt) => $mt['coverage'] < 100) as $method) { | ||
$output .= ' ' . $method['methodName'] . ': ' . ($method['executablePaths'] - $method['executedPaths']) . "\n"; | ||
} | ||
|
||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Smolblog\Test\Kits; | ||
|
||
trait ActivityPubActivityTestKit { | ||
use SerializableTestKit; | ||
|
||
public function testItGivesTheCorrectType() { | ||
$this->assertEquals($this->subject->type(), static::EXPECTED_TYPE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Smolblog\Test\Kits; | ||
|
||
use Smolblog\Framework\Objects\Identifier; | ||
|
||
trait DateIdentifierTestKit { | ||
/** | ||
* Asserts that two identifiers are created from the same date. A v7 UUID hashes the date, then adds random bytes. | ||
* This function trims the random bytes and compares the remaining data. | ||
*/ | ||
private function assertIdentifiersHaveSameDate(Identifier $expected, Identifier $actual) { | ||
$expectedTrim = substr(strval($expected), offset: 0, length: -8); | ||
$actualTrim = substr(strval($actual), offset: 0, length: -8); | ||
|
||
$this->assertEquals($expectedTrim, $actualTrim); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Smolblog\Test\Kits; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use Smolblog\Framework\Messages\Event; | ||
use Smolblog\Test\Constraints\EventIsEquivalent; | ||
|
||
trait EventComparisonTestKit { | ||
private function eventEquivalentTo(Event $expected): Constraint { | ||
return new EventIsEquivalent($expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Smolblog\Test\Kits; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Smolblog\Test\Constraints\HttpMessageIsEquivalent; | ||
|
||
trait HttpMessageComparisonTestKit { | ||
private function httpMessageEqualTo(RequestInterface|ResponseInterface $expected): Constraint { | ||
return new HttpMessageIsEquivalent($expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Smolblog\Test\Kits; | ||
|
||
trait SerializableTestKit { | ||
public function testItWillSerializeToArrayAndDeserializeToItself() { | ||
$class = get_class($this->subject); | ||
$this->assertEquals($this->subject, $class::fromArray($this->subject->toArray())); | ||
} | ||
|
||
public function testItWillSerializeToJsonAndDeserializeToItself() { | ||
$class = get_class($this->subject); | ||
$this->assertEquals($this->subject, $class::jsonDeserialize(json_encode($this->subject))); | ||
} | ||
} |
Oops, something went wrong.