I'm trying to write a test of this class.
class Page
{
private function getPage(){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://example.com/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return $response;
}
public function getTitle()
{
$page = $this->getPage();
$re = '/(<title>)(.*?)(<\/title>)/m';
preg_match_all($re, $page, $matches, PREG_SET_ORDER, 0);
return $matches[0][2];
}
}
And since my method getPage accesses an external resource, I need to build for it Mock
namespace Tests;
use PHPUnit\Framework\TestCase;
use App\Kernel\Page;
class PageTest extends TestCase
{
public function testGetTitle()
{
$stub = $this->createMock(Page::class);
$stub->method('getPage')
->willReturn('<title>Example Domain</title>');
$this->assertSame('Example Domain', $stub->getTitle());
}
}
When I call the test I get an error:
1) Tests\PageTest::testGetTitle Trying to configure method "getPage" which cannot be configured because it does not exist, has not been specified, is final, or is static
I watched the testing examples there they say to do so, but why the error occurs, I don’t remember - can anyone tell me how to test such a code?