1. Go to this page and download the library: Download robvanaarle/php-object-seam library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
robvanaarle / php-object-seam example snippets
class TemperatureApi
{
public function getCurrentTemperature(string $location): float
{
$weatherData = $this->getWeatherData($location);
if ('unknown_location' === $weatherData['error']) {
throw new \InvalidArgumentException("Unknown location: {$location}");
}
if (null !== $weatherData['error']) {
throw new \RuntimeException("Weather API error: {$weatherData['error']}");
}
return $this->fahrenheitToCelsius($weatherData['current']['temp_f']);
}
private function fahrenheitToCelsius(float $fahrenheit): float
{
return ($fahrenheit - 32) * 5 / 9;
}
private function getWeatherData(string $location): array
{
$weatherData = json_decode(file_get_contents("http://api.weatherapi.com/v1/{$location}/current"), true);
return $weatherData;
}
}
public function testFahrenheitToCelsiusAtFreezingPoint(): void
{
$api = $this->createObjectSeam(TemperatureApi::class);
// Call the protected method via the seam.
static::assertEquals(0.0, $api->seam()->call('fahrenheitToCelsius', 32.0));
}
public function testUnknownLocationThrowsException(): void
{
$api = $this->createObjectSeam(TemperatureApi::class);
// $api behaves exactly like TemperatureApi, but we can override behavior.
// Override the getWeatherData method to simulate an unknown location.
// Method must be protected or public to be overridden.
$api->seam()->override('getWeatherData', function (string $location) {
return ['error' => 'unknown_location'];
});
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown location: Atlantis');
$api->getCurrentTemperature('Atlantis');
}
class CurrencyApiTest
{
use PHPObjectSeam\CreatesObjectSeams;
public function testExample(): void
{
// Creates an CurrencyApi&ObjectSeam object - the constructor is not executed
$api = $this->createObjectSeam(CurrencyApi::class);
// $api behaves exactly like CurrencyApi, but we can override behavior.
$api->seam()
->override('connect', fn ($username, $password) => 'dummy_token')
->customConstruct(function($arg1) {
$this->url = 'http://www.dummy.url/' . $arg1;
}, 'api/v1/');
// do something with $api
static::assertEquals('dummy_token', $api->getToken());
}
}
// i.e. in the setup of your test
$this->foo = $this->createObjectSeam(Foo::class);
$this->foo->seam()->setCustomConstructor(function($arg1) {
$this->url = 'http://www.dummy.url/' . $arg1;
});
// in a specific test case
$this->foo->callCustomConstructor('api/v1/');
$foo = $this->createObjectSeam(Foo::class);
$foo->__construct('bar');
// or via the seam interface
$foo->seam()->call('__construct', 'bar');
$foo->seam()->callConstruct('bar');
$foo = $this->createObjectSeam(Foo::class);
$foo->seam()->captureCalls('publicOrProtectedMethod');
// do something with $foo
$foo->methodThatUsesTheCapturingMethods();
$calls = $foo->seam()->getCapturedCalls('publicOrProtectedMethod');
// assert that $calls contains a certain combination of arguments.
$foo = $this->createObjectSeam(Foo::class);
$foo->seam()->captureStaticCalls('publicOrProtectedStaticMethod');
// do something with $foo
$foo::methodThatUsesTheCapturingMethods();
$calls = $foo->seam()->getCapturedStaticCalls('publicOrProtectedStaticMethod');
// assert that $calls contains a certain combination of arguments.
$foo = $this->createObjectSeam(Foo::class);
$foo->seam()->captureCalls('$publicOrProtectedProperty::get')
->captureCalls('$publicOrProtectedProperty::set');
// do something with $foo
$foo->methodThatUsesTheCapturingProperties();
$getCalls = $foo->seam()->getCapturedCalls('$publicOrProtectedProperty::get');
$setCalls = $foo->seam()->getCapturedCalls('$publicOrProtectedProperty::set');
// assert that $calls contains a certain combination of arguments.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.