public function testCreateSubscription() { //Fake data $planId = '3'; //set one day expired date $expirationDate = date('Y-m-d H:i:s', mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"))); //Create user $account = new Users_Model_User(); $account->avatar = null; $account->login = '******' . date('YmdHis'); $account->email = 'autotest3' . time() . '@example.org'; $account->password = md5('password'); $account->role = Users_Model_User::ROLE_USER; $account->status = Users_Model_User::STATUS_ACTIVE; $account->save(); $subscriptionManager = new Subscriptions_Model_Subscription_Manager(); $subscription = $subscriptionManager->createFreeSubscription($account->id, $planId); //Test create subscription $this->assertNotEmpty($subscription); //Test expired subscription $subscription->expirationDate = $expirationDate; $subscription->save(); $subscriptions = $subscriptionManager->getExpiredActiveSubscriptions(); $this->assertNotEmpty($subscriptions); $this->assertCount(1, $subscriptions); //Change status $subscriptionManager->setInactiveStatusExpiredSubscriptions(); //Test none expired subscription $subscriptions = $subscriptionManager->getExpiredActiveSubscriptions(); $this->assertNotEmpty($subscriptions); $this->assertCount(0, $subscriptions); }
/** * Create subscription page * * If selected free or trial plan - changes immediately * If selected paid plan user allow select payment method (PayPAl or another payment system) and pay subscription */ public function createAction() { $planId = $this->_getParam('id'); if ($this->_request->isPost() && $planId) { //Get user $identity = Zend_Auth::getInstance()->getIdentity(); $userId = $identity->id; //Get plan $subscriptionPlansTable = new Subscriptions_Model_SubscriptionPlans_Table(); $subscriptionPlan = $subscriptionPlansTable->getById($planId); //Not free subscription if ($subscriptionPlan->price > 0) { //Create order $orderManager = new Payments_Model_Order_Manager(); $order = $orderManager->createOrder($userId, $subscriptionPlan->price); $this->view->orderId = $order->id; $this->view->subscriptionPlan = $subscriptionPlan; $subscriptionManger = new Subscriptions_Model_Subscription_Manager(); //Create paid subscription $subscriptionManger->createPaidSubscription($userId, $subscriptionPlan->id, $order->id); //'type'-orderId-userId-planId $this->view->paypalCustom = Subscriptions_Model_Subscription::ORDER_TYPE_SUBSCRIPTION . '-' . $order->id; } else { //Free subscription try { //Not allow renewal Trial subscription if ($subscriptionPlan->type === Subscriptions_Model_SubscriptionPlan::PLAN_TYPE_TRIAL) { $subscriptionTable = new Subscriptions_Model_Subscription_Table(); $select = $subscriptionTable->select()->from(array('subscriptions'))->where('userId =?', $userId)->where('subscriptionPlanId = ?', $subscriptionPlan->id); if ($subscriptionTable->fetchRow($select)) { //Found old trial plan throw new Zend_Controller_Action_Exception('You can not change to this plan'); } } $subscriptionManger = new Subscriptions_Model_Subscription_Manager(); //Create subscription $subscriptionManger->createFreeSubscription($userId, $planId); //Redirect to Thank you page $this->redirect('/subscriptions/index/complete'); } catch (Zend_Controller_Action_Exception $ex) { $this->_flashMessenger->addMessage($ex->getMessage()); $this->redirect('/subscriptions'); } } } else { $this->redirect('/subscriptions'); } }
public function testCompleteAction() { //Save fake user to DB $account = new Users_Model_User(); $account->avatar = null; $account->login = '******' . date('YmdHis'); $account->email = 'testCompleteAction' . time() . '@example.org'; $account->password = md5('password'); $account->role = Users_Model_User::ROLE_USER; $account->status = Users_Model_User::STATUS_ACTIVE; $account->save(); //Login Zend_Auth::getInstance()->getStorage()->write($account); $subscriptionManager = new Subscriptions_Model_Subscription_Manager(); $subscriptionManager->createFreeSubscription($account->id, 1); $this->dispatch('/subscriptions/index/complete'); $this->assertModule('subscriptions'); $this->assertController('index'); $this->assertAction('complete'); $this->assertContains('Current plan', $this->getResponse()->getBody()); }