我是功能测试的新手,我正在尝试在下一个功能上实现它:
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);
}
我的问题在这里,我正在以正确的方式进行功能测试?
答案 0 :(得分:1)
带来:
或