Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active June 30, 2024 14:24
Show Gist options
  • Save trikitrok/1ad86a94b0e6d91dbd01 to your computer and use it in GitHub Desktop.
Save trikitrok/1ad86a94b0e6d91dbd01 to your computer and use it in GitHub Desktop.
<?php
class SendingFormByEmailAndRedirectingTest extends PHPUnit_Framework_TestCase
{
private $form;
private $formRedirection;
private $email;
private $sendAndRedirectAction;
private $WHATEVER_FIELDS;
function setUp()
{
$this->WHATEVER_FIELDS =
array("fieldName" => "fieldContent");
$this->form = Phake::mock('Form');
$this->formRedirection = Phake::mock('FormRedirection');
$this->email = Phake::mock('FormEmail');
$this->sendAndRedirectAction =
new SendingFormByEmailAndRedirecting(
$this->email,
$this->form,
$this->formRedirection
);
}
function testRedirectionBackToFormPage()
{
$this->whenFormNotFilled();
$this->sendAndRedirectAction->execute();
Phake::verify($this->formRedirection)
->backToFormPage();
}
function testEmailSending()
{
$this->whenFormFilledWithWhateverFields();
$this->sendAndRedirectAction->execute();
Phake::verify($this->email)
->send($this->WHATEVER_FIELDS);
}
function testRedirectionToSuccesPage()
{
$this->whenFormFilledWithWhateverFields();
$this->andEmailSentSuccessfully();
$this->sendAndRedirectAction->execute();
Phake::verify($this->formRedirection)->toSuccessPage();
}
function testRedirectionToFailPage()
{
$this->whenFormFilledWithWhateverFields();
$this->andEmailNotSentSuccessfully();
$this->sendAndRedirectAction->execute();
Phake::verify($this->formRedirection)->toFailPage();
}
private function whenFormFilledWithWhateverFields() {
Phake::when($this->form)
->isNotFilled()
->thenReturn(false);
Phake::when($this->form)
->fields()
->thenReturn($this->WHATEVER_FIELDS);
}
private function whenFormNotFilled()
{
Phake::when($this->form)
->isNotFilled()
->thenReturn(true);
}
private function andEmailSentSuccessfully()
{
Phake::when($this->email)
->send($this->WHATEVER_FIELDS)
->thenReturn(true);
}
private function andEmailNotSentSuccessfully()
{
Phake::when($this->email)
->send($this->WHATEVER_FIELDS)
->thenReturn(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment