dongpang1898 2013-09-11 17:33
浏览 58
已采纳

在PHPUnit中测试工作链接

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);

  • 写回答

2条回答 默认 最新

  • douji9184 2013-09-11 19:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分