laravel/tests/laravel.php
2026-11-28 15:28:05 +02:00

91 lines
3.6 KiB
PHP

<?php
// Runs the package inside a real Laravel app, against the API with a sandbox_ key.
//
// LARAVEL_VENDOR=/path/to/a/laravel/app/vendor TEST_API_KEY=sandbox_... \
// TEST_EMAIL_FROM=you@verified-domain php tests/laravel.php
//
// Nothing is written inside that app: the test uses a temporary folder.
$vendor = getenv('LARAVEL_VENDOR');
$key = getenv('TEST_API_KEY');
if (!$vendor || !is_file($vendor . '/autoload.php') || !$key || strpos($key, 'sandbox_') !== 0) {
fwrite(STDERR, "Set LARAVEL_VENDOR and TEST_API_KEY (a sandbox_ key).\n");
exit(2);
}
require $vendor . '/autoload.php';
require __DIR__ . '/bootstrap.php';
use InterwebMz\Laravel\Client;
use InterwebMz\Laravel\Facades\InterwebMz;
use InterwebMz\Laravel\Notifications\SmsChannel;
use InterwebMz\Laravel\Notifications\SmsMessage;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
$base = sys_get_temp_dir() . '/laravel-package-test-' . getmypid();
@mkdir($base . '/views', 0700, true);
$app = new Illuminate\Foundation\Application($base);
$app->instance('config', new Illuminate\Config\Repository([
'app' => ['name' => 'Test', 'env' => 'testing', 'debug' => true],
'interweb_mz' => ['api_key' => $key, 'mode' => 'sandbox', 'sms' => ['sender' => null]],
'mail' => [
'default' => 'interweb_mz',
'mailers' => ['interweb_mz' => ['transport' => 'interweb_mz']],
'from' => ['address' => getenv('TEST_EMAIL_FROM') ?: 'test@example.org', 'name' => 'Package test'],
],
'view' => ['paths' => [$base . '/views'], 'compiled' => $base . '/views'],
]));
foreach ([
Illuminate\Events\EventServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Notifications\NotificationServiceProvider::class,
\InterwebMz\Laravel\ServiceProvider::class,
] as $p) {
$app->register($p);
}
Illuminate\Support\Facades\Facade::setFacadeApplication($app);
$app->boot();
// The same client, pointed at TEST_API_URL when it is set.
$app->singleton(Client::class, function ($app) {
$c = $app['config']->get('interweb_mz');
return new LiveClient(['api_key' => $c['api_key'], 'mode' => $c['mode']]);
});
$client = $app->make(Client::class);
check('provider merged the config', config('interweb_mz.retries') === null || is_int(config('interweb_mz.retries')));
check('facade reaches the client', InterwebMz::getFacadeRoot() === $client);
$w = InterwebMz::whois()->lookup('example.co.mz');
check('facade WHOIS', isset($w['data']['status']));
class PackageTestNotification extends Illuminate\Notifications\Notification
{
public function via($n)
{
return ['sms', SmsChannel::class];
}
public function toSms($n)
{
return SmsMessage::create('Test of the SMS channel.');
}
}
Notification::route('sms', '+258841234567')->notifyNow(new PackageTestNotification());
check('notification through the SMS channel (simulated)', $client->wasSimulated() && strpos($client->lastResponse()['headers']['x-api-mode'] ?? '', 'sandbox') === 0);
if (getenv('TEST_EMAIL_FROM')) {
Mail::raw('Test of the mail transport.', function ($m) {
$m->to('ana@example.org', 'Ana')->cc('rui@example.org')->subject('Package test')->getHeaders()->addTextHeader('X-Order-Id', '1234');
});
check('mail through the E-mail Center transport (simulated)', $client->wasSimulated() && $client->lastResponse()['status'] === 201);
}
array_map('unlink', glob($base . '/views/*') ?: []);
@rmdir($base . '/views');
@rmdir($base);
done();