Last active
December 16, 2015 23:59
-
-
Save AmyStephen/5517599 to your computer and use it in GitHub Desktop.
I ran into a couple of challenges for how to unit test my FileUpload package.: mocking $_FILES and PHP function "move_uploaded_file." What I ended up doing was establishing a property named $file_array in my upload class. In the constructor, I set that property to the value contained in $_FILES. Then, I allowed it to be overridden by the value i…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* $files_array contains $_FILE superglobal | |
* | |
* Helps with Unit Testing | |
* | |
* @var array | |
*/ | |
protected $file_array = array(); | |
/** | |
* Constructor | |
* | |
* @param array $options | |
* | |
* @since 1.0 | |
*/ | |
public function __construct($options = array()) | |
{ | |
$this->file_array = $_FILES; | |
if (isset($options['file_array'])) { | |
$this->file_array = $options['file_array']; | |
} | |
//etc... | |
} | |
//etc... | |
} | |
/** | |
* Placed this in my Unit Test Class to override the PHP function | |
*/ | |
function move_uploaded_file($temporary_name, $target_path) | |
{ | |
//override PHP's function here... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And what happens when you run it?