public function testMappingFailsIfNotEnoughFreeCodes()
 {
     $this->resetDatabase();
     $client = static::createClient();
     $container = $client->getContainer();
     $em = $container->get('doctrine.orm.entity_manager');
     $customer = new Customer();
     $customer->setIsActivated(true);
     $customer->setCouponsHaveBeenSent(false);
     $customer->setActivationCode('abc');
     $customer->setEmail('*****@*****.**');
     $customer->setEmployeeNumber('12345');
     $customer->setSalesdivision(Customer::SALESDIVISION_MEDIAMARKT_SATURN);
     $customer->setOptInAccepted(false);
     $em->persist($customer);
     $couponcode = new Couponcode();
     $couponcode->setCode('111');
     $em->flush();
     $em->clear();
     $cRepo = $em->getRepository('AppBundle\\Entity\\Customer');
     $customer = $cRepo->find(1);
     $m = new CouponMapperService($em);
     $m->mapNToCustomer(3, $customer);
     $em->clear();
     $cRepo = $em->getRepository('AppBundle\\Entity\\Customer');
     $customer = $cRepo->find(1);
     $codes = $customer->getCouponCodes()->getValues();
     $this->assertSame(0, sizeof($codes));
 }
 public function testExecute()
 {
     $kernel = $this->createKernel();
     $kernel->boot();
     $application = new Application($kernel);
     $application->setAutoExit(false);
     $input = new ArgvInput(['', 'doctrine:database:drop', '--no-interaction', '--force', '-q']);
     $application->run($input);
     $input = new ArgvInput(['', 'doctrine:database:create', '--no-interaction', '-q']);
     $application->run($input);
     $input = new ArgvInput(['', 'doctrine:migrations:migrate', '--no-interaction', '-q']);
     $application->run($input);
     $em = $kernel->getContainer()->get('doctrine')->getManager();
     $customer = new Customer();
     $customer->setEmail('*****@*****.**');
     $customer->setActivationCode('abc');
     $customer->setEmployeeNumber('1234567890');
     $customer->setSalesdivision(Customer::SALESDIVISION_MEDIAMARKT_SATURN);
     $customer->setIsActivated(true);
     $customer->setCouponsHaveBeenSent(false);
     $customer->setOptInAccepted(false);
     $em->persist($customer);
     $couponcode = new Couponcode();
     $couponcode->setCode('111');
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('222');
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('333');
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('444');
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('555');
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('666');
     $em->persist($couponcode);
     $em->flush();
     $application->add(new SendCouponsCommand());
     $command = $application->find('app:sendcoupons');
     $commandTester = new CommandTester($command);
     $commandTester->execute(array('command' => $command->getName()));
     $this->assertSame("example@example.org\n", $commandTester->getDisplay());
     $this->assertTrue($customer->getCouponsHaveBeenSent());
     $repo = $em->getRepository('AppBundle\\Entity\\Couponcode');
     $codes = $repo->findBy(['customer' => 1]);
     $this->assertSame('111', $codes[0]->getCode());
     $this->assertSame('222', $codes[1]->getCode());
     $this->assertSame('333', $codes[2]->getCode());
     $this->assertSame('444', $codes[3]->getCode());
     $this->assertSame('555', $codes[4]->getCode());
     $this->assertSame('666', $codes[5]->getCode());
     $this->assertSame(6, sizeof($codes));
 }
 public function testIndexWithExistingAndActivatedCustomerAndCorrectHash()
 {
     $client = static::createClient();
     $this->resetDatabase();
     static::$kernel = static::createKernel();
     static::$kernel->boot();
     $em = static::$kernel->getContainer()->get('doctrine')->getManager();
     $customer = new Customer();
     $customer->setEmail('*****@*****.**');
     $customer->setActivationCode('abc');
     $customer->setEmployeeNumber('456');
     $customer->setSalesdivision(Customer::SALESDIVISION_MEDIAMARKT_SATURN);
     $customer->setIsActivated(true);
     $customer->setCouponsHaveBeenSent(true);
     $customer->setOptInAccepted(false);
     $em->persist($customer);
     $couponcode = new Couponcode();
     $couponcode->setCode('111');
     $couponcode->setCustomer($customer);
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('222');
     $couponcode->setCustomer($customer);
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('333');
     $couponcode->setCustomer($customer);
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('444');
     $couponcode->setCustomer($customer);
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('555');
     $couponcode->setCustomer($customer);
     $em->persist($couponcode);
     $couponcode = new Couponcode();
     $couponcode->setCode('666');
     $couponcode->setCustomer($customer);
     $em->persist($couponcode);
     $em->flush();
     $secret = static::$kernel->getContainer()->getParameter('secret');
     $crawler = $client->request('GET', '/customer/' . $customer->getId() . '/coupons?hash=' . sha1($secret . $customer->getId()));
     $this->assertEquals(200, $client->getResponse()->getStatusCode());
     $this->assertContains('Einfach diesen Warengutschein beim Einkauf an der Kasse Ihrer Galeria Kaufhof abgeben', $crawler->filter('body')->text());
 }
    public function testFullUseCaseHappyPath()
    {
        $this->resetDatabase();
        $client = static::createClient();
        $container = $client->getContainer();
        $em = $container->get('doctrine.orm.entity_manager');
        $customerRepo = $em->getRepository('AppBundle\\Entity\\Customer');
        $couponcode = new Couponcode();
        $couponcode->setCode('111');
        $em->persist($couponcode);
        $couponcode = new Couponcode();
        $couponcode->setCode('222');
        $em->persist($couponcode);
        $couponcode = new Couponcode();
        $couponcode->setCode('333');
        $em->persist($couponcode);
        $couponcode = new Couponcode();
        $couponcode->setCode('444');
        $em->persist($couponcode);
        $couponcode = new Couponcode();
        $couponcode->setCode('555');
        $em->persist($couponcode);
        $couponcode = new Couponcode();
        $couponcode->setCode('666');
        $em->persist($couponcode);
        $em->flush();
        $crawler = $client->request('GET', '/');
        $buttonNode = $crawler->selectButton('Absenden');
        $form = $buttonNode->form();
        $client->enableProfiler();
        $crawler = $client->submit($form, ['registration[greeting]' => Customer::GREETING_MRS, 'registration[firstname]' => 'René Freiherr', 'registration[lastname]' => 'zu Schüßlen-Sêine, Jr. III.', 'registration[address]' => 'Êherne Freiherrenstraße 32', 'registration[zipcode]' => '41515', 'registration[city]' => 'Grevenbroich-Minkeln 2', 'registration[employeeNumber]' => '789', 'registration[email][first]' => '*****@*****.**', 'registration[email][second]' => '*****@*****.**', 'registration[salesdivision]' => '2', 'registration[optInAccepted]' => '1', 'registration[conditionsAccepted]' => '1']);
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        # Auf der Dankesseite darf das Formular nicht mehr angezeigt werden
        $this->assertSame(0, sizeof($crawler->filter('#registration')->first()));
        $this->assertSame('Vielen Dank. Sie erhalten nun eine Aktivierungsmail.', trim($crawler->filter('aside.messages div.alert.alert-success')->first()->text()));
        $customer = $customerRepo->findOneBy(['email' => '*****@*****.**']);
        $this->assertSame('789', $customer->getEmployeeNumber());
        $this->assertSame(2, $customer->getSalesDivision());
        $this->assertSame(true, $customer->getOptInAccepted());
        $this->assertSame(false, $customer->getIsActivated());
        $this->assertSame(false, $customer->getCouponsHaveBeenSent());
        $this->assertSame(0, sizeof($customer->getCouponcodes()));
        $mailCollector = $client->getProfile()->getCollector('swiftmailer');
        $this->assertEquals(1, $mailCollector->getMessageCount());
        $collectedMessages = $mailCollector->getMessages();
        $message = $collectedMessages[0];
        $this->assertSame('Ihre Registrierung für die Good Buy METRO Sonderaktion', $message->getSubject());
        $this->assertEquals('*****@*****.**', key($message->getTo()));
        $this->assertEquals('*****@*****.**', key($message->getFrom()));
        $expectedBody = '<html>
<body>
<p>
Guten Tag,
</p>
<p>
Sie erhalten diese E-Mail, da für die Adresse example@example.org eine Anmeldung zur Sonderaktion
"Good Buy METRO" durchgeführt wurde.
</p>
<p>
Um diese Anmeldung abzuschließen und Ihre persönlichen Gutscheine zu erhalten, klicken Sie bitte
auf den folgenden Link:
</p>
<p>
http://localhost:8000/customer/1/confirmation/' . $customer->getActivationCode() . '
</p>
<p>
Mit freundlichen Grüßen,
</p>
<p>
--&nbsp;<br>
GALERIA Kaufhof GmbH<br>
Leonhard-Tietz-Str. 1<br>
50676 Köln<br>
<br>
www.galeria-kaufhof.de<br>
Telefon: 0800-664870103<br>
Telefax: 01805-285028*<br>
Empfänger: GALERIA Kaufhof GmbH<br>
* (0,14 €/Min. aus dem dt. Festnetz, max. 0,42 €/Min. aus Mobilfunknetzen)<br>
<br>
Vorsitzender des Aufsichtsrats:<br>
Lovro Mandac<br>
Geschäftsführung:<br>
Olivier Van den Bossche (Vorsitzender)<br>
Edo Beukema<br>
Rolf Boje<br>
Thomas Fett<br>
Klaus Hellmich<br>
Volker Schlinge<br>
<br>
Sitz der Gesellschaft: Köln<br>
Eingetragen:<br>
Amtsgericht Köln<br>
HRB Nr. 64081<br>
UST-ID DE811142395<br>
WEEE-Reg.-Nr. DE80848693<br>
<br>
Member of METRO GROUP<br>
</p>
</body>
</html>
';
        $this->assertEquals($expectedBody, $message->getBody());
        $client->enableProfiler();
        $crawler = $client->request('GET', '/customer/1/confirmation/' . $customer->getActivationCode(), [], [], ['HTTP_X_FORWARDED_FOR' => '123.456.789.0']);
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
        $this->assertSame('Vielen Dank, Ihre Freischaltung war erfolgreich. Sie erhalten in wenigen Minuten eine E-Mail mit Ihren persönlichen Rabattcodes.', trim($crawler->filter('aside.messages div.alert.alert-success')->first()->text()));
        $em->clear();
        $customer = $customerRepo->findOneBy(['email' => '*****@*****.**']);
        $this->assertSame(true, $customer->getIsActivated());
        $this->assertSame(false, $customer->getCouponsHaveBeenSent());
        $this->assertNotNull($customer->getDatetimeActivation());
        $this->assertSame('123.456.789.0', $customer->getIpActivation());
        $this->assertSame(0, sizeof($customer->getCouponcodes()));
        $mailCollector = $client->getProfile()->getCollector('swiftmailer');
        $this->assertEquals(0, $mailCollector->getMessageCount());
        // Repeated Activation is not possible
        $crawler = $client->request('GET', '/customer/1/confirmation/' . $customer->getActivationCode());
        $this->assertEquals(410, $client->getResponse()->getStatusCode());
        $this->assertSame('Eine erneute Freischaltung ist nicht möglich!', trim($crawler->filter('aside.messages div.alert.alert-danger')->first()->text()));
    }