File: /home/xedaptot/ai.naniguide.com/tests/Feature/SendingServerCreateFromArrayTest.php
<?php
use App\Model\SendingServer;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
uses(TestCase::class, DatabaseTransactions::class);
/**
* Form save flow business logic — exercises the path that the user catches
* with "Save Changes refresh" symptom. Posts of vendor form data to admin
* controller call SendingServer::createFromArray(); this test exercises
* createFromArray directly to verify the validation + persistence chain
* without needing admin auth boilerplate.
*
* Catches the class of bug "method renamed but caller missed":
* - Wrong driver method dispatched in validConnection
* - Vendor config keys not routed to JSON config
* - Driver test() throwing instead of returning TestResult
*/
test('createFromArray for sendmail with bad path → validator fails on connection field', function () {
$params = [
'type' => 'sendmail',
'sendmail_path' => '/this/path/does/not/exist/at/all/sendmail',
'name' => 'Bad Path Test',
'admin_id' => 1,
];
[$validator, $server] = SendingServer::createFromArray($params);
expect($validator->fails())->toBeTrue();
expect($validator->errors()->has('connection'))->toBeTrue();
expect($validator->errors()->first('connection'))->toContain('does not exist');
// Server should NOT be persisted on validator fail.
expect($server->exists)->toBeFalse();
});
test('createFromArray with missing required field returns field-level validator error', function () {
$params = [
'type' => 'sendmail',
// sendmail_path missing
'admin_id' => 1,
];
[$validator, $server] = SendingServer::createFromArray($params);
expect($validator->fails())->toBeTrue();
expect($validator->errors()->has('sendmail_path'))->toBeTrue();
});
test('createFromArray for valid sendmail (real binary) saves with config in JSON', function () {
// /bin/sh exists + executable on every UNIX env. SendmailDriver::test()
// only checks file_exists + is_executable — does NOT actually invoke.
$params = [
'type' => 'sendmail',
'sendmail_path' => '/bin/sh',
'name' => 'Test Sendmail',
'admin_id' => 1,
];
[$validator, $server] = SendingServer::createFromArray($params);
expect($validator->fails())->toBeFalse();
expect($server->exists)->toBeTrue();
expect($server->id)->not->toBeNull();
expect($server->type)->toBe('sendmail');
expect($server->getConfig('sendmail_path'))->toBe('/bin/sh');
expect($server->status)->toBe(SendingServer::STATUS_ACTIVE);
$server->delete();
});
test('createFromArray routes vendor config via fill() override to JSON column', function () {
$params = [
'type' => 'sendmail',
'sendmail_path' => '/bin/sh',
'name' => 'Config Routing Test',
'admin_id' => 1,
];
[$validator, $server] = SendingServer::createFromArray($params);
expect($validator->fails())->toBeFalse();
// Read raw DB row — sendmail_path column doesn't exist, must be in config.
$row = \DB::table('sending_servers')->where('id', $server->id)->first();
$config = json_decode($row->config, true);
expect($config)->toHaveKey('sendmail_path');
expect($config['sendmail_path'])->toBe('/bin/sh');
$server->delete();
});
test('createFromArray throws when type is missing', function () {
expect(fn () => SendingServer::createFromArray(['admin_id' => 1]))
->toThrow(\InvalidArgumentException::class, 'requires a type');
});
test('SendingServer::sendTestEmail invokes driver->setupBeforeSend without "undefined method" error', function () {
// Catches the bug user found: $this->setupBeforeSend() / $this->test()
// in legacy → $this->driver()->setupBeforeSend() in driver pattern.
//
// We don't actually send (would need real SMTP). Instead intercept by
// using the Generic SmtpDriver pointed at unreachable host — the
// EsmtpTransport throws on transport, but the bug we're catching
// (undefined-method on driver) would fire BEFORE transport-level error.
$server = SendingServer::factory()->create(['type' => 'smtp']);
$server->setConfigMany([
'host' => '127.0.0.1',
'smtp_port' => '1', // unreachable port
'smtp_protocol' => 'tls',
'smtp_username' => 'u',
'smtp_password' => 'p',
]);
$email = new \Symfony\Component\Mime\Email();
$email->subject('Test');
$email->text('Body');
$email->from('[email protected]');
$email->to('[email protected]');
try {
$server->sendTestEmail($email);
} catch (\Throwable $e) {
// Accept: connection refused / transport failure.
// Reject: BadMethodCallException / undefined method ...
expect($e)->not->toBeInstanceOf(\BadMethodCallException::class);
expect($e->getMessage())->not->toContain('undefined method');
}
$server->delete();
});