First of all i'm using codeigniter as my PHP Framework. Sorry for asking this, i'm trying to grasp the idea of unit testing. They said that its a good practice to do unit testing before developing your site functionality. So here I am eager to learn this:
My method:
function download()
{
$data = file_get_contents("resources/temp/file.doc");
$name = "new_file.doc";
force_download($name, $data);
}
Then I created a test controller with a testDownload method:
public function testDownload()
{
//Call the controller method
$this->CI->download();
//Fetch the buffered output
$out = output();
$this->assertFalse(preg_match('/(error|notice)/i', $out));
}
Then I test it: phpunit test.php and gave me this:
F[CIUnit] PHP Error: Warning - file_get_contents(resources/temp/file.doc): failed to open stream: No such file or directory File Path: controllers/contract.php (line: 22)
Time: 4 seconds, Memory: 7.50Mb
There was 1 failure:
1) ContractTest::testDownload
Failed asserting that 0 is false.
I just want to test whether it has the correct path. or maybe do I really need a test script for downloading a file? Sorry, im just confused.
Reason for "No such file or directory" :
When you are testing using PHPUnit, the current working directory will be relative to the directory where you are running the phpunit. But in the code, you would have writtern relative to your webserver directory.
Btw, unit test should test the code you have written. Testing the external resource in unit testing does not seem to be good idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With