j’ai fini par comprendre comment ça fonctionnait, le bouchonnage des fonctions natives de PHP ; et j’ai proposé un petit ajout à la doc Atoum, ça évitera aux suivants de s’arracher les cheveux \o/
En reprenant l’exemple d’hier avec la fonction mail(), voilà un exemple :
$that = $this;
$this
->assert('mail')
->given($this->newTestedInstance())
->if($this->function->mail = function (string $to, string $subject, string $message, $headers, string $params = '') use ($that) {
$that
->string($to)
->isNotEmpty()
->boolean(filter_var($to, FILTER_VALIDATE_EMAIL) !== false)
->isTrue()
->string($subject)
->isNotEmpty()
->string($message)
->isNotEmpty()
;
return true;
})
->then
->boolean($this->testedInstance->sendMail('contact@example.com', 'Subject', 'Content'))
->isTrue()
->function('mail')
->wasCalled()
->once()
->assert('mail failed')
->given($this->newTestedInstance())
->if($this->function->mail = fn () => false)
->then
->exception(function () { $this->testedInstance->sendMail('contact', 'Subject', 'Content'); })
;
Et on peut donc définir exactement comment va réagir la fonction, voire même comme ci-dessus exploiter Atoum pour tester les paramètres fournis.
En gros, l’expression $this->function->function_native_PHP =
suivi d’une valeur, d’une expression voire d’une fonction anonyme permet de faire ça.