条纹支付功能测试以正确的方式

时间:2015-04-08 20:07:34

标签: symfony phpunit stripe-payments functional-testing

我是功能测试的新手,我正在尝试在下一个功能上实现它:

This is my controller how it looks

/**
 * @description prepare recurring stripe payment via Checkout Form
 * @author Mohamed Ragab Dahab <mdahab@treze.co.uk>
 * @access public
 *
 * @param \Symfony\Component\HttpFoundation\Request $request 
 *
 * @Extra\Route(
 *   "/prepare_checkout/{payum_token}/{ownerType}",
 *   name="itjari_stripe_prepare_checkout"
 * )
 */
public function prepareCheckoutAction(Request $request, $ownerType) {
    $paymentName = 'stripe_checkout';
    $token = $this->get('payum.security.http_request_verifier')->verify($request);
    $payment = $this->getPayum()->getPayment($token->getPaymentName());
    $agreementStatus = new GetHumanStatus($token);
    $payment->execute($agreementStatus);

    $agreement = $agreementStatus->getFirstModel();
    $agreementDetails = $agreement->getDetails();
    // get agreement details
    $storage = $this->getPayum()->getStorage('ITJari\PaymentBundle\Entity\RecurringPaymentDetails');
    $details = $storage->create();
    $details["amount"] = $agreementDetails['amount'];
    $details["currency"] = $agreementDetails['currency'];
    $details["description"] = $agreementDetails['description'];
    $details["formData"] = $agreementDetails['formData'];
    $details["planName"] = $agreementDetails['planName'];
    // check if customer submitted the payment form and stripeToken got generated 
    if ($request->isMethod('POST') && $request->request->get('stripeToken')) {
        // set stripe api key required for [stripe-php lib] requests
        \Stripe::setApiKey($this->container->getParameter('stripe.secret_key'));
        // create a new customer and assign a plan to him using [stripe-php lib] 
        $customer = \Stripe_Customer::create([
                    'description' => $details['formData'][/* working for all owners */SubscriptionStudentInputs::GuardianFirstNameFieldName] . ' ' . $details['formData'][/* working for all owners */SubscriptionStudentInputs::GuardianLastNameFieldName],
                    'source' => $request->request->get('stripeToken'),
                    'plan' => $details['planName'],
        ]);
        // assign customer ID to payment charge method
        $details["customer"] = $customer->id;
        // charge and presist the customer and the payment
        $storage->update($details);

        $captureToken = $this->getTokenFactory()->createToken(
                $paymentName, $details, 'itjari_subscription_create_stripe_recurring_payment', ['ownerType' => $ownerType, 'agreementId' => $agreement->getId()]
        );

        return $this->redirect($captureToken->getTargetUrl());
    }
    // Render Checkout Form
    return $this->render("ITJariStripeBundle:PurchaseExamples:prepareCheckout.html.twig", [
                'publishable_key' => $this->container->getParameter('stripe.publishable_key'),
                'model' => $details,
                'paymentName' => $paymentName
                    ]
    );
}

这是我的功能测试功能: 这是我的控制器看起来如何

public function testPrepareCheckoutAction() {
    // owner types
    $ownerType = $this->Faker->randomElement(['teacher', 'student']);

    //Payum Token
    $payumToken = '';

    // params
    $params = array('payum_token' => $payumToken, 'ownerType' => $ownerType);

    // Route to tested action
    $crawler = $this->client->request(RequestMethods::GET, $this->router->generate('itjari_stripe_prepare_checkout', $params));

    // shorten
    $crawlerResponse = $this->client->getResponse();

    // Assert Page is loaded ok
    $this->assertEquals(200, $crawlerResponse->getStatusCode());

    $token = $this->container->get('payum.security.http_request_verifier')->verify();

    $payment = $this->container->get('payum')->getPayment($token->getPaymentName());

    //assert
    $this->assertInternalType('array', $payment);

    $agreementStatus = new \Payum\Core\Request\GetHumanStatus($token);
    $payment->execute($agreementStatus);
    $agreementDetails = $agreementStatus->getFirstModel()->getDetails();

    //assert
    $this->assertInternalType('array', $agreementDetails);
    //assert
    $this->assertArrayHasKey(array('amount', 'currency', 'description', 'formData', 'planName'), $agreementDetails);

    \Stripe::setApiKey($this->container->getParameter('stripe.secret_key'));
    $customer = \Stripe_Customer::create([
                'description' => $agreementDetails['formData'][/* working for all owners */\ITJari\StudentBundle\Services\SubscriptionStudentInputs::GuardianFirstNameFieldName] . ' ' . $agreementDetails['formData'][/* working for all owners */\ITJari\StudentBundle\Services\SubscriptionStudentInputs::GuardianLastNameFieldName],
                'source' => $this->request->get('stripeToken'),
                'plan' => $agreementDetails['planName'],
    ]);
    //assert
    $this->assertInstanceOf('\Stripe_Customer', $customer);
}

我的问题在这里,我正在以正确的方式进行功能测试?

1 个答案:

答案 0 :(得分:1)

  1. 你的控制器很重要。您应该将整个付款移到单独的类中,然后对这些类进行单元测试,而无需实际调用付款提供商。您还可以使用控制器中的服务来处理付款。
  2. 带来:

    1. 要测试您的控制器,您只需模拟服务并返回预期要测试的各种值,如果所有情况都正确处理。
      1. 如果您的支付服务提供商为您提供了在沙箱中工作的选项,请创建一个沙盒版本的服务,然后您甚至不需要模拟它。只需在测试环境中定义不同的服务即可。