Mocking core PHP functions is a little bit tricky.
I guess you have something like this in your posts model.
public function processFile($file)
{
if (is_uploaded_file($file)) {
//process the file
return true;
}
return false;
}
And you have a corresponding test like this.
public function testProcessFile()
{
$actual = $this->Posts->processFile('noFile');
$this->assertTrue($actual);
}
As you do not upload anything during the test process, the test will always fail.
You should add a second namespace at the begining of your PostsTableTest.php, even having more namespaces in a single file is a bad practice.
<?php
namespace {
// This allows us to configure the behavior of the "global mock"
// by changing its value you switch between the core PHP function and
// your implementation
$mockIsUploadedFile = false;
}
Than you should have your original namespace declaration in curly bracket format.
namespace App\Model\Table {
And you can add the PHP core method to be overwritten
function is_uploaded_file()
{
global $mockIsUploadedFile;
if ($mockIsUploadedFile === true) {
return true;
} else {
return call_user_func_array('\is_uploaded_file',func_get_args());
}
}
//other model methods
} //this closes the second namespace declaration
More on CakePHP unit testing here: http://www.apress.com/9781484212134