I have my ApiController and in its constructor
$this->fractal = $fractal;
// Are we going to try and include embedded data?
$this->fractal->setRequestedScopes(explode(',', Input::get('embed')));
Transformer
class PlaceTransformer extends TransformerAbstract
{
protected $availableEmbeds = [
'checkins'
];
public function transform(Place $place)
{
return [
...
];
}
public function embedCheckins(Place $place)
{
$checkins = $place->checkins;
return $this->collection($checkins, new CheckinTransformer);
}
when i try to test
http://myrestapi.dev/places/3?embed=checkins
i get the following error
Call to undefined method League\Fractal\Manager::setRequestedScopes()
Ok i renamed
embed
in
include
and in my ApiController se the following lines
if (isset($_GET['include'])) {
$fractal->parseIncludes($_GET['include']);
}
instead of setRequestedScopes() and all works clear
Related
I am looking for the best way to test a call in an API I have created.
My test is:
/**
* #test
*/
public function it_can_return_a_block()
{
$block = new Block();
$block->type = "MyComp\MyOtherPackage\Widget";
$block->save();
$response = $this->call('GET', 'http://api.mysite.com/blocks?id=1');
$response->assertExactJson([
"id" => 1,
"type" => "MyComp\MyOtherPackage\Widget",
"component" => "widget",
"name" => "My Widget"
]);
}
The Block model for this uses the type attribute to instantiate a MyComp\MyOtherPackage\Widget instance which is extended from another class: MyComp\MyPackage\BlockDefintion which is abstract:
public class Block
{
...
private $definition;
public function getDefinitionAttribute()
{
if(!$this->definition)
{
$this->definition = new $this->type;
}
return $this->definition;
}
public function getNameAttribute()
{
return $this->definition->name;
}
public function getComponentAttribute()
{
return $this->definition->component;
}
...
}
This test works fine if MyComp\MyOtherPackage\Widget is real and can be instantiated but these clases will be in a different package so I want to decouple the relationship in my test.
Is there a way to create some kind of dummy class to use here? I was thinking Mockery but not sure how I can use this in this instance
In Illuminate\Support\Facades\Facade abstract In method
protected static function resolveFacadeInstance($name)
{
if (is_object($name)) {
return $name;
}
if (isset(static::$resolvedInstance[$name])) {
return static::$resolvedInstance[$name];
}
return static::$resolvedInstance[$name] = static::$app[$name];
}
static::$app is an instance of Application. And static::$app[$name] that like access value of array, and i don't understand that, What technique here?
ex: static::$app['router'] it return instance of Router. Seem that get values of protected $instances in Illuminate\Container\Container
I think it like example ? but got FATAL ERROR Uncaught Error: Cannot use object of type Foo as array
class Foo
{
public $bar = 'barValue';
}
$foo = new Foo();
echo $foo['bar'];
If you check the API of Illuminate\Container\Container, you will notice that it implements ArrayAccess and consequently the following methods.
offsetExists()
offsetGet()
offsetSet()
offsetUnset()
ArrayAccess lets you access objects as arrays. Here's a very simplistic example of a Container.
<?php
class Container implements ArrayAccess {
private $items = array();
public function __construct() {
$this->items = [
'one' => 1,
'two' => 2,
'three' => 3,
];
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->items[$offset]);
}
public function offsetUnset($offset) {
unset($this->items[$offset]);
}
public function offsetGet($offset) {
return isset($this->items[$offset]) ? $this->items[$offset] : null;
}
}
$container = new Container();
echo $container['one']; // outputs 1
$container['four'] = 4; // adds 4 to $items.
echo $container['four']; // outputs 4
As you can see, you can access the Container object as an array since it implements ArrayAccess.
It also doesn't matter if the items property is not publicly accessible. In any case, the implementation of ArrayAccess means that it will allow us to retrieve those values as if they were in an array.
Hello I have a test case which will call a route and it will return some data if the session will set.
Here is my test case
class TestControllerTest extends TestCase
{
// ...
public function testResponseOfJson()
{
$response = $this->call('GET', 'profile/test');
$this->assertEmpty( !$response );
}
// ...
}
and here is my controller
Class TestController{
public function sendResponse(Request $request){
$this->data['user_id'] = $this->request->session()->get('userdata.userid');
if($this->data['userid']){
return data;
}
else{
return Failed;
}
}
}
My routes.php
Route::get('profile/test',['uses'=>'TestController#sendResponse']);
how can i set the session variable userdata.userid and get while doing unit testing.
Please check this page.
Laravel gives you the capability of using withSession chain method and other functions that will help you test where a session is required or needs to be manipulated in some way.
Example:
<?php
class ExampleTest extends TestCase
{
public function testApplication()
{
$response = $this->withSession(['foo' => 'bar'])
->get('/');
}
}
I have a CustomController. For not to repeat myself, I defined getVars function in BaseController. I want to call getVars function from some functions in CustomController.
However, Laravel returns 404 error without any exception. What is wrong?
class BaseController extends Controller {
public function getVars($var1, $var2, $var3=Null, $var4=Null) {
return [];
}
}
class CustomController extends BaseController {
public function doBla1($var1, $var2) {
$vars = $this->getVars();
}
public function doBla2() {
$vars = $this->getVars();
}
public function doBla3() {
$vars = $this->getVars();
}
}
Sorry :( I found the reason of error. The names of doBla1 and getVars functions are same. This results in a 404 error. Sorry :(
$this is useful when you have method/function defined in same controller.
For functions inside parent controller you can use
Parent::getVars($var1, $var2)
I am working on laravel and following the tutorial http://technetlk.blogspot.com.au/2012/09/laravel-backbonejs-coffeescript_3527.html. currently working on 7th part of this tutorial.
in routes.php I have written
Route::any('api/category/(:num?)',
array('as' => 'api.category',
'uses' => 'api.category#index')
);
In api/category.php,
<?php
class Api_Category_Controller extends Base_Controller
{
public $restful = true;
public function get_index($id = null)
{
if (is_null($id ))
{
$allCats = Category::all();
return BaseModel::allToJson($allCats);
}
else
{
$cat = Category::find($id);
return $cat->toJson();
}
}
public function post_index()
{
$cat = Input::json();
$dbCat = new Category();
$dbCat->code = $cat->code;
$dbCat->name = $cat->name;
$dbCat->save();
return $dbCat->toJson();
}
public function put_index()
{
$cat = Input::json();
$dbCat = Category::find($cat->id);
$dbCat->code = $cat->code;
$dbCat->name = $cat->name;
$dbCat->save();
return $dbCat->toJson();
}
public function delete_index($id = null)
{
$dbCat = Category::find($id);
$dbCat->delete();
}
}
?>
and in BaseModel.php
<?php
class Category extends BaseModel
{
public static $table = 'tbl_category';
}
class BaseModel extends Eloquent
{
public function toJson()
{
return json_encode($this->to_array());
}
public static function allToJson($array)
{
$temp = array();
foreach($array as $t)
{
$temp[] = $t->to_array();
}
return json_encode($temp);
}
}
?>
when I am trying to run
curl -X POST http://lbc.dev/api/category -H "Content-Type: application/json" –d '{"code":"cat1", "name":"Category One"}'
I am getting the follwing error
Unhandled Exception
Message:
Class 'Category' not found Location:
C:\xampp\htdocs\NewBlog\application\controllers\api\category.php on
line 9
Your Category Model should be located in /application/models/category.php
Your BaseModel class should be located in /application/models/basemodel.php
The following is if your BaseModel class is not in a defined Autoloader directory:
Another problem you may face is the BaseModel not autoloading
correctly. This can be fixed by appending this line of code to your
start.php file found in your application folder:
// Autoloader::map
'BaseModel' => path('app').'/path/to/basemodel.php',
U have to specify the model in controller file before using it
Add this line above the following line in your controller file
use App\Category;
class Api_Category_Controller extends Base_Controller
{