Skip to content

Commit 29a4697

Browse files
committed
added command for querying single content together with its tests
1 parent fffa4b1 commit 29a4697

File tree

3 files changed

+307
-1
lines changed

3 files changed

+307
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Components\ContentBuilder\Commands;
4+
5+
use Darryldecode\Backend\Base\Commands\Command;
6+
use Darryldecode\Backend\Base\Commands\CommandResult;
7+
use Darryldecode\Backend\Components\ContentBuilder\Models\Content;
8+
use Darryldecode\Backend\Components\ContentBuilder\Models\ContentType;
9+
use Illuminate\Contracts\Bus\SelfHandling;
10+
use Illuminate\Contracts\Events\Dispatcher;
11+
12+
class QueryContentCommand extends Command implements SelfHandling {
13+
/**
14+
* @var null
15+
*/
16+
private $id;
17+
/**
18+
* @var null
19+
*/
20+
private $slug;
21+
/**
22+
* @var null
23+
*/
24+
private $title;
25+
26+
/**
27+
* @param null $id
28+
* @param null $slug
29+
* @param null $title
30+
* @param bool $disablePermissionChecking
31+
*/
32+
public function __construct($id = null, $slug = null, $title = null, $disablePermissionChecking = false)
33+
{
34+
parent::__construct();
35+
$this->id = $id;
36+
$this->slug = $slug;
37+
$this->title = $title;
38+
$this->args = func_get_args();
39+
$this->disablePermissionChecking = $disablePermissionChecking;
40+
}
41+
42+
/**
43+
* @param Dispatcher $dispatcher
44+
* @param ContentType $contentType
45+
* @param Content $content
46+
* @return CommandResult
47+
*/
48+
public function handle(Dispatcher $dispatcher, ContentType $contentType, Content $content)
49+
{
50+
// fire before query
51+
$dispatcher->fire('content.beforeQuery', array($this->args));
52+
53+
// query
54+
$results = $this->query($contentType, $content);
55+
56+
// fire after query
57+
$dispatcher->fire('content.afterQuery', array($results));
58+
59+
// all good
60+
return new CommandResult(true, "Query content successful.", $results, 200);
61+
}
62+
63+
/**
64+
* @param $contentType \Darryldecode\Backend\Components\ContentBuilder\Models\ContentType
65+
* @param $content \Darryldecode\Backend\Components\ContentBuilder\Models\Content
66+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
67+
*/
68+
private function query($contentType, $content)
69+
{
70+
$q = $content->with(array(
71+
'terms',
72+
'author',
73+
'metaData',
74+
'type.formGroups',
75+
'revisions',
76+
'type'
77+
));
78+
79+
if( !is_null($this->id) && ($this->id != '') )
80+
{
81+
return $q->find($this->id);
82+
}
83+
else
84+
{
85+
return $q->ofSlug($this->slug)->ofTitle($this->title)->first();
86+
}
87+
}
88+
}

src/Darryldecode/Backend/Components/ContentBuilder/Models/Content.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,6 @@ public function scopeOfTitle($query, $title)
168168
public function scopeOfSlug($query, $slug)
169169
{
170170
if( $slug === false || $slug === null ) return false;
171-
return $query->where('slug','>=',$slug);
171+
return $query->where('slug','=',$slug);
172172
}
173173
}
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
<?php
2+
3+
use Darryldecode\Backend\Components\ContentBuilder\Models\Content;
4+
use Darryldecode\Backend\Components\ContentBuilder\Models\ContentType;
5+
use Darryldecode\Backend\Components\User\Models\Group;
6+
use Darryldecode\Backend\Components\User\Models\User;
7+
use Faker\Factory as Faker;
8+
use Illuminate\Http\Request;
9+
10+
class QueryContentCommandTest extends TestCase {
11+
12+
protected $application;
13+
14+
protected $faker;
15+
16+
/**
17+
* @var Illuminate\Contracts\Bus\Dispatcher
18+
*/
19+
protected $commandDispatcher;
20+
21+
public function setUp()
22+
{
23+
$this->faker = Faker::create();
24+
$this->application = $this->createApplication();
25+
$this->application['config']->set('database.connections.sqlite.database',':memory:');
26+
$this->application['config']->set('session.driver','array');
27+
$this->application['db']->setDefaultConnection('sqlite');
28+
$this->application->make('Illuminate\Contracts\Console\Kernel')->call('migrate');
29+
$this->commandDispatcher = $this->application->make('Illuminate\Contracts\Bus\Dispatcher');
30+
31+
$this->createDummyData($this->createUserAndLoggedIn());
32+
}
33+
34+
public function tearDown()
35+
{
36+
37+
}
38+
39+
public function testQueryByID()
40+
{
41+
// begin
42+
$result = $this->commandDispatcher->dispatchFromArray(
43+
'Darryldecode\Backend\Components\ContentBuilder\Commands\QueryContentCommand',
44+
array(
45+
'id' => 1,
46+
)
47+
);
48+
49+
// prove successful
50+
$this->assertTrue($result->isSuccessful());
51+
$this->assertEquals('Query content successful.', $result->getMessage());
52+
$this->assertEquals(200, $result->getStatusCode(), 'Status code should be ok');
53+
54+
$this->assertEquals('Some Title',$result->getData()->toArray()['title']);
55+
}
56+
57+
public function testQueryBySlug()
58+
{
59+
// begin
60+
$result = $this->commandDispatcher->dispatchFromArray(
61+
'Darryldecode\Backend\Components\ContentBuilder\Commands\QueryContentCommand',
62+
array(
63+
'slug' => 'some-entry-2',
64+
)
65+
);
66+
67+
// prove successful
68+
$this->assertTrue($result->isSuccessful());
69+
$this->assertEquals('Query content successful.', $result->getMessage());
70+
$this->assertEquals(200, $result->getStatusCode(), 'Status code should be ok');
71+
72+
$this->assertEquals('Some Entry 2',$result->getData()->toArray()['title']);
73+
}
74+
75+
public function testQueryByTitleSearch()
76+
{
77+
// begin
78+
$result = $this->commandDispatcher->dispatchFromArray(
79+
'Darryldecode\Backend\Components\ContentBuilder\Commands\QueryContentCommand',
80+
array(
81+
'title' => 'Entry 3',
82+
)
83+
);
84+
85+
// prove successful
86+
$this->assertTrue($result->isSuccessful());
87+
$this->assertEquals('Query content successful.', $result->getMessage());
88+
$this->assertEquals(200, $result->getStatusCode(), 'Status code should be ok');
89+
90+
$this->assertEquals('Some Entry 3',$result->getData()->toArray()['title']);
91+
}
92+
93+
protected function createUserAndLoggedIn()
94+
{
95+
// create user and logged in (the user who will perform the action)
96+
$user = User::create(array(
97+
'first_name' => 'dianne',
98+
'email' => 'dianne@gmail.com',
99+
'password' => 'pass$dianne',
100+
'permissions' => array(
101+
'superuser' => 1
102+
)
103+
));
104+
105+
// logged in the user
106+
$this->application['auth']->loginUsingId($user->id);
107+
108+
return $user;
109+
}
110+
111+
112+
protected function createDummyData($user)
113+
{
114+
// create content type
115+
$ctype = ContentType::create(array(
116+
'type' => 'Blog',
117+
'enable_revisions' => true
118+
));
119+
120+
// create content type taxonomy
121+
$taxonomy = $this->commandDispatcher->dispatchFromArray(
122+
'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateContentTypeTaxonomyCommand',
123+
array(
124+
'taxonomy' => 'category',
125+
'description' => 'the blog post category',
126+
'contentTypeId' => $ctype->id,
127+
)
128+
);
129+
$taxonomy = $taxonomy->getData()->toArray();
130+
131+
// create taxonomy terms
132+
$technologyTerm = $this->commandDispatcher->dispatchFromArray(
133+
'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateTypeTaxonomyTerm',
134+
array(
135+
'term' => 'technology',
136+
'slug' => 'technology',
137+
'contentTypeTaxonomyId' => $taxonomy['id'],
138+
)
139+
);
140+
$technologyTerm = $technologyTerm->getData()->toArray();
141+
142+
$healthTerm = $this->commandDispatcher->dispatchFromArray(
143+
'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateTypeTaxonomyTerm',
144+
array(
145+
'term' => 'health',
146+
'slug' => 'health',
147+
'contentTypeTaxonomyId' => $taxonomy['id'],
148+
)
149+
);
150+
$healthTerm = $healthTerm->getData()->toArray();
151+
152+
$programmingTerm = $this->commandDispatcher->dispatchFromArray(
153+
'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateTypeTaxonomyTerm',
154+
array(
155+
'term' => 'programming',
156+
'slug' => 'programming',
157+
'contentTypeTaxonomyId' => $taxonomy['id'],
158+
)
159+
);
160+
$programmingTerm = $programmingTerm->getData()->toArray();
161+
162+
// create blog post dummy contents
163+
// this will have ID of 1, we will use this on test query by ID
164+
$this->commandDispatcher->dispatchFromArray(
165+
'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateContentCommand',
166+
array(
167+
'title' => 'Some Title',
168+
'body' => 'Some Body',
169+
'slug' => 'some-title',
170+
'status' => 'published',
171+
'authorId' => $user->id,
172+
'contentTypeId' => $ctype->id,
173+
'taxonomies' => array($healthTerm['id']=>true),
174+
'metaData' => array('form_1' => array(
175+
'meta1' => 'meta value 1',
176+
'meta2' => 'meta value 2',
177+
)),
178+
'miscData' => array()
179+
)
180+
);
181+
182+
$this->commandDispatcher->dispatchFromArray(
183+
'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateContentCommand',
184+
array(
185+
'title' => 'Some Entry 2',
186+
'body' => 'Some entry entry',
187+
'slug' => 'some-entry-2',
188+
'status' => 'published',
189+
'authorId' => $user->id,
190+
'contentTypeId' => $ctype->id,
191+
'taxonomies' => array($programmingTerm['id']=>true, $healthTerm['id']=>true),
192+
'metaData' => array('form_1' => array(
193+
'meta1' => 'meta value 1',
194+
'meta2' => 'meta value 2',
195+
)),
196+
'miscData' => array()
197+
)
198+
);
199+
200+
$this->commandDispatcher->dispatchFromArray(
201+
'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateContentCommand',
202+
array(
203+
'title' => 'Some Entry 3',
204+
'body' => 'Some entry entry 3',
205+
'slug' => 'some-entry-3',
206+
'status' => 'published',
207+
'authorId' => $user->id,
208+
'contentTypeId' => $ctype->id,
209+
'taxonomies' => array(),
210+
'metaData' => array('form_1' => array(
211+
'meta1' => 'meta value 1',
212+
'meta2' => 'meta value 2',
213+
)),
214+
'miscData' => array()
215+
)
216+
);
217+
}
218+
}

0 commit comments

Comments
 (0)