how to properly do EcomDev PHPUnit file upload test with $_FILES?
-
29-09-2020 - |
Question
I want to test file uploading function in my module, but i stuck with limitation of is_uploaded_file and move_uploaded_file over CLI.
Trying to emulate user uploading files actions by crafting custom $_FILES array:
class Company_Module_Test_Helper_CompanyHelperTests extends EcomDev_PHPUnit_Test_Case {
public function uploadProductImagesTest() {
...
$this->assertTrue(file_exists($testFile));
$this->assertTrue(is_readable($testFile));
$_FILES = array(
...
);
$this->mockSession('customer/session');
$this->customerSession($fixtureCustomerId);
...
but when i debug, i see that move_uploaded_file in Varien_File_Uploader->_moveFile() function just return false.
I found solution to use custom namespace and override move_uploaded_file and is_uploaded_file function but when i tried to add custom namespace in test class the test won't run:
namespace CustomTest;
class Company_Module_Test_Helper_CompanyHelperTests extends \EcomDev_PHPUnit_Test_Case {
...
}
Need some advice in this case, maybe it is possible somehow ? Thanks
Solution
Don't do actual file operations in the tests, mock Varien_File_Uploader
instead. To be able to do so, I would create a getUploader()
method in a helper. Then mock this helper method using the replacement feature of EcomDev_PHPUnit to let it return the mocked uploader.