Skip to content

Commit 767843c

Browse files
committed
Added Dynamic Model creation for creating user and creating content. Also fixed some failing tests
1 parent c8ef65a commit 767843c

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

src/Darryldecode/Backend/Base/Registrar/Registrar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Registrar {
1313
/**
1414
* the laravel backend version
1515
*/
16-
const VERSION = '1.0.16';
16+
const VERSION = '1.0.17';
1717
const VERSION_NAME = 'Alpha';
1818

1919
/**

src/Darryldecode/Backend/Components/ContentBuilder/Commands/CreateContentCommand.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Contracts\Events\Dispatcher;
1010
use Illuminate\Contracts\Validation\Factory;
1111
use Darryldecode\Backend\Utility\Helpers;
12+
use Illuminate\Contracts\Config\Repository;
1213

1314
class CreateContentCommand extends Command implements SelfHandling {
1415
/**
@@ -89,10 +90,13 @@ public function __construct($title, $body, $slug, $status, $authorId, $contentTy
8990
* @param Factory $validator
9091
* @param ContentType $contentType
9192
* @param Dispatcher $dispatcher
93+
* @param Repository $config
9294
* @return CommandResult
9395
*/
94-
public function handle(Content $content, Factory $validator, ContentType $contentType, Dispatcher $dispatcher)
96+
public function handle(Content $content, Factory $validator, ContentType $contentType, Dispatcher $dispatcher, Repository $config)
9597
{
98+
$content = $this->createContentModel($content, $config);
99+
96100
// get content available permissions
97101
try {
98102
$cType = $contentType->findOrFail($this->contentTypeId);
@@ -118,7 +122,7 @@ public function handle(Content $content, Factory $validator, ContentType $conten
118122
'slug' => $this->slug,
119123
'author_id' => $this->authorId,
120124
'content_type_id' => $this->contentTypeId,
121-
), Content::$rules);
125+
), $content::$rules);
122126

123127
if( $validationResult->fails() )
124128
{
@@ -173,4 +177,26 @@ public function handle(Content $content, Factory $validator, ContentType $conten
173177
// return response
174178
return new CommandResult(true, "Content successfully created.", $createdContent, 201);
175179
}
180+
181+
/**
182+
* @param $content \Darryldecode\Backend\Components\ContentBuilder\Models\Content
183+
* @param $config \Illuminate\Contracts\Config\Repository
184+
* @return mixed
185+
*/
186+
protected function createContentModel($content, $config)
187+
{
188+
if( ! $contentModelUsed = $config->get('backend.backend.content_model') )
189+
{
190+
return $content;
191+
};
192+
193+
$contentModelUsed = new $contentModelUsed();
194+
195+
if( $contentModelUsed instanceof Content )
196+
{
197+
return $contentModelUsed;
198+
}
199+
200+
return $content;
201+
}
176202
}

src/Darryldecode/Backend/Components/User/Commands/CreateUserCommand.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Contracts\Bus\SelfHandling;
1616
use Illuminate\Contracts\Events\Dispatcher;
1717
use Illuminate\Contracts\Validation\Factory;
18+
use Illuminate\Config\Repository;
1819

1920
class CreateUserCommand extends Command implements SelfHandling {
2021
/**
@@ -70,9 +71,10 @@ public function __construct($firstName, $lastName, $email, $password, $permissio
7071
* @param Factory $validator
7172
* @param Dispatcher $dispatcher
7273
* @param Group $group
74+
* @param Repository $config
7375
* @return CommandResult
7476
*/
75-
public function handle(User $user, Factory $validator, Dispatcher $dispatcher, Group $group)
77+
public function handle(User $user, Factory $validator, Dispatcher $dispatcher, Group $group, Repository $config)
7678
{
7779
// check user permission
7880
if( ! $this->disablePermissionChecking )
@@ -83,6 +85,9 @@ public function handle(User $user, Factory $validator, Dispatcher $dispatcher, G
8385
}
8486
}
8587

88+
// prepare the user model
89+
$user = $this->createUserModel($user, $config);
90+
8691
// validate data
8792
$validationResult = $validator->make(array(
8893
'first_name' => $this->firstName,
@@ -157,4 +162,22 @@ protected function transform($permissions)
157162
return $permissions;
158163
}
159164
}
165+
166+
/**
167+
* @param $user \Darryldecode\Backend\Components\User\Models\User
168+
* @param $config \Illuminate\Config\Repository
169+
* @return mixed
170+
*/
171+
protected function createUserModel($user, $config)
172+
{
173+
$userModelUsed = $config->get('backend.backend.user_model');
174+
$userModelUsed = new $userModelUsed();
175+
176+
if( $userModelUsed instanceof User )
177+
{
178+
return $userModelUsed;
179+
}
180+
181+
return $user;
182+
}
160183
}

tests/ContentBuilder/functional/commands/CreateContentCommandTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function testCreateContentShouldBePersistedWhenAllValidationPassed()
166166

167167
// begin
168168
$result = $this->commandDispatcher->dispatchFrom(
169-
'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateContentCommand',
169+
Darryldecode\Backend\Components\ContentBuilder\Commands\CreateContentCommand::class,
170170
$request
171171
);
172172

@@ -186,9 +186,9 @@ public function testCreateContentShouldBePersistedWhenAllValidationPassed()
186186

187187
$contentMeta = Content::parseMetaData($content->metaData->toArray());
188188

189-
$this->assertCount(2, $contentMeta);
190-
$this->assertArrayHasKey('meta1', $contentMeta);
191-
$this->assertArrayHasKey('meta2', $contentMeta);
189+
$this->assertCount(2, $contentMeta['form_1']);
190+
$this->assertArrayHasKey('meta1', $contentMeta['form_1']);
191+
$this->assertArrayHasKey('meta2', $contentMeta['form_1']);
192192
}
193193

194194
protected function createContentType()

tests/User/functional/UserTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function testValidationShouldFailIfInvalidEmailIsSupplied()
4646
{
4747
$userData = array(
4848
'first_name' => 'Darryl',
49+
'last_name' => 'Fernandez',
4950
'email' => 'Darryl',
5051
);
5152

@@ -60,13 +61,15 @@ public function testEmailShouldBeUnique()
6061
// create first our user
6162
$user1 = User::create(array(
6263
'first_name' => 'darryl',
64+
'last_name' => 'Fernandez',
6365
'email' => '[email protected]',
6466
'password' => 'pass$darryl',
6567
));
6668

6769
// now lets validate our first user with the same email
6870
$user2 = array(
6971
'first_name' => 'darryl',
72+
'last_name' => 'Fernandez',
7073
'email' => '[email protected]',
7174
'password' => 'pass$darryl',
7275
);
@@ -82,13 +85,15 @@ public function testShouldFailIfPasswordIsNotSuppliedAndShouldHaveMinimumOfEight
8285
// create first our user
8386
$user1 = User::create(array(
8487
'first_name' => 'darryl',
88+
'last_name' => 'Fernandez',
8589
'email' => '[email protected]',
8690
'password' => 'pass$darryl',
8791
));
8892

8993
// now lets validate our first user with the same email
9094
$user2 = array(
9195
'first_name' => 'jane',
96+
'last_name' => 'Fernandez',
9297
'email' => '[email protected]',
9398
'password' => 'short',
9499
);
@@ -104,6 +109,7 @@ public function testUserHasPermission()
104109
// create first our user
105110
$user1 = User::create(array(
106111
'first_name' => 'darryl',
112+
'last_name' => 'Fernandez',
107113
'email' => '[email protected]',
108114
'password' => 'pass$darryl',
109115
'permissions' => array(
@@ -123,6 +129,7 @@ public function testUserHasAnyPermission()
123129
// create first our user
124130
$user1 = User::create(array(
125131
'first_name' => 'darryl',
132+
'last_name' => 'Fernandez',
126133
'email' => '[email protected]',
127134
'password' => 'pass$darryl',
128135
'permissions' => array(
@@ -148,6 +155,7 @@ public function testUserShouldInheritPermission()
148155

149156
$user = User::create(array(
150157
'first_name' => 'darryl',
158+
'last_name' => 'Fernandez',
151159
'email' => '[email protected]',
152160
'password' => 'pass$darryl',
153161
'permissions' => array(
@@ -191,6 +199,7 @@ public function testUserPermissionsInThatBelongsToMultipleGroup()
191199

192200
$user = User::create(array(
193201
'first_name' => 'darryl',
202+
'last_name' => 'Fernandez',
194203
'email' => '[email protected]',
195204
'password' => 'pass$darryl',
196205
'permissions' => array(
@@ -247,6 +256,7 @@ public function testUserInGroup()
247256

248257
$user = User::create(array(
249258
'first_name' => 'darryl',
259+
'last_name' => 'Fernandez',
250260
'email' => '[email protected]',
251261
'password' => 'pass$darryl',
252262
'permissions' => array(

0 commit comments

Comments
 (0)