Reputation: 134
I am trying to test a procedural PHP file that begins with the if statement:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
then proceeds to use the superglobal $_POST
to get the variable which is then used in an SQL query.
Below is my PHPUnit code that I am trying to use to test the procedural file above:
<?php
use PHPUnit\Framework\TestCase;
require '../public_html/PHP/dbconfig.php';
//require '../public_html/PHP/getters/getBranchHeaderPhoto';
class getBranchHeaderPhotoTest extends TestCase {
protected function setUp()
{
parent::setUp();
$_POST = array();
}
/**
* @test
*/
public function checkThatGetBranchHeaderPhotoReturnsTheDirectoryAndImage() {
$_SERVER["REQUEST_METHOD"] == "POST";
$_POST = array("all");
ob_start();
include('../public_html/PHP/getters/getBranchHeaderPhoto.php');
$contents = ob_get_contents();
$this->assertNotNull($contents);
}
}
?>
However, when I try and run the test in the command line, running the following cmd phpunit getBranchHeaderPhotoTest.php
, the following error occurs;
There was 1 error:
1) getBranchHeaderPhotoTest::checkThatGetBranchHeaderPhotoReturnsTheDirectoryAndImage
Undefined index: REQUEST_METHOD
C:\Users\User\Documents\project\project\test\getBranchHeaderPhotoTest.php:21
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
I have tried following previous SO answers on this topic but can not pass a request method into the procedural file. Is this possible?
Upvotes: 1
Views: 9565
Reputation: 39364
You're using ==
(comparison) when you want =
(assignment). Change:
$_SERVER["REQUEST_METHOD"] == "POST";
to:
$_SERVER["REQUEST_METHOD"] = "POST";
If possible, fix the offending code to check the key exists, like with:
if (($_SERVER["REQUEST_METHOD"] ?? 'GET') == 'POST')
Upvotes: 4