Domanda

I am realy new to PHPUnit, and for my first self-made test case, I would like to check if a website has an image, by providing a link to PHPUnit like this.

lets call link I want to test http://somedomain.com/images/images.jpg

Now, I wrote this functions, and I know I am probably doing it very wrong, but that is why I am asking it here.

class WebsiteHasImage{
    public function images(){
        $website = 'http://somedomain.com/images/img.jpg';
        return $website;
    }
}


class WebsiteHasImageTest extends PHPUnit_Framework_TestCase
{
    public function testSuccess(){
      $this->assertTrue($this->images(), TRUE);
    }
}


 Call to undefined method WebsiteHasImageTest::images() in ... on line 15

line 15 is this. $this->assertTrue($this->images(), TRUE);

È stato utile?

Soluzione

Your class will return a string for the images() which is not what you are really testing.

class WebsiteHasImage{
    public function images(){
        $website = 'http://somedomain.com/images/img.jpg';
        return $website;
    }
}

class WebsiteHasImageTest extends PHPUnit_Framework_TestCase
{
    public function testSuccess()
    {
        WebSiteImage = new WebSiteHasImage();
        $this->assertEquals('http://somedomain.com/images/img.jpg', $WebSiteImage->images(), 'Check returned File Name is correct');
    }
}

All this has done is test that the image string is correct.

You could try and read the image with another call, and then compare it, but that would require that your website is up.

Optionally, the call could look into the current application directory for images/img.jpg and do some tests (byte size is proper, etc...) to validate an image.

You could attempt to read the website and return the contents, but again, that requires the website. So to test your code, you have a class that returns the website data, and then mock that class to return a fixed string, that you would then write tests against to see that the code in your class does extract the images correctly. Your class could then test the image exists, and again, mock that to always return it does to test what your code does.

public function GetWebPageContents($HTTPAddress)
{
    $HTML = ...    // Code that reads the contents
    return $HTML;
}

public function GetImageList($HTML)
{
    $ImageList = array();
    ...      // Code to find all images, adding to ImageList
    return $ImageList;
}

Test class additions

public function testGetWebPageContents()
{
$HTMLText = '<html><body><img scr=""></img></body><</html>';        // Fill this with a sample webpage text
    $MockClass = $this->getMock('WebsiteHasImage', array('GetWebPageContents'));

    // Set up the expectation for the GetWebPageContents() method 
    $MockClass->expects($this->any())
                ->method('GetWebPageContents')
                ->will($this->returnValue($HMTLText));

    // Create Test Object
    $TestClass = new MockClass();
    $this->assertEquals($HTMLText, $TestClass->GetWebPageContents('http://testaddress.com'), 'Validate Mock HTML return'); // Address is garbage as it does not matter since we mocked it

    $WebsiteTest = new WebsiteHasImage();
    $ImageList = $WebsiteTest->GetImageList($HTMLText);
    $this->assertEquals(1, count($ImageList), 'Count Total Number of Images');
    $this->assertEquals('images/img.jpg', $ImageList[0], 'Check actual image file name from dummy HTML parsing');

    ...       // More tests for contents
}

Now, the best change is to use the read data within your class, and for that, you then pass in the HTML code to the class normally after reading it from the website. In your test, you would then pass the object that does the reading in using the Mock object in the constructor, or via a Set. Do a Google search for Dependency Injection, Mock Objects, etc... to find more information on this topic.

Altri suggerimenti

I havent worked with PHPunit myself but looking at your code $this->images() wont work because you don't have a images method in the WebsiteHasImageTest class.

<?php
class WebsiteHasImageTest extends PHPUnit_Framework_TestCase
{
    public function images(){
        $website = 'http://somedomain.com/images/img.jpg';
        return $website;
    }
    public function testSuccess(){
      $this->assertTrue($this->images(), TRUE);
    }
}

This will remove the need for WebsiteHasImage, but if it is required you can do.

<?php
class WebsiteHasImage{
    public function images(){
        $website = 'http://somedomain.com/images/img.jpg';
        return $website;
    }
}
class WebsiteHasImageTest extends PHPUnit_Framework_TestCase
{
    public function testSuccess(){
      $images = new WebsiteHasImage();
      $this->assertTrue($images->images(), TRUE);
    }
}

So it just comes down to: a) Move your method to the WebsiteHasImageTest class or b) iniate a new WebsitehasImage() object in your WebsiteHasImageTest testSuccess method.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top