Skip to content

Commit f261260

Browse files
committed
refactor: code
refs: #49
1 parent 3932ed9 commit f261260

File tree

6 files changed

+177
-18
lines changed

6 files changed

+177
-18
lines changed

src/Generators/EntityGenerator.php

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

124-
$pathParts = explode('/', $entitiesPath);
125-
126-
if (Str::endsWith(Arr::last($pathParts), '.php')) {
124+
if (Str::endsWith($entitiesPath, '.php')) {
125+
$pathParts = explode('/', $entitiesPath);
127126
array_pop($pathParts);
127+
$entitiesPath = implode('/', $pathParts);
128128
}
129129

130-
$entitiesPath = implode('/', $pathParts);
131-
132130
if ($additionalEntityFolder) {
133131
$entitiesPath = $entitiesPath . "/{$additionalEntityFolder}";
134132
}

tests/Support/Test/Factories/CircularDepFactory.php renamed to tests/Support/Test/Factories/MediaFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
66
use Illuminate\Support\Carbon;
7-
use RonasIT\Support\Tests\Support\Test\Models\CircularDep;
7+
use RonasIT\Support\Tests\Support\Test\Models\Media;
88

9-
class CircularDepFactory extends Factory
9+
class MediaFactory extends Factory
1010
{
11-
protected $model = CircularDep::class;
11+
protected $model = Media::class;
1212

1313
public function definition(): array
1414
{

tests/Support/Test/Models/CircularDep.php renamed to tests/Support/Test/Models/Media.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Eloquent\Model;
66
use RonasIT\Support\Traits\ModelTrait;
77

8-
class CircularDep extends Model
8+
class Media extends Model
99
{
1010
use ModelTrait;
1111

@@ -20,7 +20,7 @@ class CircularDep extends Model
2020
'updated_at'
2121
];
2222

23-
public function dep()
23+
public function preview()
2424
{
2525
return $this->belongsTo(self::class);
2626
}

tests/Support/Test/TestMockTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ public function mockFilesystem(): void
4040

4141
public function mockFilesystemForCircleDependency(): void
4242
{
43-
$model = file_get_contents(getcwd() . '/tests/Support/Test/Models/CircularDep.php');
44-
$factory = file_get_contents(getcwd() . '/tests/Support/Test/Factories/CircularDepFactory.php');
43+
$model = file_get_contents(getcwd() . '/tests/Support/Test/Models/Media.php');
44+
$factory = file_get_contents(getcwd() . '/tests/Support/Test/Factories/MediaFactory.php');
4545

4646
$fileSystemMock = new FileSystemMock;
4747

48-
$fileSystemMock->models = ['CircularDep.php' => $model];
49-
$fileSystemMock->factories = ['CircularDepFactory.php' => $factory];
48+
$fileSystemMock->models = ['Media.php' => $model];
49+
$fileSystemMock->factories = ['MediaFactory.php' => $factory];
5050

5151
$fileSystemMock->setStructure();
5252
}

tests/TestGeneratorTest.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,50 @@ public function testCreateTests()
8989
]);
9090
}
9191

92+
public function testCreateTestsReadDelete()
93+
{
94+
Carbon::setTestNow('2022-02-02');
95+
96+
$mock = Mockery::mock('alias:Illuminate\Support\Facades\DB');
97+
$mock
98+
->shouldReceive('beginTransaction', 'rollBack')
99+
->times(5);
100+
101+
config([
102+
'entity-generator.paths.models' => 'RonasIT\Support\Tests\Support\Test\Models',
103+
'entity-generator.paths.factories' => 'RonasIT\Support\Tests\Support\Test\Factories',
104+
]);
105+
106+
$this->mockClass(TestsGenerator::class, [
107+
$this->classExistsMethodCall(['models', 'User']),
108+
$this->classExistsMethodCall(['factories', 'RoleFactory']),
109+
$this->classExistsMethodCall(['factories', 'UserFactory']),
110+
$this->classExistsMethodCall(['factories', 'CommentFactory'], false),
111+
$this->classExistsMethodCall(['factories', 'RoleFactory']),
112+
$this->classExistsMethodCall(['factories', 'RoleFactory']),
113+
$this->classExistsMethodCall(['factories', 'UserFactory']),
114+
$this->classExistsMethodCall(['factories', 'PostFactory']),
115+
$this->classExistsMethodCall(['factories', 'PostFactory']),
116+
]);
117+
118+
$this->mockFilesystem();
119+
120+
app(TestsGenerator::class)
121+
->setCrudOptions(['R', 'D'])
122+
->setModel('Post')
123+
->generate();
124+
125+
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/PostTest/dump.sql');
126+
$this->assertGeneratedFileEquals('post_test_read_delete.php', 'tests/PostTest.php');
127+
128+
$this->assertEventPushedChain([
129+
SuccessCreateMessage::class => [
130+
'Created a new Test dump on path: tests/fixtures/PostTest/dump.sql',
131+
'Created a new Test: PostTest',
132+
],
133+
]);
134+
}
135+
92136
public function testCreateTestsDumpStubNotExist()
93137
{
94138
config([
@@ -118,7 +162,9 @@ public function testCreateTestsDumpStubNotExist()
118162
$this->assertGeneratedFileEquals('post_test.php', 'tests/PostTest.php');
119163

120164
$this->assertEventPushedChain([
121-
WarningEvent::class => ['Generation of dump has been skipped cause the view incorrect_stub from the config entity-generator.stubs.dump is not exists. Please check that config has the correct view name value.'],
165+
WarningEvent::class => [
166+
'Generation of dump has been skipped cause the view incorrect_stub from the config entity-generator.stubs.dump is not exists. Please check that config has the correct view name value.',
167+
],
122168
SuccessCreateMessage::class => [
123169
'Created a new Test fixture on path: tests/fixtures/PostTest/create_post_request.json',
124170
'Created a new Test fixture on path: tests/fixtures/PostTest/create_post_response.json',
@@ -175,7 +221,9 @@ public function testCreateTestsTestStubNotExist()
175221
'Created a new Test fixture on path: tests/fixtures/PostTest/create_post_response.json',
176222
'Created a new Test fixture on path: tests/fixtures/PostTest/update_post_request.json',
177223
],
178-
WarningEvent::class => ['Generation of test has been skipped cause the view incorrect_stub from the config entity-generator.stubs.test is not exists. Please check that config has the correct view name value.'],
224+
WarningEvent::class => [
225+
'Generation of test has been skipped cause the view incorrect_stub from the config entity-generator.stubs.test is not exists. Please check that config has the correct view name value.',
226+
],
179227
]);
180228
}
181229

@@ -198,14 +246,14 @@ className: CircularRelationsFoundedException::class,
198246
$this->mockClass(TestsGenerator::class, [
199247
$this->classExistsMethodCall(['models', 'User']),
200248
$this->classExistsMethodCall(['factories', 'RoleFactory']),
201-
$this->classExistsMethodCall(['factories', 'CircularDepFactory']),
249+
$this->classExistsMethodCall(['factories', 'MediaFactory']),
202250
]);
203251

204252
$this->mockFilesystemForCircleDependency();
205253

206254
app(TestsGenerator::class)
207255
->setCrudOptions(['C', 'R', 'U', 'D'])
208-
->setModel('CircularDep')
256+
->setModel('Media')
209257
->generate();
210258

211259
Event::assertNothingDispatched();
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use RonasIT\Support\Tests\ModelTestState;
6+
use RonasIT\Support\Tests\Support\Test\Models\Post;
7+
use RonasIT\Support\Tests\Support\Test\Models\User;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
10+
class PostTest extends TestCase
11+
{
12+
protected static User $user;
13+
14+
protected static ModelTestState $postState;
15+
16+
public function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
self::$user ??= User::find(1);
21+
22+
self::$postState ??= new ModelTestState(Post::class);
23+
}
24+
25+
public function testDelete()
26+
{
27+
$response = $this->actingAs(self::$user)->json('delete', '/posts/1');
28+
29+
$response->assertNoContent();
30+
31+
// TODO: Need to remove last argument after first successful start
32+
self::$postState->assertChangesEqualsFixture('delete_post_state.json', true);
33+
}
34+
35+
public function testDeleteNotExists()
36+
{
37+
$response = $this->actingAs(self::$user)->json('delete', '/posts/0');
38+
39+
$response->assertNotFound();
40+
41+
self::$postState->assertNotChanged();
42+
}
43+
44+
public function testDeleteNoAuth()
45+
{
46+
$response = $this->json('delete', '/posts/1');
47+
48+
$response->assertUnauthorized();
49+
}
50+
51+
public function testGet()
52+
{
53+
$response = $this->actingAs(self::$user)->json('get', '/posts/1');
54+
55+
$response->assertOk();
56+
57+
// TODO: Need to remove after first successful start
58+
$this->exportJson('get_post.json', $response->json());
59+
60+
$this->assertEqualsFixture('get_post.json', $response->json());
61+
}
62+
63+
public function testGetNotExists()
64+
{
65+
$response = $this->actingAs(self::$user)->json('get', '/posts/0');
66+
67+
$response->assertNotFound();
68+
}
69+
70+
public function testGetNoAuth()
71+
{
72+
$response = $this->json('get', '/posts/1');
73+
74+
$response->assertUnauthorized();
75+
}
76+
77+
public static function getSearchFilters(): array
78+
{
79+
return [
80+
[
81+
'filter' => ['all' => 1],
82+
'fixture' => 'search_all.json',
83+
],
84+
[
85+
'filter' => [
86+
'page' => 2,
87+
'per_page' => 2,
88+
],
89+
'fixture' => 'search_by_page_per_page.json',
90+
],
91+
];
92+
}
93+
94+
#[DataProvider('getSearchFilters')]
95+
public function testSearch(array $filter, string $fixture)
96+
{
97+
$response = $this->actingAs(self::$user)->json('get', '/posts', $filter);
98+
99+
$response->assertOk();
100+
101+
// TODO: Need to remove after first successful start
102+
$this->exportJson($fixture, $response->json());
103+
104+
$this->assertEqualsFixture($fixture, $response->json());
105+
}
106+
107+
public function testSearchNoAuth()
108+
{
109+
$response = $this->json('get', '/posts');
110+
111+
$response->assertUnauthorized();
112+
}
113+
}

0 commit comments

Comments
 (0)