Skip to content

Commit de993d4

Browse files
committed
Simplify & fix RESTful unit tests
1 parent 172a403 commit de993d4

6 files changed

Lines changed: 451 additions & 751 deletions

File tree

system/RESTful/ResourceController.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,7 @@ public function initController(RequestInterface $request, ResponseInterface $res
7878
parent::initController($request, $response, $logger);
7979

8080
// instantiate our model, if needed
81-
if (empty($this->model) && ! empty($this->modelName))
82-
{
83-
try
84-
{
85-
$this->model = new $this->modelName;
86-
}
87-
catch (\Exception $e)
88-
{
89-
// ignored. we just don't use a model for now
90-
}
91-
}
81+
$this->setModel($this->modelName);
9282
}
9383

9484
//--------------------------------------------------------------------
@@ -166,15 +156,17 @@ public function delete($id = null)
166156
//--------------------------------------------------------------------
167157

168158
/**
169-
* Set or change the model this controller is bound to
159+
* Set or change the model this controller is bound to.
160+
* Given either the name or the object, determine the other.
170161
*
171-
* @param string|\CodeIgniter\Model $which
162+
* @param string|object $which
172163
*/
173164
public function setModel($which = null)
174165
{
166+
// save what we have been given
175167
if (! empty($which))
176168
{
177-
if ($which instanceof \CodeIgniter\Model)
169+
if (is_object($which))
178170
{
179171
$this->model = $which;
180172
}
@@ -183,6 +175,21 @@ public function setModel($which = null)
183175
$this->modelName = $which;
184176
}
185177
}
178+
179+
// make a model object if needed
180+
if (empty($this->model) && ! empty($this->modelName))
181+
{
182+
if (class_exists($this->modelName))
183+
{
184+
$this->model = new $this->modelName;
185+
}
186+
}
187+
188+
// determine model name if needed
189+
if (empty($this->modelName) && ! empty($this->model))
190+
{
191+
$this->modelName = get_class($this->model);
192+
}
186193
}
187194

188195
/**

system/RESTful/ResourcePresenter.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
namespace CodeIgniter\RESTful;
3939

40-
use CodeIgniter\API\ResponseTrait;
4140
use CodeIgniter\Controller;
4241
use CodeIgniter\HTTP\RequestInterface;
4342
use CodeIgniter\HTTP\ResponseInterface;
@@ -70,17 +69,7 @@ public function initController(RequestInterface $request, ResponseInterface $res
7069
parent::initController($request, $response, $logger);
7170

7271
// instantiate our model, if needed
73-
if (! empty($this->modelName))
74-
{
75-
try
76-
{
77-
$this->model = $this->modelName();
78-
}
79-
catch (\Exception $e)
80-
{
81-
// ignored. we just own't use a model for now
82-
}
83-
}
72+
$this->setModel($this->modelName);
8473
}
8574

8675
//--------------------------------------------------------------------
@@ -118,7 +107,8 @@ public function new()
118107
}
119108

120109
/**
121-
* Process the creation/insertion of a new resource object
110+
* Process the creation/insertion of a new resource object.
111+
* This should be a POST.
122112
*
123113
* @return string
124114
*/
@@ -161,7 +151,8 @@ public function edit($id = null)
161151
}
162152

163153
/**
164-
* Process the updating, full or partial, of a specific resource object
154+
* Process the updating, full or partial, of a specific resource object.
155+
* This should be a POST.
165156
*
166157
* @param type $id
167158
* @return string
@@ -174,13 +165,40 @@ public function update($id = null)
174165
//--------------------------------------------------------------------
175166

176167
/**
177-
* Set/change the model that this controller is bound to
168+
* Set or change the model this controller is bound to.
169+
* Given either the name or the object, determine the other.
178170
*
179-
* @param type $which
171+
* @param string|object $which
180172
*/
181173
public function setModel($which = null)
182174
{
183-
$this->model = $model;
175+
// save what we have been given
176+
if (! empty($which))
177+
{
178+
if (is_object($which))
179+
{
180+
$this->model = $which;
181+
}
182+
else
183+
{
184+
$this->modelName = $which;
185+
}
186+
}
187+
188+
// make a model object if needed
189+
if (empty($this->model) && ! empty($this->modelName))
190+
{
191+
if (class_exists($this->modelName))
192+
{
193+
$this->model = new $this->modelName;
194+
}
195+
}
196+
197+
// determine model name if needed
198+
if (empty($this->modelName) && ! empty($this->model))
199+
{
200+
$this->modelName = get_class($this->model);
201+
}
184202
}
185203

186204
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Tests\Support\RESTful;
3+
4+
use CodeIgniter\RESTful\ResourceController;
5+
6+
class MockResourceController extends ResourceController
7+
{
8+
9+
public function getModel()
10+
{
11+
return $this->model;
12+
}
13+
14+
public function getModelName()
15+
{
16+
return $this->modelName;
17+
}
18+
19+
public function getFormat()
20+
{
21+
return $this->format;
22+
}
23+
24+
}

0 commit comments

Comments
 (0)