how to unit test curl call in php

后端 未结 6 1449
既然无缘
既然无缘 2020-12-12 20:41

How would you go about unit testing a curl implementation?

  public function get() {
    $ch = curl_init($this->request->getUrl());

    curl_setopt($c         


        
6条回答
  •  情深已故
    2020-12-12 21:10

    You might use a function mock library. I made one for you: php-mock-phpunit

    namespace foo;
    
    use phpmock\phpunit\PHPMock;
    
    class BuiltinTest extends \PHPUnit_Framework_TestCase
    {
    
        use PHPMock;
    
        public function testCurl()
        {
            $curl_exec = $this->getFunctionMock(__NAMESPACE__, "curl_exec");
            $curl_exec->expects($this->once())->willReturn("body");
    
            $ch = curl_init();
            $this->assertEquals("body", curl_exec($ch));
        }
    }
    

提交回复
热议问题