PHP code example of metalogico / laravel-mocka
1. Go to this page and download the library: Download metalogico/laravel-mocka 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/ */
metalogico / laravel-mocka example snippets
return [
'POST' => [
'authenticate' => [
'success' => true,
'user_id' => 12345,
'token' => 'mock_token_12345',
'expires_at' => time() + 3600,
],
],
'GET' => [
'getFileList' => [
'success' => true,
'files' => [
[
'name' => 'file1.pdf',
'size' => 1024,
'created_at' => '2023-01-01 00:00:00',
],
[
'name' => 'file2.pdf',
'size' => 2048,
'created_at' => '2023-01-02 00:00:00',
],
// ...
],
]
],
];
'mappings' => [
[
'url' => env('EXTERNAL_API_URL').'/api/authenticate',
'file' => 'api.mock.php',
'key' => 'POST.authenticate',
],
[
'url' => env('EXTERNAL_API_URL').'/api/files/',
'file' => 'api.mock.php',
'key' => 'GET.getFileList',
],
];
namespace App\Services;
use Metalogico\Mocka\Facades\MockaHttp;
class DmsService
{
public static function authenticate($user, $password)
{
$response = MockaHttp::post(config('external_api_url').'/api/authenticate', [
'user' => $user,
'password' => $password,
]);
session()->put('token', $response->json()['token']);
return $response->json();
}
public static function getFileList()
{
$response = MockaHttp::withHeaders([
'Authorization' => 'Bearer '.session()->get('token'),
])->get(config('external_api_url').'/api/files');
return $response->json();
}
}
return [
'GET' => [
'simpleAuth' => [
'success' => true,
'token' => 'static_token_123',
'user_id' => 12345,
],
],
];
return [
'GET' => [
'userList' => fn() => [
'users' => collect(range(1, fake()->numberBetween(3, 8)))
->map(fn() => [
'name' => fake()->name,
'email' => fake()->email,
]),
'total' => fake()->numberBetween(50, 200),
],
],
],
];
return [
'GET' => [
'mixedResponse' => fn() => [
'status' => 'success', // Static
'timestamp' => time(), // Static but with function
'dynamic_data' => fn() => [
'user_count' => fake()->numberBetween(5, 20),
'featured_products' => collect(range(1, 3))
->map(fn() => [
'id' => fake()->numberBetween(1000, 9999),
'name' => fake()->words(3, true),
'price' => fake()->randomFloat(2, 10, 500),
]),
],
],
],
],
];
'mappings' => [
// Exact match
[
'url' => 'https://api.example.com/users/123',
'match' => 'exact', // which is the default
'file' => 'users.mock.php',
'key' => 'GET.specificUser',
],
// Wildcard matching
[
'url' => 'https://api.example.com/users/*',
'match' => 'wildcard',
'file' => 'users.mock.php',
'key' => 'GET.anyUser',
],
// Regex matching (coming soon ™)
[
'url' => '/^https:\/\/api\.example\.com\/orders\/\d+$/',
'match' => 'regex',
'file' => 'orders.mock.php',
'key' => 'GET.orderDetail',
],
];
'mappings' => [
[
'url' => 'https://api.example.com/users/123',
'file' => 'users.mock.php',
'key' => 'GET.specificUser',
'errors' => 'GET.specificUserErrors' // Optional error configuration
],
];
return [
'GET' => [
'specificUser' => fn() => [
'id' => fake()->numberBetween(1000, 9999),
'name' => fake()->name,
'email' => fake()->email,
],
'specificUserErrors' => [
'error_rate' => 25, // 25% chance of error
'errors' => [
422 => [
'message' => 'Validation failed',
'errors' => ['name' => ['Name is ],
],
],
],
];
'mappings' => [
[
'url' => 'https://slow-api.com/data',
'file' => 'slow.mock.php',
'key' => 'GET.slowResponse',
'delay' => 5000, // 5 second delay
],
];
use Metalogico\Mocka\Facades\MockaHttp;
$enabled = ($user === '[email protected] ') ? true : false;
$response = MockaHttp::withOptions(['mocka' => $enabled])
->get(config('external_api_url').'/api/files');
return [
// Globally enable Mocka (default: false in production)
'enabled' => env('MOCKA_ENABLED', false),
// Basic logging (can be extended later)
'logs' => env('MOCKA_LOGS', false),
// Users (emails) for which Mocka is active when enabled
// Supports comma-separated env var: MOCKA_USERS="[email protected] , [email protected] "
'users' => array_values(array_filter(array_map('trim', explode(',', env('MOCKA_USERS', ''))))),
// Path to mock files
'mocks_path' => resource_path('mocka'),
// Default delay for all mocked requests (milliseconds)
'default_delay' => 0,
// Security: only allow mocking for these hostnames (empty => all hosts allowed)
'allowed_hosts' => [],
// Allowed application environments for Mocka activation (default: local only)
// Extend this to enable in other envs, e.g. ['local', 'staging']
'environments' => ['local'],
// URL mappings
'mappings' => [
// Your API mappings here
],
];
bash
php artisan vendor:publish --tag="mocka-config"
bash
php artisan mocka:list