jeudi 30 juin 2016

How to test my Request macro from within a Laravel 5 package I'm creating?

I'm currently building a Laravel package that injects a new method in Illuminate\Http\Request. The method I'm injecting has been completed and is expected to work nicely, but I also want to test it before releasing it.

My test requires me to change the request's Content-Type header, in order for me to see if the test is passing or no. So I have done the following to simulate the request:

use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
{
    /**
     * Holds the request
     * @var Illuminate\Http\Request
     */
    protected $request;

    /**
     * Setup the test
     */
    public function setUp()
    {
        parent::setUp();

        $this->request = new Request;

        $this->request->headers->set('Content-Type', 'application/x-yaml');
    }
}

Then in my test I use the method I'm injecting into Request with $this->request->myMethod() and it's always returning false since the Content-Type header is not getting set to application/x-yaml.

/** @test */
public function it_should_do_what_i_need_it_to_do()
{
    dd($this->request->myMethod()); // Return type: boolean

    $this->assertTrue($this->request->myMethod()); // It fails!
}

How do I go on simulating the headers in a test in Laravel package development?

Aucun commentaire:

Enregistrer un commentaire