Skip to content

Commit

Permalink
refactor: Migrate tests to Pest
Browse files Browse the repository at this point in the history
  • Loading branch information
dshafik committed Aug 30, 2024
1 parent 5689cad commit 04fa2a7
Show file tree
Hide file tree
Showing 79 changed files with 3,538 additions and 4,421 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/sonarcloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ jobs:
- name: Install dependencies with composer
run: composer update --no-ansi --no-interaction --no-progress

- name: Run tests with phpunit/phpunit
- name: Run tests with pest
run: >
vendor/bin/phpunit --coverage-clover=coverage.xml
vendor/bin/pest --coverage-clover coverage.xml
- name: Run PHPStan
run: vendor/bin/phpstan analyse --memory-limit=-1 --error-format=json > phpstan.json
Expand Down
26 changes: 4 additions & 22 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,8 @@ jobs:
- name: Install dependencies with composer
run: composer update --no-ansi --no-interaction --no-progress

- name: Run tests with phpunit/phpunit
run: vendor/bin/phpunit --coverage-clover=coverage.xml
mutation:
name: Mutation Testing
runs-on: 'ubuntu-latest'
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup PHP with Xdebug
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
coverage: xdebug
extensions: mbstring, intl, fileinfo, sqlite3, pdo_sqlite

- name: Install dependencies with composer
run: composer update --no-ansi --no-interaction --no-progress
- name: Run tests with pest
run: vendor/bin/pest --coverage --min=80

- name: Run mutation tests
run: vendor/bin/infection --min-covered-msi=80
- name: Run type coverage with pest
run: vendor/bin/pest --type-coverage --min=99 --memory-limit=-1
2 changes: 1 addition & 1 deletion captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
]
},
{
"action": "vendor/bin/phpunit --testdox",
"action": "vendor/bin/pest --coverage --min=80",
"conditions": [
{
"exec": "\\CaptainHook\\App\\Hook\\Condition\\FileStaged\\OfType",
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"require-dev": {
"captainhook/captainhook-phar": "^5.23",
"captainhook/hook-installer": "^1.0",
"fakerphp/faker": "^1.23",
"infection/infection": "^0.28.1",
"larastan/larastan": "^2.0",
"laravel/pint": "^1.15",
"laravel/prompts": "^0.1.25",
"orchestra/testbench": "^8.0|^9.0",
"phpunit/phpunit": "^10.5|^11",
"orchestra/pest-plugin-testbench": "^2.0",
"pestphp/pest-plugin-drift": "^2.6",
"pestphp/pest-plugin-faker": "^2.0",
"pestphp/pest-plugin-type-coverage": "^2.8",
"ramsey/conventional-commits": "dev-allow-sf-7",
"roave/security-advisories": "dev-latest",
"symfony/var-dumper": "*"
Expand Down Expand Up @@ -67,7 +67,8 @@
"allow-plugins": {
"captainhook/captainhook-phar": true,
"captainhook/hook-installer": true,
"infection/extension-installer": true
"infection/extension-installer": true,
"pestphp/pest-plugin": true
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="random"
requireCoverageMetadata="true"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="false"
beStrictAboutOutputDuringTests="true"
failOnRisky="false"
Expand Down
2 changes: 1 addition & 1 deletion src/Bag/BagServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class BagServiceProvider extends ServiceProvider
{
public function register()
public function register(): void
{
$this->app->beforeResolving(Bag::class, function (string $class, array $parameters, Application $app) {
if ($this->app->has($class)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Bag/Casts/CollectionOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public function set(string $propertyType, string $propertyName, LaravelCollectio
throw new InvalidCollection(sprintf('The property "%s" must be a subclass of %s', $propertyName, LaravelCollection::class));
}

return $propertyType::make($properties->get($propertyName))->map(fn ($item) => $this->valueClassname::from($item));
return $propertyType::make($properties->get($propertyName))->map(fn (mixed $item) => $this->valueClassname::from($item));
}
}
84 changes: 69 additions & 15 deletions src/Bag/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@

class Collection extends LaravelCollection
{
/**
* \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TKey>|TKey $keys
*/
#[Override]
public function forget($keys)
public function forget($keys): static
{
// @phpstan-ignore-next-line return.type
return (clone $this)->forgetReal($keys);
}

protected function forgetReal($keys)
/**
* \Illuminate\Contracts\Support\Arrayable<array-key, TValue>|iterable<array-key, TKey>|TKey $keys
*/
protected function forgetReal($keys): static
{
foreach ($this->getArrayableItems($keys) as $key) {
parent::offsetUnset($key);
Expand All @@ -28,94 +35,141 @@ protected function forgetReal($keys)
return $this;
}

/**
* @param int $count
*/
#[Override]
public function pop($count = 1)
public function pop($count = 1): mixed
{
return (clone $this)->popReal($count);
}

protected function popReal($count)
protected function popReal(int $count): mixed
{
return parent::pop($count);
}

/**
* @param array-key $key
*/
#[Override]
public function prepend($value, $key = null)
public function prepend($value, $key = null): self
{
// @phpstan-ignore-next-line return.type
return (clone $this)->prependReal($value, $key);
}

protected function prependReal($value, $key)
/**
* @param array-key $key
*/
protected function prependReal(mixed $value, string|int $key): static
{
return parent::prepend($value, $key);
}

/**
* @param array-key $key
*/
#[Override]
public function pull($key, $default = null)
public function pull($key, $default = null): static
{
throw new ImmutableCollectionException('Method pull is not allowed on ' . static::class);
}

/**
*/
#[Override]
public function push(...$values)
public function push(...$values): static
{
// @phpstan-ignore-next-line return.type
return (clone $this)->pushReal(...$values);
}

protected function pushReal(... $values)
protected function pushReal(mixed ... $values): static
{
return parent::push(...$values);
}

/**
* @param array-key $key
*/
#[Override]
public function put($key, $value)
public function put($key, $value): static
{
// @phpstan-ignore-next-line return.type
return (clone $this)->putReal($key, $value);
}

protected function putReal($key, $value)
/**
* @param array-key $key
*/
protected function putReal(string|int $key, mixed $value): static
{
parent::offsetSet($key, $value);

return $this;
}

/**
* @param int $count
*/
#[Override]
public function shift($count = 1)
public function shift($count = 1): void
{
throw new ImmutableCollectionException('Method shift is not allowed on ' . static::class);
}

/**
* @param int $offset
* @param int $length
* @param array $replacement
* @phpstan-ignore method.childReturnType
*/
#[Override]
public function splice($offset, $length = null, $replacement = [])
public function splice($offset, $length = null, $replacement = []): void
{
throw new ImmutableCollectionException('Method splice is not allowed on ' . static::class);
}

/**
* @phpstan-ignore method.childReturnType
*/
#[Override]
public function transform(callable $callback)
public function transform(callable $callback): void
{
throw new ImmutableCollectionException('Method transform is not allowed on ' . static::class);
}

/**
* @param array-key $key
*/
#[Override]
public function getOrPut($key, $value)
public function getOrPut($key, $value): void
{
throw new ImmutableCollectionException('Method getOrPut is not allowed on ' . static::class);
}

/**
* @param array-key $key
*/
#[Override]
public function offsetSet($key, $value): void
{
throw new ImmutableCollectionException('Array key writes not allowed on ' . static::class);
}

/**
* @param array-key $key
*/
#[Override]
public function offsetUnset($key): void
{
throw new ImmutableCollectionException('Array key writes not allowed on ' . static::class);
}

/**
* @param string $name
*/
public function __set($name, $value): void
{
throw new ImmutableCollectionException('Property writes not allowed on ' . static::class);
Expand Down
4 changes: 2 additions & 2 deletions src/Bag/Console/Commands/MakeBagCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected function createBag(string $namespace, string $name, string $directory,
return true;
}

protected function updateBag(string $namespace, string $name, string $classFile, ?string $factory, ?string $collection)
protected function updateBag(string $namespace, string $name, string $classFile, ?string $factory, ?string $collection): void
{
require_once $classFile;

Expand Down Expand Up @@ -374,7 +374,7 @@ protected function createCollection(string $namespace, string $name, string $dir
return true;
}

protected function ensureDirectory(string $directory)
protected function ensureDirectory(string $directory): void
{
if (!\file_exists($directory)) {
mkdir($directory, recursive: true);
Expand Down
8 changes: 4 additions & 4 deletions src/Bag/Internal/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ class Cache

public static array $cacheArray = [];

public static function fake()
public static function fake(): MockInterface
{
self::$instance = Mockery::mock(static::class);

return self::$instance;
}

public static function spy()
public static function spy(): MockInterface
{
self::$instance = Mockery::spy(static::class);

return self::$instance;
}

public static function reset()
public static function reset(): void
{
self::clear();
self::$instance = null;
Expand Down Expand Up @@ -81,7 +81,7 @@ public function store(string $store, object|string $key, callable $callback): mi
return self::$cacheArray[$store][$key];
}

protected static function clear()
protected static function clear(): void
{
self::$cacheArray = self::$cacheWeakMap = [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bag/Mappers/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __invoke(string $inputName): string
$outputName = Str::of($inputName);
foreach ($this->stringOperations as $stringOperation) {
$stringOperation = Str::of($stringOperation)->explode(':');
$args = $stringOperation->map(fn ($op) => Str::of($op)->explode(',')->toArray())->flatten()->skip(1)->toArray();
$args = $stringOperation->map(fn (string $op) => Str::of($op)->explode(',')->toArray())->flatten()->skip(1)->toArray();
$outputName = $outputName->{$stringOperation->first()}(...$args);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Bag/Pipelines/Pipes/CastInputValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Bag\Pipelines\Values\BagInput;
use Bag\Property\Value;
use Bag\Property\ValueCollection;
use Illuminate\Support\Collection;

readonly class CastInputValues
{
Expand All @@ -15,7 +16,7 @@ public function __invoke(BagInput $input)
$properties = $input->params;
$values = $input->values;

$input->values = $input->values->map(function ($value, $key) use ($properties, $values) {
$input->values = $input->values->map(function (mixed $value, string $key) use ($properties, $values) {
if (!isset($properties[$key])) {
return $this->castVariadic($properties, $values, $value);
}
Expand All @@ -29,7 +30,7 @@ public function __invoke(BagInput $input)
return $input;
}

protected function castVariadic(ValueCollection $properties, $values, $value): mixed
protected function castVariadic(ValueCollection $properties, Collection $values, mixed $value): mixed
{
/** @var Value $last */
$last = $properties->last();
Expand Down
6 changes: 3 additions & 3 deletions src/Bag/Pipelines/Pipes/CastOutputValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __invoke(BagOutput $output)
$params = $output->params;
$values = $output->values;

$output->values = $output->values->map(function ($value, $key) use ($properties, $params, $values) {
$output->values = $output->values->map(function (mixed $value, string $key) use ($properties, $params, $values) {
if ($params[$key]->variadic) {
return $this->castVariadic($params, $values, $value);
}
Expand All @@ -31,12 +31,12 @@ public function __invoke(BagOutput $output)
return $output;
}

protected function castVariadic(ValueCollection $params, $values, $value): mixed
protected function castVariadic(ValueCollection $params, Collection $values, mixed $value): mixed
{
/** @var Value $last */
$last = $params->last();

return Collection::wrap($value)->map(function ($value) use ($last, $values) {
return Collection::wrap($value)->map(function (mixed $value) use ($last, $values) {
return ($last->outputCast)($values->put($last->name, $value));
})->toArray();
}
Expand Down
Loading

0 comments on commit 04fa2a7

Please sign in to comment.