Skip to content

Commit

Permalink
Add console register commands tests (#134)
Browse files Browse the repository at this point in the history
* Add tests for registering reactants & reacters console commands

* Fix love:register-reactants command with --ids delimited by comma

* Fix love:register-reacters command with --ids delimited by comma
  • Loading branch information
antonkomarev authored Jan 29, 2020
1 parent 125a34e commit 5d75e7f
Show file tree
Hide file tree
Showing 12 changed files with 544 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/Console/Commands/RegisterReactants.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,14 @@ private function findModelTypeInMorphMap(

private function normalizeIds(array $modelIds): array
{
if (isset($modelIds[0]) && strpos($modelIds[0], ',')) {
$modelIds = explode(',', $modelIds[0]);
if (!isset($modelIds[0])) {
return $modelIds;
}

$firstItem = $modelIds[0];

if (is_string($firstItem) && strpos($firstItem, ',')) {
$modelIds = explode(',', $firstItem);
}

return $modelIds;
Expand Down
10 changes: 8 additions & 2 deletions src/Console/Commands/RegisterReacters.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,14 @@ private function findModelTypeInMorphMap(

private function normalizeIds(array $modelIds): array
{
if (isset($modelIds[0]) && strpos($modelIds[0], ',')) {
$modelIds = explode(',', $modelIds[0]);
if (!isset($modelIds[0])) {
return $modelIds;
}

$firstItem = $modelIds[0];

if (is_string($firstItem) && strpos($firstItem, ',')) {
$modelIds = explode(',', $firstItem);
}

return $modelIds;
Expand Down
40 changes: 40 additions & 0 deletions tests/Stubs/Models/MorphMappedReactable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Tests\Laravel\Love\Stubs\Models;

use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableContract;
use Cog\Laravel\Love\Reactable\Models\Traits\Reactable;
use Illuminate\Database\Eloquent\Model;

final class MorphMappedReactable extends Model implements
ReactableContract
{
use Reactable;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'morph_mapped_reactables';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];
}
40 changes: 40 additions & 0 deletions tests/Stubs/Models/MorphMappedReacterable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Tests\Laravel\Love\Stubs\Models;

use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableContract;
use Cog\Laravel\Love\Reacterable\Models\Traits\Reacterable;
use Illuminate\Database\Eloquent\Model;

final class MorphMappedReacterable extends Model implements
ReacterableContract
{
use Reacterable;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'morph_mapped_reacterables';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
];
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Cog\Laravel\Love\LoveServiceProvider;
use Cog\Tests\Laravel\Love\Stubs\Models\EntityWithMorphMap;
use Cog\Tests\Laravel\Love\Stubs\Models\MorphMappedReactable;
use Cog\Tests\Laravel\Love\Stubs\Models\MorphMappedReacterable;
use Cog\Tests\Laravel\Love\Stubs\Models\User;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Filesystem\Filesystem;
Expand Down Expand Up @@ -102,6 +104,8 @@ private function registerTestMorphMaps(): void
{
Relation::morphMap([
'entity-with-morph-map' => EntityWithMorphMap::class,
'morph-mapped-reactable' => MorphMappedReactable::class,
'morph-mapped-reacterable' => MorphMappedReacterable::class,
]);
}

Expand Down
145 changes: 145 additions & 0 deletions tests/Unit/Console/Commands/RegisterReactantsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Tests\Laravel\Love\Unit\Console\Commands;

use Cog\Laravel\Love\Console\Commands\RegisterReactants;
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Cog\Tests\Laravel\Love\Stubs\Models\Article;
use Cog\Tests\Laravel\Love\Stubs\Models\MorphMappedReactable;
use Cog\Tests\Laravel\Love\Stubs\Models\User;
use Cog\Tests\Laravel\Love\TestCase;

final class RegisterReactantsTest extends TestCase
{
/** @test */
public function it_can_create_reactants_for_all_models_of_type(): void
{
Article::unsetEventDispatcher();
User::unsetEventDispatcher();
$articleReactables = factory(Article::class, 3)->create();
$userReactables = factory(User::class, 3)->create();
$reactantsCount = Reactant::query()->count();
$command = $this->artisan(RegisterReactants::class, [
'--model' => Article::class,
]);

$status = $command->run();

$this->assertSame(0, $status);
$this->assertSame($reactantsCount + 3, Reactant::query()->count());
foreach ($articleReactables as $reactable) {
$this->assertTrue($reactable->fresh()->isRegisteredAsLoveReactant());
}
foreach ($userReactables as $reactable) {
$this->assertFalse($reactable->fresh()->isRegisteredAsLoveReactant());
}
}

/** @test */
public function it_can_create_reactants_for_all_models_of_type_with_morph_map(): void
{
MorphMappedReactable::unsetEventDispatcher();
User::unsetEventDispatcher();
$morphMappedReactables = factory(MorphMappedReactable::class, 3)->create();
$userReactables = factory(User::class, 3)->create();
$reactantsCount = Reactant::query()->count();
$command = $this->artisan(RegisterReactants::class, [
'--model' => 'morph-mapped-reactable',
]);

$status = $command->run();

$this->assertSame(0, $status);
$this->assertSame($reactantsCount + 3, Reactant::query()->count());
foreach ($morphMappedReactables as $reactable) {
$this->assertTrue($reactable->fresh()->isRegisteredAsLoveReactant());
}
foreach ($userReactables as $reactable) {
$this->assertFalse($reactable->fresh()->isRegisteredAsLoveReactant());
}
}

/** @test */
public function it_can_create_reactants_for_specific_model_ids(): void
{
Article::unsetEventDispatcher();
User::unsetEventDispatcher();
$articleReactables = factory(Article::class, 3)->create();
$firstArticleReactable = $articleReactables->get(0);
$lastArticleReactable = $articleReactables->get(2);
$userReactables = factory(User::class, 3)->create();
$reactantsCount = Reactant::query()->count();
$command = $this->artisan(RegisterReactants::class, [
'--model' => Article::class,
'--ids' => [
$firstArticleReactable->id,
$lastArticleReactable->id,
],
]);

$status = $command->run();

$this->assertSame(0, $status);
$this->assertSame($reactantsCount + 2, Reactant::query()->count());
$this->assertTrue($articleReactables->get(0)->fresh()->isRegisteredAsLoveReactant());
$this->assertFalse($articleReactables->get(1)->fresh()->isRegisteredAsLoveReactant());
$this->assertTrue($articleReactables->get(2)->fresh()->isRegisteredAsLoveReactant());
$this->assertFalse($userReactables->get(0)->fresh()->isRegisteredAsLoveReactant());
$this->assertFalse($userReactables->get(1)->fresh()->isRegisteredAsLoveReactant());
$this->assertFalse($userReactables->get(2)->fresh()->isRegisteredAsLoveReactant());
}

/** @test */
public function it_can_create_reactants_for_specific_model_ids_delimited_with_comma(): void
{
Article::unsetEventDispatcher();
User::unsetEventDispatcher();
$articleReactables = factory(Article::class, 3)->create();
$firstArticleReactable = $articleReactables->get(0);
$lastArticleReactable = $articleReactables->get(2);
$userReactables = factory(User::class, 3)->create();
$reactantsCount = Reactant::query()->count();
$command = $this->artisan(RegisterReactants::class, [
'--model' => Article::class,
'--ids' => ["{$firstArticleReactable->id},{$lastArticleReactable->id}"],
]);

$status = $command->run();

$this->assertSame(0, $status);
$this->assertSame($reactantsCount + 2, Reactant::query()->count());
$this->assertTrue($articleReactables->get(0)->fresh()->isRegisteredAsLoveReactant());
$this->assertFalse($articleReactables->get(1)->fresh()->isRegisteredAsLoveReactant());
$this->assertTrue($articleReactables->get(2)->fresh()->isRegisteredAsLoveReactant());
$this->assertFalse($userReactables->get(0)->fresh()->isRegisteredAsLoveReactant());
$this->assertFalse($userReactables->get(1)->fresh()->isRegisteredAsLoveReactant());
$this->assertFalse($userReactables->get(2)->fresh()->isRegisteredAsLoveReactant());
}

/** @test */
public function it_not_create_duplicate_reactants(): void
{
factory(Article::class, 3)->create();
factory(User::class, 3)->create();
$reactantsCount = Reactant::query()->count();
$command = $this->artisan(RegisterReactants::class, [
'--model' => Article::class,
]);

$status = $command->run();

$this->assertSame(0, $status);
$this->assertSame($reactantsCount, Reactant::query()->count());
}
}
Loading

0 comments on commit 5d75e7f

Please sign in to comment.