1

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

1 Answer 1

0

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.

2
  • Thanks you for reply, but i tried to mock the Varien_File_Uploader , filled it with my custom file data, and got exception in method _setUploadFileId: if (empty($_FILES)) { throw new Exception('$_FILES array is empty', self::TMP_NAME_EMPTY); } maybe i did not fully understnad you, would be much gratituded for details. do you mean i should totally replace Varien_File_Uploader with my custom class (extended from Varien_File_Uploader for example) ?
    – Ilja
    Commented Mar 28, 2016 at 10:43
  • 1
    Then you did not mock the public methods and the original methods are called. Read the mock chapter of the phpunit manual to learn more about mocking methods. Commented Mar 28, 2016 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.