Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jan 16, 2025
1 parent e7cc680 commit 2c03ea8
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 72 deletions.
8 changes: 4 additions & 4 deletions tests/Feature/EnumAnnotateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
});

it('warns that no enums were annotated if no enums can be found', function() {
Enums::paths('noEnums');
Enums::setPaths('noEnums');

$this
->artisan('enum:annotate', ['--all' => true])
->expectsOutput('No enums to annotate.')
->assertExitCode(0);

Enums::paths('app/Enums'); // reset default path
Enums::setPaths('app/Enums');
});

it('displays an error message when it fails', function() {
Expand All @@ -34,7 +34,7 @@
});

it('annotates all the discoverable enums', function() {
Enums::paths('app/Enums', 'domain/*/Enums');
Enums::setPaths('app/Enums', 'domain/*/Enums');

expect($this->artisan('enum:annotate', ['--all' => true]))->toAnnotate([
Enum1::class,
Expand All @@ -44,5 +44,5 @@
PayoutStatuses::class,
]);

Enums::paths('app/Enums'); // reset default path
Enums::setPaths('app/Enums'); // reset default path
});
45 changes: 45 additions & 0 deletions tests/Feature/EnumMakeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Laravel\Prompts\TextareaPrompt;

it('succeeds if an enum already exists', function() {
$this
->artisan('enum:make', ['enum' => 'App/Enums/Enum1'])
->expectsOutput('The enum App\Enums\Enum1 already exists.')
->assertExitCode(0);
});

it('fails if the backed option is not supported', function() {
$this
->artisan('enum:make', ['enum' => 'App/Enums/Test', 'cases' => ['one'], '--backed' => 'foo'])
->expectsOutputToContain('The option --backed supports only')
->assertExitCode(1);
});

it('generates enums', function(string $enum, ?string $backed) {
$command = $this->artisan('enum:make', [
'enum' => $enum,
'cases' => ['CaseOne', 'CaseTwo'],
'--backed' => $backed
]);

expect($command)->toGenerate($enum);
})->with([
['App\Enums\Generated1', 'bitwise'],
['Domain\Common\Enums\Generated2', 'snake'],
['SubDirectory\Generated3', null],
]);

it('generates enums with prompts', function() {
// Currently textarea() of Laravel Prompts seems to have some testing issues.
// Apparently the method expectsQuestion() does not test textarea rightly.
// In alternative, we fallback the prompt with the expected user input.
TextareaPrompt::fallbackUsing(fn() => 'CaseOne' . PHP_EOL . 'CaseTwo');

$command = $this->artisan('enum:make')
->expectsQuestion('The namespace of the enum', 'App\Enums\Generated1')
->expectsQuestion('How cases should be backed', 'bitwise');
// ->expectsQuestion('The cases (one per line)', 'CaseOne' . PHP_EOL . 'CaseTwo');

expect($command)->toGenerate('App\Enums\Generated1');
});
30 changes: 30 additions & 0 deletions tests/Feature/EnumTsCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use App\Enums\Enum1;
use App\Enums\Enum2;
use Cerbero\LaravelEnum\Enums;

it('warns that no enums were annotated if invalid enums are provided', function() {
$this
->artisan('enum:ts', ['enums' => ['InvalidEnum']])
->expectsOutput('No enums to annotate.')
->assertExitCode(0);
});

it('warns that no enums were annotated if no enums can be found', function() {
Enums::setPaths('noEnums');

$this
->artisan('enum:ts', ['--all' => true])
->expectsOutput('No enums to annotate.')
->assertExitCode(0);

Enums::setPaths('app/Enums');
});

it('synchronizes all the discoverable enums', function() {
expect($this->artisan('enum:ts', ['--all' => true]))->toTypeScript([
Enum1::class,
Enum2::class,
]);
});
4 changes: 2 additions & 2 deletions tests/Feature/EnumsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
});

it('yields the namespace of enums in custom paths', function() {
Enums::paths('app/Enums', 'domain/*/Enums');
Enums::setPaths('app/Enums', 'domain/*/Enums');

expect(Enums::namespaces())->sequence(
'App\Enums\Enum1',
Expand All @@ -21,5 +21,5 @@
);

// reset the initial state
Enums::paths('app/Enums');
Enums::setPaths('app/Enums');
});
73 changes: 40 additions & 33 deletions tests/Feature/SessionKeysTest.php
Original file line number Diff line number Diff line change
@@ -1,102 +1,109 @@
<?php

use Cerbero\LaravelEnum\SessionKeys;
use Cerbero\LaravelEnum\Capsules\SessionKey;
use Domain\Common\Enums\SessionKeys;
use Illuminate\Support\Facades\Session;

it('supports the exists() method', function() {
Session::shouldReceive('exists')->with('PageViews')->andReturn(true);
Session::shouldReceive('exists')->with('CartItems')->andReturn(true);

expect(SessionKeys::PageViews->exists())->toBe(true);
expect(SessionKeys::CartItems()->exists())->toBe(true);
});

it('supports dynamic parameters', function() {
Session::shouldReceive('exists')->with('Forms.123.Data')->andReturn(true);

expect(SessionKeys::FormsData(123)->exists())->toBe(true);
});

it('supports the missing() method', function() {
Session::shouldReceive('missing')->with('PageViews')->andReturn(true);
Session::shouldReceive('missing')->with('CartItems')->andReturn(true);

expect(SessionKeys::PageViews->missing())->toBe(true);
expect(SessionKeys::CartItems()->missing())->toBe(true);
});

it('supports the has() method', function() {
Session::shouldReceive('has')->with('PageViews')->andReturn(true);
Session::shouldReceive('has')->with('CartItems')->andReturn(true);

expect(SessionKeys::PageViews->hasValue())->toBe(true);
expect(SessionKeys::CartItems()->hasValue())->toBe(true);
});

it('supports the get() method', function() {
Session::shouldReceive('get')->with('PageViews', 123)->andReturn('foo');
Session::shouldReceive('get')->with('CartItems', 123)->andReturn('foo');

expect(SessionKeys::PageViews->get(123))->toBe('foo');
expect(SessionKeys::CartItems()->get(123))->toBe('foo');
});

it('supports the pull() method', function() {
Session::shouldReceive('pull')->with('PageViews', 123)->andReturn('foo');
Session::shouldReceive('pull')->with('CartItems', 123)->andReturn('foo');

expect(SessionKeys::PageViews->pull(123))->toBe('foo');
expect(SessionKeys::CartItems()->pull(123))->toBe('foo');
});

it('supports the hasOldInput() method', function() {
Session::shouldReceive('hasOldInput')->with('PageViews')->andReturn(true);
Session::shouldReceive('hasOldInput')->with('CartItems')->andReturn(true);

expect(SessionKeys::PageViews->hasOldInput())->toBe(true);
expect(SessionKeys::CartItems()->hasOldInput())->toBe(true);
});

it('supports the getOldInput() method', function() {
Session::shouldReceive('getOldInput')->with('PageViews', 123)->andReturn('foo');
Session::shouldReceive('getOldInput')->with('CartItems', 123)->andReturn('foo');

expect(SessionKeys::PageViews->getOldInput(123))->toBe('foo');
expect(SessionKeys::CartItems()->getOldInput(123))->toBe('foo');
});

it('supports the put() method', function() {
Session::shouldReceive('put')->with('PageViews', 123);
Session::shouldReceive('put')->with('CartItems', 123);

expect(SessionKeys::PageViews->put(123))->toBe(SessionKeys::PageViews);
expect(SessionKeys::CartItems()->put(123))->toBeInstanceOf(SessionKey::class);
});

it('supports the remember() method', function() {
$expectedCallback = Mockery::on(fn(Closure $callback) => $callback() === 'foo');

Session::shouldReceive('remember')->with('PageViews', $expectedCallback)->andReturn('foo');
Session::shouldReceive('remember')->with('CartItems', $expectedCallback)->andReturn('foo');

expect(SessionKeys::PageViews->remember(fn() => 'foo'))->toBe('foo');
expect(SessionKeys::CartItems()->remember(fn() => 'foo'))->toBe('foo');
});

it('supports the push() method', function() {
Session::shouldReceive('push')->with('PageViews', 123);
Session::shouldReceive('push')->with('CartItems', 123);

expect(SessionKeys::PageViews->push(123))->toBe(SessionKeys::PageViews);
expect(SessionKeys::CartItems()->push(123))->toBeInstanceOf(SessionKey::class);
});

it('supports the increment() method', function() {
Session::shouldReceive('increment')->with('PageViews', 2)->andReturn(10);
Session::shouldReceive('increment')->with('CartItems', 2)->andReturn(10);

expect(SessionKeys::PageViews->increment(2))->toBe(10);
expect(SessionKeys::CartItems()->increment(2))->toBe(10);
});

it('supports the decrement() method', function() {
Session::shouldReceive('decrement')->with('PageViews', 2)->andReturn(10);
Session::shouldReceive('decrement')->with('CartItems', 2)->andReturn(10);

expect(SessionKeys::PageViews->decrement(2))->toBe(10);
expect(SessionKeys::CartItems()->decrement(2))->toBe(10);
});

it('supports the flash() method', function() {
Session::shouldReceive('flash')->with('PageViews', 123);
Session::shouldReceive('flash')->with('CartItems', 123);

expect(SessionKeys::PageViews->flash(123))->toBe(SessionKeys::PageViews);
expect(SessionKeys::CartItems()->flash(123))->toBeInstanceOf(SessionKey::class);
});

it('supports the now() method', function() {
Session::shouldReceive('now')->with('PageViews', 123);
Session::shouldReceive('now')->with('CartItems', 123);

expect(SessionKeys::PageViews->now(123))->toBe(SessionKeys::PageViews);
expect(SessionKeys::CartItems()->now(123))->toBeInstanceOf(SessionKey::class);
});

it('supports the remove() method', function() {
Session::shouldReceive('remove')->with('PageViews')->andReturn('foo');
Session::shouldReceive('remove')->with('CartItems')->andReturn('foo');

expect(SessionKeys::PageViews->remove())->toBe('foo');
expect(SessionKeys::CartItems()->remove())->toBe('foo');
});

it('supports the forget() method', function() {
Session::shouldReceive('forget')->with('PageViews');
Session::shouldReceive('forget')->with('CartItems');

expect(SessionKeys::PageViews->forget())->toBe(SessionKeys::PageViews);
expect(SessionKeys::CartItems()->forget())->toBeInstanceOf(SessionKey::class);
});
49 changes: 48 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
|
*/

use Cerbero\LaravelEnum\Enums;

use function Cerbero\Enum\namespaceToPath;
use function Cerbero\Enum\path;

uses(Cerbero\LaravelEnum\TestCase::class)->in('Feature');

/*
Expand Down Expand Up @@ -40,7 +45,7 @@
foreach ($oldContents as $filename => $oldContent) {
$stub = __DIR__ . '/stubs/' . basename($filename, '.php') . '.stub';

expect(file_get_contents($filename))->toBe(file_get_contents($stub));
expect($filename)->toContainIgnoreEol($stub);
}
} finally {
foreach ($oldContents as $filename => $oldContent) {
Expand All @@ -49,6 +54,48 @@
}
});

expect()->extend('toContainIgnoreEol', function (string $path) {
// normalize content to avoid end-of-line incompatibility between OS
$actualContent = str_replace("\r\n", "\n", file_get_contents(path($this->value)));
$expectedContent = str_replace("\r\n", "\n", file_get_contents(path($path)));

expect($actualContent)->toBe($expectedContent);
});

expect()->extend('toGenerate', function (string $enum) {
expect(class_exists($enum))->toBeFalse();

try {
$this->value->expectsOutputToContain($enum)->assertExitCode(0)->run();

$filename = namespaceToPath($enum);
$stub = __DIR__ . '/stubs/make/' . class_basename($enum) . '.stub';

expect($filename)->toContainIgnoreEol($stub);
} finally {
file_exists($filename) && unlink($filename);
}
});

expect()->extend('toTypeScript', function (array $enums) {
$paths = [];

try {
$this->value->expectsOutputToContain($enums[0])->assertExitCode(0)->run();

foreach ($enums as $enum) {
$paths[] = $path = Enums::basePath(Enums::typeScript($enum));
$stub = __DIR__ . '/stubs/ts/enums.stub';

expect($path)->toContainIgnoreEol($stub);
}
} finally {
foreach ($paths as $path) {
file_exists($path) && unlink($path);
}
}
});

/*
|--------------------------------------------------------------------------
| Functions
Expand Down
17 changes: 0 additions & 17 deletions tests/SessionKeys.php

This file was deleted.

8 changes: 4 additions & 4 deletions tests/Skeleton/domain/Common/Enums/SessionKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
/**
* The enum to enumerate session keys.
*/
enum SessionKeys
enum SessionKeys: string
{
use EnumeratesSessionKeys;

case CartItems;
case OnboardingStep;
case PageViews;
case CartItems = 'CartItems';
case OnboardingStep = 'OnboardingStep';
case FormsData = 'Forms.{int $formId}.Data';
}
6 changes: 3 additions & 3 deletions tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
/**
* The testing user model.
*
* @property ?CasesCollection<int, BitwiseEnum> $bitwise
* @property ?CasesCollection<int, BackedEnum> $numbers
* @property ?CasesCollection<int, PureEnum> $pureNumbers
* @property ?CasesCollection<BitwiseEnum> $bitwise
* @property ?CasesCollection<BackedEnum> $numbers
* @property ?CasesCollection<PureEnum> $pureNumbers
*/
final class User extends Model
{
Expand Down
2 changes: 1 addition & 1 deletion tests/stubs/CacheKeys.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ declare(strict_types=1);

namespace Domain\Common\Enums;

use Cerbero\LaravelEnum\Capsules\CacheKey;
use Cerbero\LaravelEnum\Concerns\EnumeratesCacheKeys;
use Cerbero\LaravelEnum\Services\CacheKey;

/**
* The enum to enumerate cache keys.
Expand Down
Loading

0 comments on commit 2c03ea8

Please sign in to comment.