I have a class which has a method that makes use of PHP's global file_get_contents
function. I need to test the method on the class, without actually calling the global function.
I know that I could use namespaces to override what gets returned from file_get_contents
, however my tests are in a separate namespace already so I cannot simply match namespaces with the class.
Here's some code:
The class
<?php
namespace MyVendor\MyProject;
class MyClass
{
public function myMethod()
{
$request = 'http://domain.com';
$response = $this->someMethodUsingGlobals($request);
// Do something with the response..
}
private function someMethodUsingGlobals($url)
{
return json_decode(file_get_contents($url),true)['results'][0];
}
}
The test
<?php
namespace MyProjectTests;
public function test_it_does_something_with_the_response()
{
$sut = new MyClass();
$response = $sut->myMethod();
$this->assertEquals('Some expectation', $response);
}
I need to mock the someMethodUsingGlobals()
method on the class, but not entirely sure how to go about this.
Aucun commentaire:
Enregistrer un commentaire