First you should change this method to non-static.
public function get_accepted_images($limit, $page, $uuid) {
try {
$conn = new pdo_connect();
$query = "SELECT...";
$result = $conn->prepare($query);
$result->bindParam(':uuid', $uuid);
$result->execute();
$array_result = $result->fetchAll(PDO::FETCH_OBJ);
$paginate_result = WS_Utility::paginate_array($array_result, $page, $limit);
$result_Set = array($paginate_result, TRUE);
} catch (Exception $e) {
$result_Set = array($e->getMessage(), FALSE);
}
return $res;
}
Next thing is work on this method with some integration testing (NOT UNIT TESTING).
When you do integration testing you don't care how the method is implemented, but you should know precisely the contract of the method (for ex: on this input ill receive this output, on this context ill receive this exception, etc etc)
Based on this knowledge of this method contract you start doing the integration tests:
class ImageDatabaseHelperIntegrationTest extends PHPUnit_Framework_TestCase
{
//The most simple test. When no images exists in the DB what happens?
public function testWhenNoImagesExistsReturnCorrectResult() {
$sut = new ImageDatabaseHelper(); //I dont know what is the name of the class
$actual = $sut->get_accepted_images(1,1,1);
$expected = array(null, FALSE); //?? I dont know what the result is. you know this
$this->assertEquals($expected, $actual);
}
//You should keep doing like this.
//The next tests will include inserting data in the database, and testing what is the output
EDIT:
user3421904 wrote: But why not static? Cant we do it like: $actual =
images::get_accepted_images(1, 1, 11);
this one works on my end as
well! Is it wrong?
Is not wrong. But if you are going to unit test other component that consumes ImageDatabaseHelper
you will not be able to mock it. Here is why:
Let's say that you are using an ImageService
class which consumes directly the get_accepted_images
static method. Like this:
class ImageService {
public function getAcceptedImages() {
$images = ImageDatabaseHelper::get_accepted_images(1,1,1);
//...
//more code
//...
return $images;
}
}
How do you test the getAcceptedImages
method without accessing the DB? You can't, right?
Now imagine that you do the method non-static.
class ImageService {
private $imageDatabaseHelper;
public function __construct(ImageDatabaseHelper $imageDatabaseHelper = null) {
if(!$imageDatabaseHelper) {
$this->imageDatabaseHelper = new ImageDatabaseHelper();
}
else {
$this->imageDatabaseHelper = $imageDatabaseHelper;
}
}
public function getAcceptedImages() {
$images = $this->imageDatabaseHelper->get_accepted_images(1,1,1);
//...
//more code
//...
return $images;
}
}
This can be tested easily!!!
class ImageServiceTest extends PHPUnit_Framework_TestCase {
protected $sut;
protected $imageDatabaseHelperDouble;
public function setUp() {
$this->imageDatabaseHelperDouble = $this->getMockBuilder('ImageDatabaseHelper')->disableOriginalConstructor()->getMock();
$this->sut = new ImageService($this->imageDatabaseHelperDouble);
}
public function testGetAcceptedImagesWhenCalledShouldCallImageDatabaseHelper() {
$this->imageDatabaseHelperDouble->expected($this->once())->method('get_accepted_images')->with(1,1,1);
$this->sut->getAcceptedImages();
}
public function testGetAcceptedImagesWhenCalledShouldReturnImages() {
$this->imageDatabaseHelperDouble->expected($this->any())->method('get_accepted_images')->will($this->returnValue(array("hi im an image", "me tooo")));
$actual = $this->sut->getAcceptedImages();
$expected = array("hi im an image", "me tooo");
$this->assertEquals($expected, $actual);
}
}
You see? The mocked ImageDatabaseHelper overrides the original ImageDatabaseHelper used by ImageService and he thinks he is using it. From this you can do whatever you want! (this also includes making ImageDatabaseHelper throw exceptions!)
Have fun!