A few days ago I was explaining mocking to a few colleagues. I came up with the following quick n dirty code to show them the elegance of Mockery as a mocking framework.
The code is straight-forward: it checks the health of a server using a tiny heart beat script.
1. The script residing in the remote server:
/remote/heart.php
1 2 3 4 5 6 | <?php $response = array('status' => 'online'); header("Content-type: application/json"); echo json_encode($response); |
2. The class that tries to fetch the status from the remote server:
/test/Heartbeat.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php class Heartbeat { private $http; public function __construct(Http $http) { $this->http = $http; } public function checkHealth() { $url = 'http://localhost/remote/heart.php'; $response = $this->http->getResponse($url); $beat = json_decode($response); return (!is_null($beat) && $beat->status == 'online'); } } |
Recent Comments