Skip to content
Merged
Changes from 9 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
69 changes: 68 additions & 1 deletion tests/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,33 @@

namespace RonasIT\Support\Tests;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Event;
use Illuminate\View\ViewException;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use RonasIT\Support\Events\SuccessCreateMessage;
use RonasIT\Support\Events\WarningEvent;
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
use RonasIT\Support\Exceptions\ClassNotExistsException;
use RonasIT\Support\Exceptions\IncorrectClassPathException;
use RonasIT\Support\Generators\FactoryGenerator;
use RonasIT\Support\Tests\Support\Factory\FactoryMockTrait;

class FactoryGeneratorTest extends TestCase
{
use FactoryMockTrait;

public function setUp(): void
{
parent::setUp();

vfsStream::newDirectory('config')->at($this->rootDirectory);
vfsStream::newDirectory('routes')
->at($this->rootDirectory)
->addChild(new vfsStreamDirectory('api.php'));
}

public function testModelNotExists()
{
$this->assertExceptionThrew(
Expand Down Expand Up @@ -104,7 +118,7 @@ public function testCreateFactoryWithoutFactoryStub(): void

config(['entity-generator.stubs.factory' => 'incorrect_stub']);

$result = app(FactoryGenerator::class)
app(FactoryGenerator::class)
->setFields([
'integer-required' => ['author_id'],
'string' => ['title', 'iban', 'something'],
Expand All @@ -125,4 +139,57 @@ className: WarningEvent::class,
message: 'Generation of factory has been skipped cause the view incorrect_stub from the config entity-generator.stubs.factory is not exists. Please check that config has the correct view name value.',
);
}

public function testHasntFolderCorrectCase(): void
{
$this->mockFilesystem();

Config::set('entity-generator.paths.factories', 'dAtaAbase/FactoorieesS');

$this->expectException(IncorrectClassPathException::class);

$this->expectExceptionMessage('Incorrect path to factories, dAtaAbase folder must start with a capital letter, please specify the path according to the PSR.');

app(FactoryGenerator::class)
->setFields([
'integer-required' => ['author_id'],
'string' => ['title', 'iban', 'something'],
'json' => ['json_text'],
])
->setRelations([
'hasOne' => ['user'],
'hasMany' => [],
'belongsTo' => ['user'],
])
->setModel('Post')
->generate();
}

public function testRemovePhpExtension(): void
{
$this->mockFilesystem();

Config::set('entity-generator.paths.factories', 'database/factories/Factory.php');

app(FactoryGenerator::class)
->setFields([
'integer-required' => ['author_id'],
'string' => ['title', 'iban', 'something'],
'json' => ['json_text'],
])
->setRelations([
'hasOne' => ['user'],
'hasMany' => [],
'belongsTo' => ['user'],
])
->setModel('Post')
->generate();

$this->assertGeneratedFileEquals('post_factory.php', '/database/factories/PostFactory.php');

$this->assertEventPushed(
className: SuccessCreateMessage::class,
message: 'Created a new Factory: PostFactory',
);
}
}