flyingluscas's avatar

How to test a class that has dependencies injected by the Laravel service container?

Hi guys, I'am new at Laracasts, sorry if this is a silly doubt but I dont know how to write a test for a method in a class that has dependecies injected by the Laravel service container, should I mock the depenencies? or Laravel already provide an easier way to do that?

This is my class

namespace App\Support;

use App\Transformers\Transform;
use League\Fractal\TransformerAbstract;
use Illuminate\Contracts\Routing\ResponseFactory;
use Symfony\Component\HttpFoundation\Response as HttpResponse;

class Response
{
    /**
     * HTTP Response.
     *
     * @var \Illuminate\Contracts\Routing\ResponseFactory
     */
    private $response;

    /**
     * API transformer helper.
     *
     * @var \App\Transformers\Transform
     */
    public $transform;

    /**
     * HTTP status code.
     *
     * @var int
     */
    private $statusCode = HttpResponse::HTTP_OK;

    /**
     * Create a new class instance.
     *
     * @param ResponseFactory $response
     * @param Transform       $transform
     */
    public function __construct(ResponseFactory $response, Transform $transform)
    {
        $this->response = $response;
        $this->transform = $transform;
    }

    /**
     * Make an error response.
     *
     * @param  mixed $message
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function withError($message)
    {
        return $this->json([
            'messages' => (is_array($message) ? $message : [$message]),
        ]);
    }

    /**
     * Make a JSON response with the transformed item.
     *
     * @param  mixed               $item
     * @param  TransformerAbstract $transformer
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function item($item, TransformerAbstract $transformer)
    {
        return $this->json(
            $this->transform->item($item, $transformer)
        );
    }

    /**
     * Make a JSON response with the transformed items.
     *
     * @param  mixed               $items
     * @param  TransformerAbstract $transformer
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function collection($items, TransformerAbstract $transformer)
    {
        return $this->json(
            $this->transform->collection($items, $transformer)
        );
    }

    /**
     * Make a JSON response.
     *
     * @param  mixed  $data
     * @param  array  $headers
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function json($data = [], array $headers = [])
    {
        return $this->response->json($data, $this->statusCode, $headers);
    }

    /**
     * Set HTTP status code.
     *
     * @param int $statusCode
     *
     * @return self
     */
    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;

        return $this;
    }

    /**
     * Gets the HTTP status code.
     *
     * @return int
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }
}
0 likes
2 replies
DarkRoast's avatar

In this case I would be ok with just using resolve() (or App::make()) to fetch the class from the container with its dependencies then running tests against that. You don't have to unit test everything in isolation.

2 likes

Please or to participate in this conversation.