Skip to content

Commit ebc5f9b

Browse files
authored
Merge pull request #79 from RonasIT/49-add-tests-generator-tests
Add tests generator tests
2 parents 74c273d + f261260 commit ebc5f9b

21 files changed

+872
-21
lines changed

src/Generators/AbstractTestsGenerator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ abstract class AbstractTestsGenerator extends EntityGenerator
2525

2626
public function generate(): void
2727
{
28+
if ($this->canGenerateUserData()) {
29+
$this->withAuth = true;
30+
}
31+
2832
$this->createDump();
2933
$this->generateFixtures();
3034
$this->generateTests();
@@ -70,9 +74,8 @@ protected function getInserts(): array
7074
{
7175
$arrayModels = [$this->model];
7276

73-
if ($this->canGenerateUserData()) {
77+
if ($this->withAuth) {
7478
array_unshift($arrayModels, 'User');
75-
$this->withAuth = true;
7679
}
7780

7881
return array_map(function ($model) {
@@ -113,7 +116,7 @@ protected function getDumpValuesList($model): array
113116

114117
array_walk($values, function (&$value) {
115118
if ($value instanceof DateTime) {
116-
$value = "'{$value->format('Y-m-d h:i:s')}'";
119+
$value = "{$value->format('Y-m-d h:i:s')}";
117120
} elseif (is_bool($value)) {
118121
$value = ($value) ? 'true' : 'false';
119122
} elseif (is_array($value)) {

src/Generators/EntityGenerator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ protected function saveClass($path, $name, $content, $additionalEntityFolder = f
121121
{
122122
$entitiesPath = base_path($this->paths[$path]);
123123

124+
if (Str::endsWith($entitiesPath, '.php')) {
125+
$pathParts = explode('/', $entitiesPath);
126+
array_pop($pathParts);
127+
$entitiesPath = implode('/', $pathParts);
128+
}
129+
124130
if ($additionalEntityFolder) {
125131
$entitiesPath = $entitiesPath . "/{$additionalEntityFolder}";
126132
}

src/Generators/TestsGenerator.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,6 @@ public function getTestClassName(): string
1111
return "{$this->model}Test";
1212
}
1313

14-
protected function generateExistedEntityFixture()
15-
{
16-
$object = $this->getFixtureValuesList($this->model);
17-
$entity = Str::snake($this->model);
18-
19-
foreach (self::FIXTURE_TYPES as $type => $modifications) {
20-
if ($this->isFixtureNeeded($type)) {
21-
foreach ($modifications as $modification) {
22-
$excepts = [];
23-
if ($modification === 'request') {
24-
$excepts = ['id'];
25-
}
26-
$this->generateFixture("{$type}_{$entity}_{$modification}.json", Arr::except($object, $excepts));
27-
}
28-
}
29-
}
30-
}
31-
3214
protected function isFixtureNeeded($type): bool
3315
{
3416
$firstLetter = strtoupper($type[0]);

tests/Support/FileSystemMock.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class FileSystemMock
1616
public ?array $testFixtures = null;
1717
public ?array $testClasses = null;
1818
public ?array $routes = null;
19+
public ?array $factories = null;
1920

2021
public function setStructure(): void
2122
{
@@ -97,6 +98,14 @@ public function setStructure(): void
9798
}
9899
}
99100

101+
if (!is_null($this->factories)) {
102+
$structure['database']['factories'] = [];
103+
104+
foreach ($this->factories as $factory => $content) {
105+
$structure['database']['factories'][$factory] = $content;
106+
}
107+
}
108+
100109
vfsStream::create($structure);
101110
}
102111
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Test\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Carbon;
7+
use RonasIT\Support\Tests\Support\Test\Models\Media;
8+
9+
class MediaFactory extends Factory
10+
{
11+
protected $model = Media::class;
12+
13+
public function definition(): array
14+
{
15+
return [
16+
'title' => 'some title',
17+
'body' => 'some body',
18+
'posted_at' => Carbon::now(),
19+
'drafted' => false,
20+
'data' => [
21+
'title' => '1',
22+
'body' => '2',
23+
],
24+
];
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Test\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Carbon;
7+
use RonasIT\Support\Tests\Support\Test\Models\Post;
8+
9+
class PostFactory extends Factory
10+
{
11+
protected $model = Post::class;
12+
13+
public function definition(): array
14+
{
15+
return [
16+
'title' => 'some title',
17+
'body' => 'some body',
18+
'posted_at' => Carbon::now(),
19+
'drafted' => false,
20+
'data' => [
21+
'title' => '1',
22+
'body' => '2',
23+
],
24+
];
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Test\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use RonasIT\Support\Tests\Support\Test\Models\Role;
7+
8+
class RoleFactory extends Factory
9+
{
10+
protected $model = Role::class;
11+
12+
public function definition(): array
13+
{
14+
return [
15+
'name' => 'some name',
16+
];
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Test\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use RonasIT\Support\Tests\Support\Test\Models\User;
7+
8+
class UserFactory extends Factory
9+
{
10+
protected $model = User::class;
11+
12+
public function definition(): array
13+
{
14+
return [
15+
'name' => 'some name',
16+
'email' => 'some email',
17+
];
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Test\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use RonasIT\Support\Traits\ModelTrait;
7+
8+
class Comment extends Model
9+
{
10+
use ModelTrait;
11+
12+
protected $fillable = [
13+
'text',
14+
'post_id',
15+
'created_at',
16+
'updated_at'
17+
];
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Test\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use RonasIT\Support\Traits\ModelTrait;
7+
8+
class Media extends Model
9+
{
10+
use ModelTrait;
11+
12+
protected $fillable = [
13+
'title',
14+
'body',
15+
'data',
16+
'drafted',
17+
'user_id',
18+
'posted_at',
19+
'created_at',
20+
'updated_at'
21+
];
22+
23+
public function preview()
24+
{
25+
return $this->belongsTo(self::class);
26+
}
27+
}

0 commit comments

Comments
 (0)