mercredi 12 octobre 2016

Laravel's FormRequest don't have 'method' attribute when testing with phpunit

I'm building a package that offers some URLs. I have a problem that I don't know if is an Laravel issue, a phpunit issue or my issue. In a controller I have this:

class SellersController extends BaseController
{
    public function store(SellerRequest $request)
    {
        $seller = Seller::create($request->all());
        return $this->respond(['data' => ['id' => $seller->id]]);
    }
}

Next, just for testing, because I have errors when I run my tests in phpunit, I put this in SellerRequest:

public function rules()
{
    dd("method: " . $this->method);

    switch ($this->method) {
        case 'POST':
            ....
    ....
            break;

        case 'PATCH':
        case 'PUT':
            ....
    ....
            break;

    }
}

Now the thing is, when I visit the url for the "store" method through Postman, I have: "method: POST". But when the test visit the same URL the result is "method: ". I'm not sure if I'm missing something, but it seems like when the request come from phpunit, the FormRequest can't know the method. My test looks like this:

/** @test */
public function it_creates_a_seller()
{
    $this->json('POST', $this->testBaseUrl, $this->sellerData())
        ->seeJsonStructure([
            'data' => ['id']
        ])
        ;
}

I appreciate any help

Aucun commentaire:

Enregistrer un commentaire