gazd1977's avatar

Accessing Cookie in PHPunit Test

Hi Everyone,

I'm trying my hand with phpunit and trying to test cookies, but i am having difficulty accessing the cookie to check its value.

In my test i post to a controller that performs some logic, queues a cookie and then redirects. I then access another controller via a get request where i am trying to access the cookie to compare its value against a token stored in my database.

The cookie 'appears' (in inverted commas) to be being set, as when i dump the response i can see the cookie and it has a value (as below).

...Illuminate\Foundation\Testing\TestResponse^ {#3006
  +baseResponse: Illuminate\Http\Response^ {#3072
    +headers: Symfony\Component\HttpFoundation\ResponseHeaderBag^ {#3018
      #computedCacheControl: array:2 [
        "no-cache" => true
        "private" => true
      ]
      #cookies: array:1 [
        "" => array:1 [
          "/" => array:2 [
            "XSRF-TOKEN" => Symfony\Component\HttpFoundation\Cookie^ {#3074
              #name: "XSRF-TOKEN"
              #value: "eyJpdiI6ImpmQXZrU1VpWnNJSzJFNU5jTWhJd3c9PSIsInZhbHVlIjoiMThadHZ5c3JWd1QraXNjaTlKYUNpMGhDMitYVzNnSzVcLzIrZGh3Uk83ZU1cL3lSUHNIMlpET01EU1wvdnZOSTJ6dyIsIm1hYyI6IjIxMzY1ZTZhZTc3NTM4MjZmZDVlOWUyYzNjZWZkZGIyMmQ0MDM2MGIyNmI1MDlmNWJkZDU3NDY5NmFmMGViZjgifQ=="
              #domain: null
              #expire: 1573426758
              #path: "/"
              #secure: false
              #httpOnly: false
              -raw: false
              -sameSite: null
              -secureDefault: false
            }
            "twoFactor" => Symfony\Component\HttpFoundation\Cookie^ {#3071
              #name: "twoFactor"
              #value: "eyJpdiI6IkVBSEhhOHlrM1RmUW1ZMmcyUzlKS0E9PSIsInZhbHVlIjoiWU91S01GcjV4UVJBdnlRN2FVdTJcL0V6Yjg4SEx3OExVZWdaRWpydjEweklBNHU1Z2I0djZBTHBhNWo2eVZcL2tiS0VvaHZUdGtkQTZiVExZaFwvOHRZTSs0d05WdWIzXC9oWklhREo1ODJLYmJWbnNqVDRtTXJBNVA3WDhjbGtqeDVOZ1ZheW1LeHFRaGF4RUZaRXlRZ3g4SHpvbExGSEVGTlhseWVqbllSNlE4UUh2Z1BvUnd1bjdEZXNcL2RHQ2hPdmgiLCJtYWMiOiI0MGY2YzYxMTgyNzk5YWFlZTgyY2U5MmExMzczYTliMDEzNGI1MWVlMDk1ZGE1NmRkZjE3Zjk4MWRhMzY3ODc0In0="
              #domain: null
              #expire: 1576011558
              #path: "/"
              #secure: false
              #httpOnly: true
              -raw: false
              -sameSite: null
              -secureDefault: false
            }
          ]
        ]
      ]

On the get controller i am (until i can fathom out where I'm going wrong) just trying to return the cookie value

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;

class TwoFactorEntryController extends Controller
{
    public function handle(Request $request)
    {
        return Cookie::get('twoFactor'); // i've also tried $request->cookie('twoFactor');
    }    
   
}

When i dump the response i was expecting the content to have the cookie value, but its returning empty.

I assume I'm not passing the cookie properly or that because there is no browser cookies don't work in quite the same way.

Any pointers would be gratefully received.

Thanks in advance.

0 likes
2 replies
gazd1977's avatar

If i try the below test

$this->post('two-factor-setup', [
'otp' => $otp,
'remember' => $remember
]);

$fetch = $this->get('two-factor-entry);
$fetch->assertCookie('twoFactor');

The assertion comes back true, but trying to return the cookie value on the get request returns null.

gazd1977's avatar

Just in case it helps anyone else, i have managed to get a 'dirty' way to at least mock the existence of a cookie,

Rather than use $this->get, you can use $this->call, which accepts an array of cookies as the 4th parameter.

$this->call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)

Taking the cookie value from the header of my earlier POST request i then decrypt to get the token value and then create a mocked cookie i can pass as the 4th param

Please or to participate in this conversation.