Skip to content

Commit

Permalink
fix(Testing): Fix MakeExpectationCommand with union returns in assert…
Browse files Browse the repository at this point in the history
… class

- Add ablity to re-generate stub files for the expectation command tests
  • Loading branch information
pionl committed Jun 21, 2023
1 parent 9b37cc5 commit b0874e8
Show file tree
Hide file tree
Showing 12 changed files with 365 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .idea/runConfigurations/tests_stubs.xml

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

6 changes: 6 additions & 0 deletions CONTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ composer run check
2. `composer require wrkflow/larastrict:*`
4. (optional, IDEA) Mark `core/src` directory as `Sources root` and set `Larastrict` as the namespace.

## Stubfiles

For MakeExpectationCommand we are using stubs. If you want to generate the stub files from current command output then run:

- Composer: `composer test:stubs`
- PHPStorm run configuration: `test:stubs`

## Fixes

Expand Down
2 changes: 2 additions & 0 deletions src/Testing/Commands/MakeExpectationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ protected function generateExpectationMethodAssert(

if ($returnType instanceof ReflectionNamedType) {
$enumReturnType = PhpType::tryFrom($returnType->getName()) ?? PhpType::Mixed;
} elseif ($returnType instanceof ReflectionUnionType) {
$enumReturnType = PhpType::Mixed;
} else {
$enumReturnType = $phpDoc->returnType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use LaraStrict\Enums\EnvironmentType;
use LaraStrict\Testing\Laravel\TestingApplication;

class TestReturnUnionAction
class TestReturnUnionAction implements TestReturnUnionActionContract
{
public function execute(
int $int,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;

use ArrayAccess;
use Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants;
use const DIRECTORY_SEPARATOR;
use Illuminate\Support\Collection;
use Illuminate\Support\Enumerable;
use LaraStrict\Cache\Constants\CacheExpirations;
use LaraStrict\Enums\EnvironmentType;
use LaraStrict\Testing\Laravel\TestingApplication;

interface TestReturnUnionActionContract
{
public function execute(
int $int,
string $string,
$noTypeHint,
EnvironmentType $type,
TestingApplication $testingApplication,
string|int $multi,
string|int|null $multiNull,
string|int|null $multiNullDefault = 'test',
?string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = CacheExpirations::HalfDay,
EnvironmentType $enumDefault = EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
TestingApplication $object = new TestingApplication(),
ArrayAccess&Enumerable $intersectionType = new Collection()
): string|int|null|TestingApplication;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;

use PHPUnit\Framework\Assert;

/**
* @extends \LaraStrict\Testing\AbstractExpectationCallMap<\Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\TestReturnUnionActionContractExpectation>
*/
class TestReturnUnionActionContractAssert extends \LaraStrict\Testing\AbstractExpectationCallMap implements TestReturnUnionActionContract
{
function execute(
int $int,
string $string,
$noTypeHint,
\LaraStrict\Enums\EnvironmentType $type,
\LaraStrict\Testing\Laravel\TestingApplication $testingApplication,
string|int $multi,
string|int|null $multiNull,
string|int|null $multiNullDefault = 'test',
string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
\LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = Constants\CustomConstants::TEST,
\LaraStrict\Testing\Laravel\TestingApplication $object = new \LaraStrict\Testing\Laravel\TestingApplication(/* unknown */),
\ArrayAccess&\Illuminate\Support\Enumerable $intersectionType = new \Illuminate\Support\Collection(/* unknown */),
): \LaraStrict\Testing\Laravel\TestingApplication|string|int|null {
$expectation = $this->getExpectation();
$message = $this->getDebugMessage();

Assert::assertEquals($expectation->int, $int, $message);
Assert::assertEquals($expectation->string, $string, $message);
Assert::assertEquals($expectation->noTypeHint, $noTypeHint, $message);
Assert::assertEquals($expectation->type, $type, $message);
Assert::assertEquals($expectation->testingApplication, $testingApplication, $message);
Assert::assertEquals($expectation->multi, $multi, $message);
Assert::assertEquals($expectation->multiNull, $multiNull, $message);
Assert::assertEquals($expectation->multiNullDefault, $multiNullDefault, $message);
Assert::assertEquals($expectation->optional, $optional, $message);
Assert::assertEquals($expectation->optionalString, $optionalString, $message);
Assert::assertEquals($expectation->constant, $constant, $message);
Assert::assertEquals($expectation->constantClass, $constantClass, $message);
Assert::assertEquals($expectation->enumDefault, $enumDefault, $message);
Assert::assertEquals($expectation->noTypeHintDefault, $noTypeHintDefault, $message);
Assert::assertEquals($expectation->customConstants, $customConstants, $message);
Assert::assertEquals($expectation->object, $object, $message);
Assert::assertEquals($expectation->intersectionType, $intersectionType, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $int, $string, $noTypeHint, $type, $testingApplication, $multi, $multiNull, $multiNullDefault, $optional, $optionalString, $constant, $constantClass, $enumDefault, $noTypeHintDefault, $customConstants, $object, $intersectionType, $expectation);
}

return $expectation->return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;

final class TestReturnUnionActionContractExpectation
{
/**
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly \LaraStrict\Testing\Laravel\TestingApplication|string|int|null $return,
public readonly int $int,
public readonly string $string,
public readonly mixed $noTypeHint,
public readonly \LaraStrict\Enums\EnvironmentType $type,
public readonly \LaraStrict\Testing\Laravel\TestingApplication $testingApplication,
public readonly string|int $multi,
public readonly string|int|null $multiNull,
public readonly string|int|null $multiNullDefault = 'test',
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
public readonly \LaraStrict\Testing\Laravel\TestingApplication $object = new \LaraStrict\Testing\Laravel\TestingApplication(/* unknown */),
public readonly \ArrayAccess&\Illuminate\Support\Enumerable $intersectionType = new \Illuminate\Support\Collection(/* unknown */),
public readonly ?\Closure $hook = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace App\Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;

use PHPUnit\Framework\Assert;

/**
* @extends \LaraStrict\Testing\AbstractExpectationCallMap<\App\Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\TestReturnUnionActionContractExpectation>
*/
class TestReturnUnionActionContractAssert extends \LaraStrict\Testing\AbstractExpectationCallMap implements \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\TestReturnUnionActionContract
{
function execute(
int $int,
string $string,
$noTypeHint,
\LaraStrict\Enums\EnvironmentType $type,
\LaraStrict\Testing\Laravel\TestingApplication $testingApplication,
string|int $multi,
string|int|null $multiNull,
string|int|null $multiNullDefault = 'test',
string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
\LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
\LaraStrict\Testing\Laravel\TestingApplication $object = new \LaraStrict\Testing\Laravel\TestingApplication(/* unknown */),
\ArrayAccess&\Illuminate\Support\Enumerable $intersectionType = new \Illuminate\Support\Collection(/* unknown */),
): \LaraStrict\Testing\Laravel\TestingApplication|string|int|null {
$expectation = $this->getExpectation();
$message = $this->getDebugMessage();

Assert::assertEquals($expectation->int, $int, $message);
Assert::assertEquals($expectation->string, $string, $message);
Assert::assertEquals($expectation->noTypeHint, $noTypeHint, $message);
Assert::assertEquals($expectation->type, $type, $message);
Assert::assertEquals($expectation->testingApplication, $testingApplication, $message);
Assert::assertEquals($expectation->multi, $multi, $message);
Assert::assertEquals($expectation->multiNull, $multiNull, $message);
Assert::assertEquals($expectation->multiNullDefault, $multiNullDefault, $message);
Assert::assertEquals($expectation->optional, $optional, $message);
Assert::assertEquals($expectation->optionalString, $optionalString, $message);
Assert::assertEquals($expectation->constant, $constant, $message);
Assert::assertEquals($expectation->constantClass, $constantClass, $message);
Assert::assertEquals($expectation->enumDefault, $enumDefault, $message);
Assert::assertEquals($expectation->noTypeHintDefault, $noTypeHintDefault, $message);
Assert::assertEquals($expectation->customConstants, $customConstants, $message);
Assert::assertEquals($expectation->object, $object, $message);
Assert::assertEquals($expectation->intersectionType, $intersectionType, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $int, $string, $noTypeHint, $type, $testingApplication, $multi, $multiNull, $multiNullDefault, $optional, $optionalString, $constant, $constantClass, $enumDefault, $noTypeHintDefault, $customConstants, $object, $intersectionType, $expectation);
}

return $expectation->return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;

final class TestReturnUnionActionContractExpectation
{
/**
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly \LaraStrict\Testing\Laravel\TestingApplication|string|int|null $return,
public readonly int $int,
public readonly string $string,
public readonly mixed $noTypeHint,
public readonly \LaraStrict\Enums\EnvironmentType $type,
public readonly \LaraStrict\Testing\Laravel\TestingApplication $testingApplication,
public readonly string|int $multi,
public readonly string|int|null $multiNull,
public readonly string|int|null $multiNullDefault = 'test',
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
public readonly \LaraStrict\Testing\Laravel\TestingApplication $object = new \LaraStrict\Testing\Laravel\TestingApplication(/* unknown */),
public readonly \ArrayAccess&\Illuminate\Support\Enumerable $intersectionType = new \Illuminate\Support\Collection(/* unknown */),
public readonly ?\Closure $hook = null,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace App\Integration\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;

use PHPUnit\Framework\Assert;

/**
* @extends \LaraStrict\Testing\AbstractExpectationCallMap<\App\Integration\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\TestReturnUnionActionContractExpectation>
*/
class TestReturnUnionActionContractAssert extends \LaraStrict\Testing\AbstractExpectationCallMap implements \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\TestReturnUnionActionContract
{
function execute(
int $int,
string $string,
$noTypeHint,
\LaraStrict\Enums\EnvironmentType $type,
\LaraStrict\Testing\Laravel\TestingApplication $testingApplication,
string|int $multi,
string|int|null $multiNull,
string|int|null $multiNullDefault = 'test',
string $optional = null,
string $optionalString = 'test',
string $constant = DIRECTORY_SEPARATOR,
int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
\LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
$noTypeHintDefault = null,
string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
\LaraStrict\Testing\Laravel\TestingApplication $object = new \LaraStrict\Testing\Laravel\TestingApplication(/* unknown */),
\ArrayAccess&\Illuminate\Support\Enumerable $intersectionType = new \Illuminate\Support\Collection(/* unknown */),
): \LaraStrict\Testing\Laravel\TestingApplication|string|int|null {
$expectation = $this->getExpectation();
$message = $this->getDebugMessage();

Assert::assertEquals($expectation->int, $int, $message);
Assert::assertEquals($expectation->string, $string, $message);
Assert::assertEquals($expectation->noTypeHint, $noTypeHint, $message);
Assert::assertEquals($expectation->type, $type, $message);
Assert::assertEquals($expectation->testingApplication, $testingApplication, $message);
Assert::assertEquals($expectation->multi, $multi, $message);
Assert::assertEquals($expectation->multiNull, $multiNull, $message);
Assert::assertEquals($expectation->multiNullDefault, $multiNullDefault, $message);
Assert::assertEquals($expectation->optional, $optional, $message);
Assert::assertEquals($expectation->optionalString, $optionalString, $message);
Assert::assertEquals($expectation->constant, $constant, $message);
Assert::assertEquals($expectation->constantClass, $constantClass, $message);
Assert::assertEquals($expectation->enumDefault, $enumDefault, $message);
Assert::assertEquals($expectation->noTypeHintDefault, $noTypeHintDefault, $message);
Assert::assertEquals($expectation->customConstants, $customConstants, $message);
Assert::assertEquals($expectation->object, $object, $message);
Assert::assertEquals($expectation->intersectionType, $intersectionType, $message);

if (is_callable($expectation->hook)) {
call_user_func($expectation->hook, $int, $string, $noTypeHint, $type, $testingApplication, $multi, $multiNull, $multiNullDefault, $optional, $optionalString, $constant, $constantClass, $enumDefault, $noTypeHintDefault, $customConstants, $object, $intersectionType, $expectation);
}

return $expectation->return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Integration\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand;

final class TestReturnUnionActionContractExpectation
{
/**
* @param \Closure(int,string,mixed,\LaraStrict\Enums\EnvironmentType,\LaraStrict\Testing\Laravel\TestingApplication,string|int,string|int|null,string|int|null,string,string,string,int,\LaraStrict\Enums\EnvironmentType,mixed,string,\LaraStrict\Testing\Laravel\TestingApplication,ArrayAccess&Illuminate\Support\Enumerable,self):void|null $hook
*/
public function __construct(
public readonly \LaraStrict\Testing\Laravel\TestingApplication|string|int|null $return,
public readonly int $int,
public readonly string $string,
public readonly mixed $noTypeHint,
public readonly \LaraStrict\Enums\EnvironmentType $type,
public readonly \LaraStrict\Testing\Laravel\TestingApplication $testingApplication,
public readonly string|int $multi,
public readonly string|int|null $multiNull,
public readonly string|int|null $multiNullDefault = 'test',
public readonly ?string $optional = null,
public readonly string $optionalString = 'test',
public readonly string $constant = \DIRECTORY_SEPARATOR,
public readonly int $constantClass = \LaraStrict\Cache\Constants\CacheExpirations::HalfDay,
public readonly \LaraStrict\Enums\EnvironmentType $enumDefault = \LaraStrict\Enums\EnvironmentType::Testing,
public readonly mixed $noTypeHintDefault = null,
public readonly string $customConstants = \Tests\LaraStrict\Feature\Testing\Commands\MakeExpectationCommand\Constants\CustomConstants::TEST,
public readonly \LaraStrict\Testing\Laravel\TestingApplication $object = new \LaraStrict\Testing\Laravel\TestingApplication(/* unknown */),
public readonly \ArrayAccess&\Illuminate\Support\Enumerable $intersectionType = new \Illuminate\Support\Collection(/* unknown */),
public readonly ?\Closure $hook = null,
) {
}
}

0 comments on commit b0874e8

Please sign in to comment.