Hi everyone im trying to test my ServiceImplement. Im using polymorphic relation ..
My service its :
public function findAll($class, $classId, ApiRequest $request = null);
public function save(Comment $comment);
My ServiceImplement :
public function findAll($class, $classId, ApiRequest $request = null)
{
$comment = Comment::where('commentable_type', $class)->where('commentable_id', $classId)->get();
if ($request != null) {
$comment->load($request->getRelations());
}
return $comment;
}
public function save(Comment $comment)
{
$model = NamespaceConstants::NAMESPACE_CONST . $comment->commentable_type;
$rules = [
'comment' => 'required|max:2000',
'commentable_id' => 'required|exists:' . $model::getModel()->getTable() . ',id',
'commentable_type' => 'required|max:255',
];
$validator = \Validator::make($comment->getAttributes(), $rules);
if ($validator->passes()) {
$comment->timestamp = date(DateConstants::DATETIME, strtotime(date(DateConstants::DATETIME) . "+2 hours"));
$comment->user_id = \Auth::user()->id;
$comment->save();
return $comment;
}
return \Response::json($validator->errors()->toArray(), 422);
}
Im trying to write unit test but i have this error
ErrorException: Trying to get property of non-object
This is my code that i use to test my serviceimlpement
public function testFindAll()
{
$user = new User();
$user->id = 1;
$user->login = 'test@test.com';
$user->password = 'password';
$user->active = 'true';
$userService = new UserServiceImpl();
$userService->save($user);
$product = new Product();
$product->id = 1;
$product->name = 'test';
$productService = new ProductServiceImpl();
$productService->save($product);
$comment = new Comment();
$comment->comment = 'test test';
$comment->timestamp = date(DateConstants::DATETIME, strtotime(date(DateConstants::DATETIME) . "+2 hours"));;
$comment->user_id = $user->id;
// polymorphic
$comment->commentable_id = $product->id;
$comment->commentable_type = 'Product';
$commentService = new CommentServiceImpl();
$commentService->save($comment);
// Act
$comment = $commentService->findAll($comment->commentable_type, $comment->commentable_id);
// Assert
$this->assertTrue(count($comment) > 0);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire