Passed
Pull Request — master (#489)
by Andrew
05:39
created

WayForPayControllerTest::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Stfalcon\Bundle\EventBundle\Tests\Controller;
4
5
use Application\Bundle\DefaultBundle\Controller\WayForPayController;
6
use Prophecy\Argument;
7
use Stfalcon\Bundle\EventBundle\Entity\Payment;
8
9
class WayForPayControllerTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var \Prophecy\Prophet
13
     */
14
    protected $prophet;
15
16
    protected function setup()
17
    {
18
        $this->prophet = new \Prophecy\Prophet();
19
    }
20
21
    protected function tearDown()
22
    {
23
        $this->prophet->checkPredictions();
24
    }
25
26
    public function testPaymentSetPaid()
27
    {
28
        $request = $this->prophet->prophesize('Symfony\Component\HttpFoundatio\Request');
29
        $request->willExtend('Symfony\Component\HttpFoundation\Request');
30
31
        $wayforpayService = $this->prophet->prophesize('Application\Bundle\DefaultBundle\Service\WayForPayService');
32
        $paymentService = $this->prophet->prophesize('Application\Bundle\DefaultBundle\Service\PaymentService');
33
34
        $container = $this->prophet->prophesize('Symfony\Component\DependencyInjection\Container');
35
        $doctrine = $this->prophet->prophesize('Doctrine\Bundle\DoctrineBundle\Registry');
36
        $em = $this->prophet->prophesize('Doctrine\ORM\EntityManager');
37
        $paymentRepository = $this->prophet->prophesize('Stfalcon\Bundle\EventBundle\Repository\PaymentRepository');
38
        $payment = $this->prophet->prophesize('Stfalcon\Bundle\EventBundle\Entity\Payment');
39
        $logger = $this->prophet->prophesize('Symfony\Bridge\Monolog\Logger');
40
        $referralService = $this->prophet->prophesize('Application\Bundle\DefaultBundle\Service\ReferralService');
41
        $session = $this->prophet->prophesize('Symfony\Component\HttpFoundation\Session\Session');
42
43
        $request->getContent()->shouldBeCalled()
44
            ->willReturn('{"orderNo":"1"}');
45
46
        $doctrine->getRepository('StfalconEventBundle:Payment')
47
            ->shouldBeCalled()
48
            ->willReturn($paymentRepository);
49
50
        $doctrine->getManager()->willReturn($em);
51
52
        $payment->isPending()->shouldBeCalled()->willReturn(true);
53
        $payment->setPaidWithGate(Payment::WAYFORPAY_GATE)->shouldBeCalled();
54
55
        $session->set('way_for_pay_payment', 1)->shouldBeCalled()->willReturn(null);
56
57
        $referralService->chargingReferral($payment)->shouldBeCalled()->willReturn(null);
58
        $referralService->utilizeBalance($payment)->shouldBeCalled()->willReturn(null);
59
60
        $container->has('doctrine')->willReturn(true);
61
        $container->get('doctrine')->willReturn($doctrine);
62
63
        $paymentRepository->findOneBy(Argument::any())->willReturn($payment);
64
65
        $container->get('app.way_for_pay.service')->willReturn($wayforpayService);
66
        $container->get('stfalcon_event.referral.service')->willReturn($referralService);
67
        $container->get('stfalcon_event.payment.service')->willReturn($paymentService);
68
        $container->get('logger')->willReturn($logger);
69
        $container->get('session')->willReturn($session);
70
71
        $wayforpayService->checkPayment(Argument::any(), Argument::any())
72
            ->willReturn(true)
73
            ->shouldBeCalled();
74
        $wayforpayService->saveResponseLog($payment, Argument::any(), 'set paid')->willReturn(null)
75
            ->shouldBeCalled();
76
        $wayforpayService->getResponseOnServiceUrl(Argument::any())->willReturn([])
77
            ->shouldBeCalled();
78
        $em->flush()->shouldBeCalled();
79
80
        $wayforpayController = new WayForPayController();
81
        $wayforpayController->setContainer($container->reveal());
82
        $result = $wayforpayController->serviceInteractionAction($request->reveal());
83
84
        $this->assertEquals(200, $result->getStatusCode());
85
    }
86
87
    public function testPaymentNotFound()
88
    {
89
        $request = $this->prophet->prophesize('Symfony\Component\HttpFoundatio\Request');
90
        $request->willExtend('Symfony\Component\HttpFoundation\Request');
91
92
        $container = $this->prophet->prophesize('Symfony\Component\DependencyInjection\Container');
93
        $doctrine = $this->prophet->prophesize('Doctrine\Bundle\DoctrineBundle\Registry');
94
        $em = $this->prophet->prophesize('Doctrine\ORM\EntityManager');
95
        $paymentRepository = $this->prophet->prophesize('Stfalcon\Bundle\EventBundle\Repository\PaymentRepository');
96
        $logger = $this->prophet->prophesize('Symfony\Bridge\Monolog\Logger');
97
        $wayforpayService = $this->prophet->prophesize('Application\Bundle\DefaultBundle\Service\WayForPayService');
98
99
        $wayforpayService->saveResponseLog(null, Argument::any(), Argument::any())->shouldBeCalled();
100
101
        $request->getContent()->shouldBeCalled()
102
            ->willReturn('{"orderNo":"1"}');
103
104
        $doctrine->getManager()->willReturn($em);
105
        $doctrine->getRepository('StfalconEventBundle:Payment')
106
            ->shouldBeCalled()
107
            ->willReturn($paymentRepository);
108
        $logger->addCritical(Argument::any())->shouldBeCalled();
109
110
        $container->get('app.way_for_pay.service')->willReturn($wayforpayService);
111
        $container->has('doctrine')->willReturn(true);
112
        $container->get('doctrine')->willReturn($doctrine);
113
        $container->get('logger')->willReturn($logger);
114
115
        $paymentRepository->findOneBy(Argument::any())->willReturn(null);
116
117
        $wayforpayController = new WayForPayController();
118
        $wayforpayController->setContainer($container->reveal());
119
        $result = $wayforpayController->serviceInteractionAction($request->reveal());
120
121
        $this->assertEquals(400, $result->getStatusCode());
122
    }
123
}
124