How can a beginner effectively use a fake API for testing purposes in a PHP project?

To effectively use a fake API for testing purposes in a PHP project, you can create a mock API class that simulates the behavior of the real API. This mock API class should have methods that return predefined responses to mimic the different API endpoints. By using this mock API class in your tests, you can control the responses and easily test different scenarios without relying on the actual API.

class FakeAPI {
    public function getUsers() {
        return json_encode([
            ['id' => 1, 'name' => 'John Doe'],
            ['id' => 2, 'name' => 'Jane Smith']
        ]);
    }

    public function getUserById($id) {
        $users = [
            1 => ['id' => 1, 'name' => 'John Doe'],
            2 => ['id' => 2, 'name' => 'Jane Smith']
        ];

        return json_encode($users[$id]);
    }
}

// Example of using the FakeAPI class in your PHP project
$fakeAPI = new FakeAPI();

// Get all users
$users = json_decode($fakeAPI->getUsers(), true);
var_dump($users);

// Get user by ID
$user = json_decode($fakeAPI->getUserById(1), true);
var_dump($user);