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