Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
"laravel/pao": "^1.1",
"mockery/mockery": "^1.6",
"pestphp/pest": "^5.3",
"pestphp/pest-plugin-phpstan": "^5.2",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan": "^2.3",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-mockery": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpstan/phpstan-symfony": "^2.0"
},
Expand Down
142 changes: 141 additions & 1 deletion composer.lock

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

1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ parameters:
paths:
- bin
- src
- tests
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
use RuntimeException;

abstract class TestCase extends BaseTestCase
{
protected const string DATA_DIR = __DIR__ . '/data';

protected const string ALL_VERSIONS_FILE = __DIR__ . '/../data/all-versions.json';

/** @var string[] */
protected const array RELEASES_JSONS = [
self::DATA_DIR . '/versions/v5.json',
self::DATA_DIR . '/versions/v7.json',
Expand All @@ -24,12 +26,23 @@ abstract class TestCase extends BaseTestCase
protected function allVersions(): array
{
static $allVersions = null;

if ($allVersions === null) {
$content = file_get_contents(self::ALL_VERSIONS_FILE);

if (!is_string($content)) {
$message = sprintf('Failed to read fixture file content at path "%s".', self::ALL_VERSIONS_FILE);
throw new RuntimeException($message);
}

$allVersions = json_decode($content, true, 512, JSON_THROW_ON_ERROR);

if (!is_array($allVersions) || !array_all($allVersions, static fn(mixed $v) => is_string($v))) {
throw new RuntimeException('Failed to decode fixture into string[].');
}
}

/** @var string[] $allVersions */
return $allVersions;
}
}
56 changes: 26 additions & 30 deletions tests/Unit/ComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

covers(Composer::class);

describe(Composer::class, static function (): void {
describe(Composer::class, function (): void {
beforeEach(function () {
$this->tempFile = tempnam(sys_get_temp_dir(), 'composer_');
});
Expand All @@ -20,14 +20,12 @@
unlink($this->tempFile);
});

describe('::fromFile()', static function (): void {
describe('::fromFile()', function (): void {
it('reads valid file', function () {
file_put_contents($this->tempFile, '{"require":{"php":"^8.1"}}');

$composer = Composer::fromFile($this->tempFile);

expect($composer)->toBeInstanceOf(Composer::class);
});
})->throwsNoExceptions();

it('throws on unreadable file', function () {
$path = '/nonexistent/path/composer.json';
Expand All @@ -42,7 +40,7 @@
})->throws(JsonException::class);
});

describe('::requiredPhpConstraint()', static function (): void {
describe('::requiredPhpConstraint()', function (): void {
it('reads valid file', function (string $content) {
file_put_contents($this->tempFile, $content);

Expand All @@ -60,29 +58,27 @@

$composer = Composer::fromFile($this->tempFile);

$composer->requiredPhpConstraint();
})
->throws(UnexpectedValueException::class)
->with([
// No "require.php" is set.
'{"require":{"some/package":"^1.0"}}',
'{"require":{}}',
'{"require":123}',
'{"require-dev":{"php":"^1.0"}}',
'{"php":"^1.0"}',
'{}',
// "require.php" is not a string.
'{"require":{"php":null}}',
'{"require":{"php":{}}}',
'{"require":{"php":[]}}',
'{"require":{"php":123}}',
'{"require":{"php":12.3}}',
'{"require":{"php":true}}',
'{"require":{"php":false}}',
// "require.php" is not a valid constraint.
'{"require":{"php":"invalid constraint"}}',
'{"require":{"php":"~>1.0"}}',
'{"require":{"php":""}}',
]);
expect($composer->requiredPhpConstraint(...))->toThrow(UnexpectedValueException::class);
})->with([
// No "require.php" is set.
'{"require":{"some/package":"^1.0"}}',
'{"require":{}}',
'{"require":123}',
'{"require-dev":{"php":"^1.0"}}',
'{"php":"^1.0"}',
'{}',
// "require.php" is not a string.
'{"require":{"php":null}}',
'{"require":{"php":{}}}',
'{"require":{"php":[]}}',
'{"require":{"php":123}}',
'{"require":{"php":12.3}}',
'{"require":{"php":true}}',
'{"require":{"php":false}}',
// "require.php" is not a valid constraint.
'{"require":{"php":"invalid constraint"}}',
'{"require":{"php":"~>1.0"}}',
'{"require":{"php":""}}',
]);
});
});
19 changes: 5 additions & 14 deletions tests/Unit/Matrices/MatrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@
use Mockery;
use TypistTech\PhpMatrix\Exceptions\UnexpectedValueException;
use TypistTech\PhpMatrix\Matrices\Matrix;
use TypistTech\PhpMatrix\Matrices\MatrixInterface;
use TypistTech\PhpMatrix\Releases\ReleasesInterface;

covers(Matrix::class);

describe(Matrix::class, static function (): void {
it('implements ReleasesInterface', function () {
$releases = Mockery::mock(ReleasesInterface::class);

$matrix = new Matrix($releases);

expect($matrix)->toBeInstanceOf(MatrixInterface::class);
});

describe('::satisfiedBy()', static function (): void {
dataset('satisfied_by', static function (): array {
describe(Matrix::class, function (): void {
describe('::satisfiedBy()', function (): void {
dataset('satisfied_by', function (): array {
$releases = [
'1.0.0',
'2.0.1',
Expand Down Expand Up @@ -75,8 +66,8 @@

$matrix = new Matrix($releases);

$matrix->satisfiedBy('invalid constraint');
})->throws(UnexpectedValueException::class);
expect(fn() => $matrix->satisfiedBy('invalid constraint'))->toThrow(UnexpectedValueException::class);
});

it('does not invoke ReleasesInterface::all() when invalid constraint', function () {
$releases = Mockery::mock(ReleasesInterface::class);
Expand Down
15 changes: 3 additions & 12 deletions tests/Unit/Matrices/MinorOnlyMatrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,14 @@
namespace Tests\Unit\Matrices;

use Mockery;
use TypistTech\PhpMatrix\Matrices\MatrixInterface;
use TypistTech\PhpMatrix\Matrices\MinorOnlyMatrix;
use TypistTech\PhpMatrix\Releases\ReleasesInterface;

covers(MinorOnlyMatrix::class);

describe(MinorOnlyMatrix::class, static function (): void {
it('implements ReleasesInterface', function () {
$releases = Mockery::mock(ReleasesInterface::class);

$matrix = new MinorOnlyMatrix($releases);

expect($matrix)->toBeInstanceOf(MatrixInterface::class);
});

describe('::satisfiedBy()', static function (): void {
dataset('satisfied_by', static function (): array {
describe(MinorOnlyMatrix::class, function (): void {
describe('::satisfiedBy()', function (): void {
dataset('satisfied_by', function (): array {
$releases = [
'1.0.0',
'2.0.1',
Expand Down
Loading
Loading