Single cookie testing in PHP

The default practice for unit testing based on session / cookie information is to use an abstraction library. But what if I want to write and unit test this abstraction library? The documentation for the PHP function says that the cookie will be available at the next request. Using the command line tool for testing, there is no such thing as a “request”. So, how can I unit test specify the correct cookie settings? setcookie

I want to check if all function parameters are set correctly by setcookiemy abstraction library. These parameters will be set in accordance with certain conditions and method calls.

The only solution I can think of is to make fun of a function setcookiewith an extension runkitthat I don't want to install. Other ideas?

+3
source share
3 answers

I found another very simple solution: the class wrapper around the PHP function setcookie, which is so simple, it does not need to be tested for unity:

/**
 * Wrapper around setcookie function for better testability
 */ 
class Cookiesetter {
  public function setcookie($name, $value = "",  $expire = 0,  $path = "", 
    $domain = "", $secure = false, $httponly = false) {
    return setcookie($name, $value,  $expire, $path, $domain, $secure, $httponly);
  }
}

Then the method setcookiecan be ridiculed. This has the added benefit that I can implement other methods, such as expireCookie.

+9
source

You can set the value directly to the current $ _COOKIE array:

<?php
$_COOKIE['test']='hello';
print_r($_COOKIE);
run_tests();

( CLI). , $_REQUEST .

, cookie, , :

<?php
$_SESSION['auth_user']='root';
run_tests();

, , , .

+2

selenium ide. , phpunit , .

-2

Source: https://habr.com/ru/post/1781419/


All Articles