I have a question concerning the functional test in Silex/PHPUnit.
require_once '/var/www/silex/vendor/autoload.php';
class MyTest extends Silex\WebTestCase{
protected $app;
public function createApplication() {
$this->app = require 'app.php';
return $this->app;
}
public function testInitialPage(){
$client = $this->createClient();
$crawler = $client->request('GET', '/login');
$this->assertTrue($client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('html:contains("Login")'));
$this->assertCount(1, $crawler->filter('form'));
$this->app['session']->set('userid', X);
$this->app['session']->set('username', 'X');
$this->app['session']->set('isadmin', true);
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('html:contains("Cred")'));
}
public function testAdd() {
$client = $this->createClient();
$crawler = $client->request('GET', '/');
$this->assertTrue($client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('html:contains("Cred")'));
}
This should be the first "test" but every time I run it, the testInitialPage() method runs and I do not get any Errors.
But in testAdd() I get a failure ' No route found for "GET /" '
For me it seems that $app (and routing) does not exist anymore in the second Method "testAdd()"
Does anybody have a hint for me how to set a a right Functional Testing system?
There was a failure using the 'app.php' the roue to app.php was wrong.
Related
I need to send multiple mails with symfony swiftmailer inside a loop. This is my code.
public function __construct($name = null, \Swift_Mailer $mailer, EngineInterface $templating) {
parent::__construct($name)
$this->mailer = $mailer;
$this->templating = $templating;
}
protected function execute(InputInterface $input, OutputInterface $output) {
foreach ($ads as $ad) {
if($counter == 10) break;
$this->sendMail($ad->getUser()->getMail(), $ad, 'matching');
$counter++;
}
}
protected function sendMail($mail, $ad, $template = '') {
if($template == 'matching'){
$template = 'emails/matching-cars.html.twig';
}elseif($template == 'owner'){
$template = 'emails/matching-car-owners.html.twig';
}
$message = (new \Swift_Message('Hello Email'))
->setFrom('admin#admin.com')
->setTo($mail)
->setBody(
$this->templating->render(
$template, [
'ad' => $ad
]
), 'text/html'
);
$this->mailer->send($message);
}
Since I'm doing it inside loop it's really hard to pass mails array to swift mailer. I'm running this inside Console Command and I'm getting this error in console.
17:14:09 ERROR [app] Exception occurred while flushing email
queue: Expected response code 354 but got code "550", with message
"550 5.7.0 Requested action not taken: too many emails per second
symfony swiftmailer send multiple mails inside loop
let assume you have function called sendEmail that you use to sent email to all users that have account in you system.
Here are explanation that can help you to solve email error with code implementation:
For me it work....
public function sendEmail(\Swift_Mailer $mailer): Response
{
$entityManager=$this->getDoctrine()->getManager();
$connection= $entityManager->getConnection();
$decl12="SELECT id
FROM
user";
$statement97=$connection->prepare($decl12);
$statement97->execute();
$tap=$statement97->fetchAll();
try{
foreach($tap as $i=>$data)
{
//check what type of data taken from variable data by
//var_dump($data);
$test =$this->getDoctrine()->getRepository(User::class)->findOneBy(['id'=>$data]);
$email=$test->getEmail();
$message = (new \Swift_Message('Email Subject'))
->setFrom('admin#gmail.com')
->setTo($email)
->setBody(
"click here to get bonus",
'text/html'
);
$mailer->send($message);
}
}
catch (\Swift_TransportException $e)
{
return new Response($e->getMessage());
}
}
recently i see some issue on my projects, a found that there is some exception that i forget to catch them! this is my code:
try {
$this->user = $invoiceEvent->user;
$this->invoice = $invoiceEvent->invoice;
if ($this->user->email) {
$this->sendEmail();
}
}catch (Swift_RfcComplianceException $e) {
}
by using this try/catch my problem is solved, this is my test, and is green!, who can i assert the exception is catch?
/**
* #test
*/
public function it_should_Not_provide_exception_when_mailFromAddress_is_not_set()
{
$invoice = $this->makeInvoice();
$user = $this->makeUser();
$mail = app('Illuminate\Contracts\Mail\Mailer');
(new SendInvoiceToUser($mail))->handle(new InvoiceCreated($invoice, $user));
}
finally using this way i assert the exception catch an my method in this situation return null that is exactly what i except.
/**
* #test
*/
public function it_should_Not_provide_exception_when_mailFromAddress_is_not_set()
{
$invoice = $this->makeInvoice();
$user = $this->makeUser();
$mail = app('Illuminate\Contracts\Mail\Mailer');
$mocked = $this->getMockBuilder(SendInvoiceToUser::class)
->setConstructorArgs([$mail])
->setMethods(null)
->getMock();
$this->assertNull($mocked->handle(new InvoiceCreated($invoice, $user)));
}
by pass null to ->setMethods(null) the mocke object run the actual code contained within the method when called,
I am virtually at a brick wall with Symfony, I have a folder at /../compiled/ with some minified files, and the controller is configured to get files from getRootDir() + /../compiled/file.min.css
However, it just throws out an exception 'file not found' when call file_get_contents(file) even when the file actually exists.
I just don't know what is wrong, it is such a cryptic problem.
Code as requested:
public function atpInitAction(Request $request) // Validates the origin.
{
$content = null; $_file = $request->query->get('file');
if ($_SERVER["SERVER_NAME"] == atpHandler::getDomain())
{
// I AM FROM THE ORIGIN...
$webfolder = $this->get('kernel')->getRootDir() . '/../compiled';
$_file = $webfolder."/".$_file;
}
// What's my mime?
$_mime = 'text/plain';
if ($_file[strlen($_file)-2] == 'j') { $_mime = 'text/javascript'; }
else { $_mime = 'text/css'; }
$response = new Response();
$response->headers->set('Content-Type', $_mime);
$response->headers->set('Content-Disposition', 'filename="'.basename($_file) . '";');
$response->headers->set('Content-Length', filesize($_file));
$response->setContent(file_get_contents($_file));
$response->sendHeaders();
return $response;
}
i want to be able to set a Cookie onKernelRequest Method, but the cookie is not beeing set,
Everything else is working fine, what am missing here?
What i want to achieve is that if the user is not logged in and doesnt have the cookie he should see the http basic auth headers.
If the user is logged in or does have the cookie he has access to the preview domains without having to enter their user credentials in http basic auth.
const AUTH_USER = 'myuser';
const AUTH_PW = 'mypass';
public function sendAuthorizationHeader()
{
header('WWW-Authenticate: Basic realm="Preview Domain"');
header('HTTP/1.0 401 Unauthorized');
die();
}
public function onKernelRequest(GetResponseEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
$host = $request->getHost();
$loginSuccessful = false;
// check if we are on a preview domain
if (preg_match('/^preview-\d+/', $host)) {
$user = $request->getUser();
$cookie = $request->cookies->get('preview_user');
$phpAuthUser = $request->server->get('PHP_AUTH_USER');
$phpAuthPw = $request->server->get('PHP_AUTH_PW');
if (isset($phpAuthUser) && isset($phpAuthPw)) {
if ($phpAuthUser == self::AUTH_USER && $phpAuthPw == self::AUTH_PW) {
$loginSuccessful = true;
}
} else if ($user === null && $cookie === null) {
$this->sendAuthorizationHeader();
}
if (!$loginSuccessful) {
$this->sendAuthorizationHeader();
} else {
$cookie = new Cookie('preview_user', true, 86400, '/', null, false, false);
$response = new Response();
$response->headers->setCookie($cookie);
$response->sendHeaders();
}
}
}
Setting a cookie on a response object doesn't do anything but adding a cookie to that request. You need to return the same response object, so Symfony renders it back to the client. Rendering it yourself is not a good idea, as there might be contents send later and it's not really testable.
It's easier done in a kernel.response event listener, since you already have a response there. Remember to use the response that your application creates. Do not create it yourself.
If you set the cookie based on logic that should also be available during the request, you can split it into two event listener methods. One will set a request attribute on kernel.request, and the other one will set the cookie on the response on kernel.response:
public function onKernelRequest(GetResponseEvent $event)
{
// your logic goes here. calculate the $result
// ...
$event->getRequest()->attributes->set('my_result', $result);
}
public function onKernelResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();
$request = $event->getRequest();
$myResult = $request->attributes->get('my_result');
$cookie = new Cookie(/* ... */);
$response->headers->setCookie($cookie);
}
In the functional test below, the assertion that the text 'Glenshire' exists fails even though the output of echo $client->getResponse()->getContent(); includes
<li>Glenshire...
The first two assertions are true. There is no redirect.
The test
class SearchTest extends WebTestCase
{
public function setUp()
{
$classes = array(
'Vol\VolBundle\DataFixtures\ORM\LoadFocusSkillData',
'Vol\VolBundle\DataFixtures\ORM\LoadOpportunity',
);
$this->loadFixtures($classes);
}
public function testSearch()
{
$client = static::createClient();
$crawler = $client->request('GET', '/search');
$this->assertTrue($crawler->filter('html:contains("Focus")')->count() > 0);
$this->assertTrue($crawler->filter('html:contains("Skill")')->count() > 0);
$form = $crawler->selectButton('match_search[Submit]')->form();
$form['match_search[focuses][0]'] = 1;
$client->submit($form);
echo $client->getResponse()->getContent();
$this->assertTrue($crawler->filter('li:contains("Glenshire")')->count() > 0, 'Glenshire not found');
}
The fixture (using LiipFunctionalTestBundle)
public function load(ObjectManager $manager)
{
$manager->clear();
$org = new Organization();
$org->setOrganization('Glenshire Marmot Fund');
$foc1 = $manager->getRepository("VolVolBundle:Focus")->find(1);
$foc3 = $manager->getRepository("VolVolBundle:Focus")->find(3);
$foc4 = $manager->getRepository("VolVolBundle:Focus")->find(4);
$org->addFocus($foc1);
$org->addFocus($foc3);
$org->addFocus($foc4);
$opp = new Opportunity();
$opp->setName('Feeder');
$opp->setDescription("Beyond recourse");
$opp->setExpireDate(date_add(new \DateTime(), new \DateInterval('P1Y')));
$opp->setOrganization($org);
$manager->persist($opp);
$manager->flush();
}
Curiously, replacing the line
$this->assertTrue($crawler->filter('li:contains("Glenshire")')->count() > 0, 'Glenshire not found');
with
$this->assertRegExp(
'/Glenshire/', $client->getResponse()->getContent(), 'Glenshire not found'
);
provides a successful test!