Last active
October 18, 2017 19:25
-
-
Save jonathonbyrdziak/e546484f1e9a01c68d2a5c60807d9b98 to your computer and use it in GitHub Desktop.
Sample code showing my work with phpunit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Mage Plugins, LLC | |
* | |
* NOTICE OF LICENSE | |
* | |
* This source file is subject to the Mage Plugins Commercial License (MPCL 1.0) | |
* that is bundled with this package in the file LICENSE.md. | |
* It is also available through the world-wide-web at this URL: | |
* https://mageplugins.net/commercial-license/ | |
* If you did not receive a copy of the license and are unable to | |
* obtain it through the world-wide-web, please send an email | |
* to [email protected] so we can send you a copy immediately. | |
* | |
* DISCLAIMER | |
* | |
* Do not edit or add to this file if you wish to upgrade to newer | |
* versions in the future. If you wish to customize the extension for your | |
* needs please refer to http://www.mageplugins.net for more information. | |
* | |
* @category MP | |
* @package MP_Recurring | |
* @copyright Copyright (c) 2006-2017 Mage Plugins, LLC. and affiliates (https://mageplugins.net/) | |
* @license https://mageplugins.net/commercial-license/ Mage Plugins Commercial License (MPCL 1.0) | |
*/ | |
/** | |
* Class MP_Recurring_Test_Model_OrderTest | |
* | |
* @license Copyright: MP, 2016 | |
* @link https://merchantprotocol.com | |
* | |
* @covers MP_Recurring_Model_Order | |
* @codeCoverageIgnore | |
*/ | |
class MP_Recurring_Test_Model_Order extends EcomDev_PHPUnit_Test_Case | |
{ | |
/** | |
* Prepares the testing environment | |
* Star session issue | |
* @link https://github.com/EcomDev/EcomDev_PHPUnit/issues/206 | |
*/ | |
public function setUp() | |
{ | |
parent::setUp(); | |
@session_start(); | |
} | |
/** | |
* Testing to ensure that our date functions are accurate | |
*/ | |
public function testDateHasPassedOrIsToday() | |
{ | |
$dateHelper = Mage::helper('mp_recurring/date')->setTimeOffset(0); | |
$tonight = $dateHelper->sqlDateToday(); | |
$tonightParsed = date_parse($tonight); | |
$tomorrowMorningTime = mktime(0,0,1,$tonightParsed['month'],$tonightParsed['day']+1,$tonightParsed['year']); | |
$tomorrowMorning = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, $tomorrowMorningTime); | |
$yesterMorningTime = mktime(0,0,1,$tonightParsed['month'],$tonightParsed['day']-1,$tonightParsed['year']); | |
$yesterMorning = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, $yesterMorningTime); | |
$weekAgoTime = mktime(0,0,1,$tonightParsed['month'],$tonightParsed['day']-7,$tonightParsed['year']); | |
$weekAgo = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, $weekAgoTime); | |
$fourDaysAgoTime = mktime(0,0,1,$tonightParsed['month'],$tonightParsed['day']-4,$tonightParsed['year']); | |
$fourDaysAgo = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, $fourDaysAgoTime); | |
$tenDaysAgoTime = mktime(0,0,1,$tonightParsed['month'],$tonightParsed['day']-10,$tonightParsed['year']); | |
$tenDaysAgo = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, $tenDaysAgoTime); | |
$oneWeekTime = mktime(0,0,1,$tonightParsed['month'],$tonightParsed['day']+7,$tonightParsed['year']); | |
$oneWeek = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, $oneWeekTime); | |
// ---------------------------------------------------- | |
$bool = $dateHelper->dateHasPassedOrIsToday($yesterMorning); | |
$this->assertTrue($bool); | |
$bool = $dateHelper->dateHasPassedOrIsToday($tonight); | |
$this->assertTrue($bool); | |
$bool = $dateHelper->dateHasPassedOrIsToday($tomorrowMorning); | |
$this->assertFalse($bool); | |
// ---------------------------------------------------- | |
// Checking the SEND EMAIL REMINDER function | |
Mage::helper('mp_recurring/emails')->setReminderEmailDaysMax(8); | |
Mage::helper('mp_recurring/emails')->setReminderEmailDaysMin(6); | |
// $max = Mage::helper('mp_recurring/emails')->getReminderEmailDaysMax(); | |
// $this->assertSame(8, $max); | |
// $min = Mage::helper('mp_recurring/emails')->getReminderEmailDaysMin(); | |
// $this->assertSame(6, $min); | |
// $bool = $dateHelper->dateIsDaysBefore($tenDaysAgo); | |
// $this->assertFalse($bool); | |
// $bool = $dateHelper->dateIsDaysBefore($weekAgo); | |
// $this->assertTrue($bool); | |
// $bool = $dateHelper->dateIsDaysBefore($fourDaysAgo); | |
// $this->assertFalse($bool); | |
// ---------------------------------------------------- | |
$bool = $dateHelper->dateHasPassed($yesterMorning); | |
$this->assertTrue($bool); | |
$bool = $dateHelper->dateHasPassed($tomorrowMorning); | |
$this->assertFalse($bool); | |
// ---------------------------------------------------- | |
$bool = $dateHelper->dateIsToday($yesterMorning); | |
$this->assertFalse($bool); | |
$bool = $dateHelper->dateIsToday($tonight); | |
$this->assertTrue($bool); | |
$bool = $dateHelper->dateIsToday($tomorrowMorning); | |
$this->assertFalse($bool); | |
// ---------------------------------------------------- | |
$int = $dateHelper->getDaysAfter($yesterMorning); | |
$this->assertSame(1, $int); | |
$int = $dateHelper->getDaysAfter($tenDaysAgo); | |
$this->assertSame(10, $int); | |
$int = $dateHelper->getDaysAfter($fourDaysAgo); | |
$this->assertSame(4, $int); | |
$int = $dateHelper->getDaysAfter($tonight, $tenDaysAgo); | |
$this->assertSame(0, $int); | |
$int = $dateHelper->getDaysAfter($tomorrowMorning, $oneWeek); | |
$this->assertSame(6, $int); | |
$int = $dateHelper->getDaysAfter($oneWeek, $tonight); | |
$this->assertSame(0, $int); | |
} | |
/** | |
* We're testing to ensure that the array intersect method returns | |
* the exact number of recurring products that we need to create | |
*/ | |
public function testArrayInterset() | |
{ | |
// SIMPLE | |
$triggers = array( | |
'A' => 1 | |
); | |
$order = array( | |
'A' => 1 | |
); | |
$qty = Mage::helper('mp_recurring')->arrayIntersectQuantity($order, $triggers); | |
$this->assertSame(1, $qty); | |
// MULTIPLE SIMPLE | |
$triggers = array( | |
'A' => 1 | |
); | |
$order = array( | |
'A' => 2 | |
); | |
$qty = Mage::helper('mp_recurring')->arrayIntersectQuantity($order, $triggers); | |
$this->assertSame(2, $qty); | |
// COMPLEX SINGLE | |
$triggers = array( | |
'A' => 1, | |
'B' => 2, | |
); | |
$order = array( | |
'A' => 1, | |
'B' => 2, | |
); | |
$qty = Mage::helper('mp_recurring')->arrayIntersectQuantity($order, $triggers); | |
$this->assertSame(1, $qty); | |
// COMPLEX MULTI | |
$triggers = array( | |
'A' => 1, | |
'B' => 2, | |
); | |
$order = array( | |
'A' => 2, | |
'B' => 4, | |
); | |
$qty = Mage::helper('mp_recurring')->arrayIntersectQuantity($order, $triggers); | |
$this->assertSame(2, $qty); | |
// ------------------ TESTING TRICKY OPTIONS | |
$triggers = array( | |
'A' => 1 | |
); | |
$order = array( | |
'A' => 0 | |
); | |
$qty = Mage::helper('mp_recurring')->arrayIntersectQuantity($order, $triggers); | |
$this->assertSame(0, $qty); | |
// COMPLEX MULTI | |
$triggers = array( | |
'A' => 1, | |
'B' => 2, | |
); | |
$order = array( | |
'A' => 1, | |
'B' => 4, | |
); | |
$qty = Mage::helper('mp_recurring')->arrayIntersectQuantity($order, $triggers); | |
$this->assertSame(1, $qty); | |
// COMPLEX MULTI | |
$triggers = array( | |
'A' => 1, | |
'B' => 2, | |
); | |
$order = array( | |
'A' => 4, | |
'B' => 4, | |
); | |
$qty = Mage::helper('mp_recurring')->arrayIntersectQuantity($order, $triggers); | |
$this->assertSame(2, $qty); | |
} | |
/** | |
* Crete recurring order | |
* | |
* @return false|Mage_Core_Model_Abstract | |
* @throws Exception | |
* @throws Mage_Core_Exception | |
*/ | |
protected function _createRecurringOrder() | |
{ | |
// Create Customer | |
$customer = $this->_createCustomer(); | |
$model = Mage::getModel('mp_recurring/order'); | |
$model->setStoreId($this->_getDefaultStoreId()); | |
$model->setEnabled(1); | |
// Setup the customer | |
$model->setCustomerId($customer->getId()); | |
$model->setCustomer($customer); | |
$model->setCustomerEmail($customer->getEmail()); | |
$address = array( | |
'prefix' => 'Mr', | |
'firstname' => 'phpunit', | |
'middlename'=> 'jack', | |
'lastname' => 'test', | |
'suffix' => 'Jr', | |
'company' => 'Merchant', | |
'street' => '113 cherry st', | |
'region' => 'Washington', | |
'region_id' => Mage::helper('mp_recurring/states')->getRegionId('Washington'), | |
'city' => 'Seattle', | |
'country_id'=> 'US', | |
'postcode' => '98104', | |
'fax' => '1245326421', | |
'telephone' => '9876754645' | |
); | |
$model->setBillingAddressData( $address ); | |
$model->setShippingAddressData( $address ); | |
$model->setShippingMethod( MP_Recurring_Helper_Data::shippingMethod ); | |
$model->savePayment(array( | |
'method' => MP_Recurring_Helper_Data::PAYMENTMETHOD, | |
'cc_owner' => 'phpunit card holder', | |
'cc_number' => '4242424242424242', | |
'cc_exp_month' => date('m'), | |
'cc_exp_year' => date('Y', strtotime('+2 years')) | |
)); | |
$product = $this->_createProduct( 'simple', 19.95 ); | |
$model->addProduct($product, 3); | |
$model->setCreatedInAdmin(1); | |
$model->save(); | |
return $model; | |
} | |
/** | |
* Clear registry for tests | |
*/ | |
protected function _clearRegistry() | |
{ | |
$keys = Mage::helper('mp_recurring/cards')->getPaymentMethods(); | |
foreach ($keys as $index => $key) { | |
Mage::unregister('_singleton/' . $key); | |
} | |
} | |
/** | |
* Create full recurring order | |
* | |
* @return Ambigous | |
*/ | |
protected function _createFullRecurringOrder() | |
{ | |
$model = Mage::getModel('mp_recurring/order'); | |
$model->setStoreId($this->_getDefaultStoreId()); | |
$model->setEnabled(1); | |
$model->setCustomerEmail('[email protected]'); | |
$address = array( | |
'prefix' => 'Mr', | |
'firstname' => 'phpunit', | |
'middlename'=> 'jack', | |
'lastname' => 'test', | |
'suffix' => 'Jr', | |
'company' => 'Merchant', | |
'street' => '113 cherry st', | |
'region' => 'Washington', | |
'region_id' => Mage::helper('mp_recurring/states')->getRegionId('Washington'), | |
'city' => 'Seattle', | |
'country_id'=> 'US', | |
'postcode' => '98104', | |
'fax' => '1245326421', | |
'telephone' => '9876754645' | |
); | |
$model->setBillingAddressData( $address ); | |
$model->setShippingAddressData( $address ); | |
$model->setShippingMethod( MP_Recurring_Helper_Data::shippingMethod ); | |
$model->savePayment(array( | |
'method' => MP_Recurring_Helper_Data::PAYMENTMETHOD, | |
'cc_owner' => 'phpunit card holder', | |
'cc_number' => '4242424242424242', | |
'cc_exp_month' => '12', | |
'cc_exp_year' => date('Y', strtotime('+2 years')) | |
)); | |
$product = $this->_createProduct( 'simple', 19.95 ); | |
$model->addProduct($product, 3); | |
$model->setOrderDate( date(MP_Recurring_Helper_Data::DATE_FORMAT) ); | |
$model->setIntervalOverride( 1 ); | |
$model->setIntervalType( 1 ); | |
$model->setInterval( 1 ); | |
$model->setCreatedInAdmin( 1 ); | |
$model->setCustomerId(100); | |
$model->setFailedAttempt(2); | |
$model->setReminderSent(1); | |
$model->addError('This is an error'); | |
$model->setShippingIndicator(2); | |
$model->setCancelling(0); | |
$model->setPeriod(2); | |
$model->setCancelReason('Phone Call'); | |
$model->save(); | |
return $model; | |
} | |
/** | |
* Test full recurring order | |
* | |
* 1. Create full recurring order | |
* 2. Checks | |
*/ | |
public function testCreateRecurringOrder() | |
{ | |
// Create and test that it saved properly | |
$model = $this->_createFullRecurringOrder(); | |
$itemsOriginal = $model->getAllItems(); | |
$this->assertNotNull($model->getId()); | |
// test that all values were created properly | |
$this->assertSame(100, $model->getCustomerId()); | |
$this->assertSame(2, $model->getFailedAttempt()); | |
$this->assertSame(1, $model->getReminderSent()); | |
$this->assertContains('This is an error', $model->getErrors()); | |
$this->assertSame(2, $model->getShippingIndicator()); | |
$this->assertFalse($model->getCancelling()); | |
// $this->assertSame(2, $model->getPeriod()); | |
$this->assertSame('Phone Call', $model->getCancelReason()); | |
$this->assertSame($this->_getDefaultStoreId(), $model->getStoreId()); | |
$this->assertSame(1, $model->getEnabled()); | |
$this->assertSame('[email protected]', $model->getCustomerEmail()); | |
$this->assertSame(date(MP_Recurring_Helper_Data::DATE_FORMAT), $model->getOrderDate()); | |
$this->assertSame(1, $model->getIntervalOverride()); | |
$this->assertSame(1, $model->getData('interval_type')); | |
$this->assertSame('weeks', $model->getIntervalType()); | |
$this->assertSame(1, $model->getInterval()); | |
$this->assertSame(1, $model->getCreatedInAdmin()); | |
$this->assertSame(MP_Recurring_Helper_Data::shippingMethod, $model->getShippingMethod()); | |
$this->assertSame(MP_Recurring_Helper_Data::PAYMENTMETHOD, $model->getPaymentMethod()); | |
$items = $model->getAllItems(); | |
foreach($items as $item) | |
{ | |
foreach($itemsOriginal as $itemO) | |
{ | |
$this->assertSame($itemO->getSku(), $item->getSku()); | |
$this->assertSame($itemO->getQty(), $item->getQty()); | |
} | |
} | |
$this->assertNotNull($model->getData('credit_card_id')); | |
$this->assertNotNull($model->getData('stripe_customer_id')); | |
// delete and test that it deleted properly | |
$model->delete(); | |
$modelDeleted = Mage::getModel('mp_recurring/order')->load($model->getId()); | |
$this->assertNull($modelDeleted->getId()); | |
} | |
/** | |
* Test the creation of a new customer | |
*/ | |
public function testCreateCustomer() | |
{ | |
$model = $this->_createCustomer(); | |
$this->assertInstanceOf('Mage_Customer_Model_Customer', $model, $model); | |
// delete and test that it deleted properly | |
$model->delete(); | |
$modelDeleted = Mage::getModel('customer/customer')->load($model->getId()); | |
$this->assertNull($modelDeleted->getId()); | |
} | |
/** | |
* Test recurring order creation simple functions | |
* 1. Create customer | |
* 2. Create recurring order | |
* 3. Create initial orders | |
* 4. Checks | |
* 5. Remove tests data | |
*/ | |
public function testRecurringPlaceOrderSimpleFunctions() | |
{ | |
$model = $this->_createRecurringOrder(); | |
// Set current months date and interval type = day | |
$model->setOrderDate( date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT) ); | |
$model->setIntervalOverride( 1 ); | |
$model->setIntervalType( 0 ); | |
$model->setInterval( 1 ); | |
$model->save(); | |
$this->assertTrue(Mage::helper('mp_recurring/date')->isUpdating($model->getUpdatedOn())); | |
$this->assertFalse(Mage::helper('mp_recurring/date')->isUpdating(strtotime('+61 minutes',strtotime($model->getUpdatedOn())))); | |
$this->assertFalse($model->getCancelling()); | |
$this->assertSame(0, $model->getPeriod()); | |
$this->assertSame(0, $model->countChildOrders()); | |
$this->assertFalse($model->wasProcessedToday()); | |
$this->assertFalse($model->isFirstRecurring()); | |
$this->assertFalse($model->isOngoingRecurring()); | |
$this->assertSame(MP_Recurring_Helper_Data::shippingMethod, $model->getQuote()->getShippingAddress()->getShippingMethod()); | |
// This is the initial order being processed | |
$order_id = $model->placeOrder(); | |
$this->assertInternalType('int', $order_id); | |
$order = Mage::getModel('sales/order')->load($order_id); | |
$this->assertNotNull($order->getId()); | |
// Check credit card expiration | |
// $this->assertFalse($model->isCardExpired()); | |
$this->assertFalse($model->getCancelling()); | |
$this->assertEquals(0, $model->getPeriod()); | |
$this->assertFalse($model->isInitialPurchase()); | |
$this->assertEquals(1, $model->getChildOrders()->count()); | |
$this->assertTrue($model->wasProcessedToday()); | |
$this->assertTrue($model->isFirstRecurring()); | |
$this->assertFalse($model->isOngoingRecurring()); | |
// This is the first recurring order being processed | |
$order_id = $model->placeOrder(); | |
$this->assertInternalType('int', $order_id); | |
$order = Mage::getModel('sales/order')->load($order_id); | |
$this->assertNotNull($order->getId()); | |
// Check credit card expiration | |
// $this->assertFalse($model->isCardExpired()); | |
$this->assertFalse($model->getCancelling()); | |
$this->assertEquals(0, $model->getPeriod()); | |
$this->assertFalse($model->isInitialPurchase()); | |
$this->assertEquals(2, $model->getChildOrders()->count()); | |
$this->assertTrue($model->wasProcessedToday()); | |
$this->assertFalse($model->isFirstRecurring()); | |
$this->assertTrue($model->isOngoingRecurring()); | |
// delete and test that it deleted properly | |
$model->delete(); | |
$modelDeleted = Mage::getModel('mp_recurring/order')->load($model->getId()); | |
$this->assertNull($modelDeleted->getId()); | |
// delete and test that it deleted properly | |
$this->_removeTestCustomer($model->getCustomerId()); | |
// delete and test that it deleted properly | |
$card = $model->getCreditCardObj(); | |
$stripeCustomerIsRemoved = $this->_removeStripeCustomer($card->getCustomer()); | |
$this->assertTrue($stripeCustomerIsRemoved); | |
} | |
/** | |
* Test complex recurring order creation | |
* 1. Create Plan | |
* 2. Create customer | |
* 3. Create recurring order | |
* 4. Create initial orders | |
* 5. Checks | |
* 6. Remove tests data | |
*/ | |
public function testComplexRecurringOrder() | |
{ | |
$dateHelper = Mage::helper('mp_recurring/date')->setTimeOffset(0); | |
$tonight = $dateHelper->sqlDateToday(); | |
$tonightParsed = date_parse($tonight); | |
$tomorrowMorningTime = mktime(0,0,1,$tonightParsed['month'],$tonightParsed['day']+1,$tonightParsed['year']); | |
$tomorrowMorning = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, $tomorrowMorningTime); | |
$plan = $this->_createPlan(); | |
$plan->setInfinite(0); | |
$plan->setConcurrences(3); | |
$plan->setCancellationFeeEnabled(1); | |
$plan->setCancellationFee(5.00); | |
$plan->setCancellationFeeWaived(0); | |
$plan->setCancellationProductId(3); | |
$plan->save(); | |
// Create recurring order | |
$recurring = $this->_createRecurringOrder(); | |
// Validate the parameters of the recurring order | |
$this->assertFalse($recurring->getCancelling()); | |
$this->assertSame(0, $recurring->getPeriod()); | |
// Setup the Plan | |
$recurring->setPlan($plan); | |
// $this->assertTrue($recurring->getCancelling()); | |
// $this->assertSame(4, $recurring->getPlan()->getCustomerGroup()); | |
// $this->assertSame(3, $recurring->getPeriod()); | |
$this->assertSame(1, (int)$recurring->getCustomer()->getGroupId()); | |
// set the trigger product to this recurring order | |
$recurring->removeAllItems(); | |
$triggerProducts = $plan->getProductsTrigger(); | |
foreach($triggerProducts as $productId => $qty) | |
{ | |
$product = Mage::getModel('catalog/product')->load($productId); | |
$recurring->addProduct($product); | |
} | |
$recurring->save(); | |
// ----------------------------- DONE WITH SETUP ---------------------- | |
// ----------------------------- TEST IS INITIAL PURCHASE ---------------------- | |
// Validate the parameters of the recurring order | |
$this->assertTrue($recurring->isInitialPurchase()); | |
$this->assertSame(1, $recurring->hasInitialTriggerProducts()); | |
$this->assertFalse($recurring->isFirstRecurring()); | |
$this->assertFalse($recurring->isOngoingRecurring()); | |
// Validate that the products got updated after the first order | |
$items = $recurring->getAllItems(); | |
foreach($items as $item) | |
{ | |
foreach($recurring->getPlan()->getProductsTrigger() as $_productId => $qty) | |
{ | |
$_product = Mage::getModel('catalog/product')->load($_productId); | |
$this->assertSame($_product->getSku(), $item->getSku()); | |
$this->assertEquals($qty, $item->getQty()); | |
} | |
} | |
// ----------------------------- INITIAL ORDER ---------------------- | |
// This is the initial order being processed | |
$order_id = $recurring->placeOrder(); | |
// $recurring->load($recurring->getId()); | |
$this->assertInternalType('int', $order_id); | |
$order = Mage::getModel('sales/order')->load($order_id); | |
$this->assertNotNull($order->getId()); | |
// ----------------------------- TEST IS FIRST RECURRING ORDER STATE ---------------------- | |
// checking what the date should be | |
$fifteenAhead = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, strtotime($recurring->firstOrderDate())); | |
$int = $dateHelper->getDaysAfter($tomorrowMorning, $fifteenAhead); | |
$this->assertSame(14, $int); | |
// making sure that we're 14 days after tomorrow | |
$fifteenAhead = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, strtotime($recurring->getOrderDate())); | |
$int = $dateHelper->getDaysAfter($tomorrowMorning, $fifteenAhead); | |
$this->assertSame(14, $int); | |
// Validate the parameters of the recurring order | |
$this->assertFalse($recurring->isInitialPurchase()); | |
$this->assertTrue($recurring->isFirstRecurring()); | |
$this->assertSame(0, $recurring->hasInitialTriggerProducts()); | |
$this->assertFalse($recurring->isOngoingRecurring()); | |
// $this->assertSame(4, $recurring->getCustomer()->getGroupId()); | |
// $this->assertSame(2, $recurring->getPeriod()); | |
// Validate that the products got updated after the first order | |
$items = $recurring->getAllItems(); | |
foreach($items as $item) | |
{ | |
foreach($recurring->getPlan()->getProductsRecurring() as $sku => $qty) | |
{ | |
$this->assertSame($sku, $item->getSku()); | |
$this->assertEquals($qty, $item->getQty()); | |
} | |
} | |
// ---------------------- PLACE (2) FIRST RECURRING ORDER --------------------- | |
// This is the initial order being processed | |
$order_id = $recurring->placeOrder(); | |
$this->assertInternalType('int', $order_id); | |
$order = Mage::getModel('sales/order')->load($order_id); | |
$this->assertNotNull($order->getId()); | |
// ----------------------------- TEST IS ONGOING ORDER ---------------------- | |
// Check credit card expiration | |
// $this->assertFalse($recurring->isCardExpired()); | |
// making sure that we're 14 days after today | |
$nextDate = date(MP_Recurring_Helper_Date::SQL_DATE_FORMAT, strtotime($recurring->getOrderDate())); | |
$int = $dateHelper->getDaysAfter($tomorrowMorning, $nextDate); | |
$this->assertSame(42, $int); // 14 + 28 | |
// $this->assertSame(1, $recurring->getPeriod()); | |
$this->assertFalse($recurring->isInitialPurchase()); | |
$this->assertSame(2, $recurring->getChildOrders()->count()); | |
$this->assertFalse($recurring->isFirstRecurring()); | |
$this->assertTrue($recurring->isOngoingRecurring()); | |
// ---------------------- THIRD AND FINAL RECURRING ORDER ------------- | |
// This is the initial order being processed | |
$order_id = $recurring->placeOrder(); | |
$this->assertInternalType('int', $order_id); | |
$order = Mage::getModel('sales/order')->load($order_id); | |
$this->assertNotNull($order->getId()); | |
// Check credit card expiration | |
// $this->assertFalse($recurring->isCardExpired()); | |
// ----------------------- SHOULD HAVE CANCELLED ---------------------- | |
// $this->assertSame(0, $recurring->getPeriod()); | |
// $this->assertFalse($recurring->isEnabled()); | |
// delete and test that it deleted properly | |
$card = $recurring->getCreditCardObj(); | |
$stripeCustomerIsRemoved = $this->_removeStripeCustomer($card->getCustomer()); | |
$this->assertTrue($stripeCustomerIsRemoved); | |
// delete and test that it deleted properly | |
$recurring->delete(); | |
$modelDeleted = Mage::getModel('mp_recurring/order')->load($recurring->getId()); | |
$this->assertNull($modelDeleted->getId()); | |
// // delete and test that it deleted properly | |
$this->_removeTestCustomer($recurring->getCustomerId()); | |
// delete and test that it deleted properly | |
$plan->delete(); | |
$modelDeleted = Mage::getModel('mp_recurring/plan')->load($plan->getId()); | |
$this->assertNull($modelDeleted->getId()); | |
} | |
/** | |
* RReturn default store id | |
* | |
* @return mixed | |
* @throws Mage_Core_Exception | |
*/ | |
protected function _getDefaultStoreId() | |
{ | |
return Mage::app() | |
->getWebsite(true) | |
->getDefaultGroup() | |
->getDefaultStoreId(); | |
} | |
/** | |
* Remove stripe customer | |
* | |
* @param $stripeId | |
* @return bool | |
*/ | |
protected function _removeStripeCustomer($stripeId) | |
{ | |
try | |
{ | |
$resource = Mage::getSingleton('core/resource'); | |
$connection = $resource->getConnection('core_write'); | |
$condition = array( | |
$connection->quoteInto('stripe_id=?', $stripeId) | |
); | |
$connection->delete( | |
Mage::getConfig()->getTablePrefix() . 'cryozonic_stripesubscriptions_customers', | |
$condition | |
); | |
} | |
catch (Exception $e) | |
{ | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Delete and test deleted customer | |
* | |
* @param $customerId | |
* @throws Exception | |
*/ | |
protected function _removeTestCustomer($customerId) | |
{ | |
$customer = Mage::getModel('customer/customer')->load($customerId); | |
$customer->delete(); | |
$modelDeleted = Mage::getModel('customer/customer')->load($customer->getId()); | |
$this->assertNull($modelDeleted->getId()); | |
} | |
/** | |
* Create customer | |
* | |
* @return Ambigous <Mage_Core_Model_Abstract, false> | |
*/ | |
protected function _createCustomer() | |
{ | |
$customer = Mage::getModel('customer/customer'); | |
$customer | |
->setConfirmation(null) | |
->setWebsiteId($this->_getDefaultWebsiteId()) | |
->setFirstname('john') | |
->setLastname('doe') | |
->setEmail(rand(1,10000).'@example.com') | |
->setPassword($customer->generatePassword(10)) | |
->setGroupId(1); | |
try { | |
$customer->save(); | |
} catch(Exception $e) { | |
return $e->getMessage(); | |
} | |
return $customer; | |
} | |
/** | |
* Create product for order | |
* | |
* @param $sku | |
* @param $price | |
* @return false|Mage_Core_Model_Abstract | |
*/ | |
protected function _createProduct( $sku, $price ) | |
{ | |
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); | |
$product = Mage::getModel('catalog/product'); | |
$product | |
->setStoreId($this->_getDefaultStoreId()) //you can set data in store scope | |
->setWebsiteIds(array($this->_getDefaultWebsiteId())) //website ID the product is assigned to, as an array | |
->setAttributeSetId(4) //ID of a attribute set named 'default' | |
->setTypeId('simple') //product type | |
->setCreatedAt(strtotime('now')) //product creation time | |
// ->setUpdatedAt(strtotime('now')) //product update time | |
->setSku(rand(0,1000).$sku) //SKU | |
->setName($sku.' '.date('H:i:s')) //product name | |
->setWeight(4.0000) | |
->setStatus(1) //product status (1 - enabled, 2 - disabled) | |
->setTaxClassId(2) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping) | |
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility | |
// ->setManufacturer(28) //manufacturer id | |
// ->setColor(24) | |
// ->setNewsFromDate('06/26/2014') //product set as new from | |
// ->setNewsToDate('06/30/2014') //product set as new to | |
// ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code) | |
->setPrice($price) //price in form 11.22 | |
// ->setCost(22.33) //price in form 11.22 | |
// ->setSpecialPrice(00.44) //special price in form 11.22 | |
// ->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY) | |
// ->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY) | |
// ->setMsrpEnabled(1) //enable MAP | |
// ->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config) | |
// ->setMsrp(99.99) //Manufacturer's Suggested Retail Price | |
->setMetaTitle($sku) | |
->setMetaKeyword($sku) | |
->setMetaDescription($sku) | |
->setDescription($sku) | |
->setShortDescription($sku) | |
// ->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization | |
// ->addImageToMediaGallery('media/catalog/product/1/0/10243-1.png', array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery | |
// ->setCategoryIds(array(3, 10)) //assign product to categories | |
; | |
$product->getResource()->save($product); | |
$product = Mage::getModel('catalog/product')->load($product->getId()); | |
try { | |
$stockItem = Mage::getModel('cataloginventory/stock_item'); | |
$stockItem->assignProduct($product); | |
$stockItem->setData('is_in_stock', 1); | |
$stockItem->setData('qty', 999); | |
$stockItem->save(); | |
} catch(Exception $e){} | |
return $product; | |
} | |
/** | |
* Create recurring plan | |
* | |
* @return false|Mage_Core_Model_Abstract | |
* @throws Exception | |
*/ | |
protected function _createPlan() | |
{ | |
$productTrigger = $this->_createProduct( 'trigger', 19.95 ); | |
$productRecurring = $this->_createProduct( 'recurring', 55.80 ); | |
$model = Mage::getModel('mp_recurring/plan'); | |
$model->setStatus(1); | |
$model->setName( 'PHPUnit Test Plan '.date('H:i:s') ); | |
$model->setCustomerGroup(4); | |
$model->setInitialFeeType( 0 ); | |
$model->setInitialFee(0.00); | |
$model->setStartDateType(1); | |
$model->setStartDate(15); | |
$model->setStartDateDelay(0); | |
$model->setFirstRecurringFeeType(0); | |
$model->setFirstRecurringFee(0.00); | |
$model->setIntervalType(0); | |
$model->setInterval(28); | |
$model->setFailedAttempt(3); | |
// Managing the cancellation | |
$model->setOnCancelResetGroup(1); | |
$model->setConcurrences(0); | |
$model->setInfinite(1); | |
$model->setCancellationFeeEnabled(0); | |
$model->setCancellationFee(0.00); | |
$model->setCancellationFeeWaived(0); | |
$model->setCancellationProductId(0); | |
// Sending reminder emails | |
$model->setReminder(1); | |
$model->setReminderDaysBefore(2); | |
$model->addProductsTrigger($productTrigger->getId(), 1); | |
$model->addProductsRecurring($productRecurring->getSku(), 1); | |
$model->setShippingMethod( MP_Recurring_Helper_Data::shippingMethod ); | |
$model->save(); | |
return $model; | |
} | |
/** | |
* Get default website id | |
* | |
* @return mixed | |
*/ | |
protected function _getDefaultWebsiteId() | |
{ | |
return Mage::app() | |
->getWebsite(true) | |
->getId(); | |
} | |
/** | |
* Clear registry | |
* Star session issue | |
* @link https://github.com/EcomDev/EcomDev_PHPUnit/issues/206 | |
*/ | |
public function tearDown() | |
{ | |
parent::tearDown(); | |
@session_write_close(); | |
// $this->_clearRegistry(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Merchant Protocol | |
* | |
* NOTICE OF LICENSE | |
* | |
* This source file is subject to the Merchant Protocol Commercial License (MPCL 1.0) | |
* that is bundled with this package in the file LICENSE.md. | |
* It is also available through the world-wide-web at this URL: | |
* https://merchantprotocol.com/commercial-license/ | |
* If you did not receive a copy of the license and are unable to | |
* obtain it through the world-wide-web, please send an email | |
* to [email protected] so we can send you a copy immediately. | |
* | |
* DISCLAIMER | |
* | |
* Do not edit or add to this file if you wish to upgrade to newer | |
* versions in the future. If you wish to customize the extension for your | |
* needs please refer to http://www.merchantprotocol.com for more information. | |
* | |
* @category MP | |
* @package MP_Recurring | |
* @copyright Copyright (c) 2006-2016 Merchant Protocol LLC. and affiliates (https://merchantprotocol.com/) | |
* @license https://merchantprotocol.com/commercial-license/ Merchant Protocol Commercial License (MPCL 1.0) | |
*/ | |
/** | |
* Plan Model | |
* | |
* @category MP | |
* @package MP_Recurring | |
* @author Merchant Protocol Team <[email protected]> | |
*/ | |
class MP_Recurring_Model_Plan extends Mage_Core_Model_Abstract | |
{ | |
/** | |
* | |
*/ | |
public function _construct() | |
{ | |
parent::_construct(); | |
$this->_init('mp_recurring/plan'); | |
} | |
/** | |
* | |
* @var unknown | |
*/ | |
protected $_recurring = null; | |
/** | |
* | |
* @param MP_Recurring_Model_Order $recurring | |
*/ | |
public function setRecurring( MP_Recurring_Model_Order $recurring ) | |
{ | |
$this->_recurring = $recurring; | |
} | |
/** | |
* | |
*/ | |
public function getRecurring() | |
{ | |
return $this->_recurring; | |
} | |
/** | |
* | |
* @return MP_Recurring_Model_Plan | |
*/ | |
public function makeDefault() | |
{ | |
$plans = Mage::getModel('mp_recurring/plan')->getCollection() | |
->setClearDefaultFilter(); | |
foreach ($plans as $plan) | |
{ | |
$plan->removeDefault(); | |
} | |
$this->setData('default', '1'); | |
$this->save(); | |
return $this; | |
} | |
/** | |
* | |
* @return MP_Recurring_Model_Plan | |
*/ | |
public function removeDefault() | |
{ | |
$this->setData('default', 0); | |
$this->save(); | |
return $this; | |
} | |
/** | |
* (non-PHPdoc) | |
* @see Mage_Core_Model_Abstract::delete() | |
*/ | |
public function delete() | |
{ | |
// we need to disable all of the child orders | |
foreach($this->getChildOrders() as $_order) | |
{ | |
$_order->setData('enabled', 0); | |
$_order->save(); | |
} | |
return parent::delete(); | |
} | |
/** | |
* This method is intended to reset the user to the default customer group if there are no more orders | |
*/ | |
public function checkOrderStatus() | |
{ | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public function isEnabled() | |
{ | |
return $this->getData('status') ?true :false; | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public function hasActiveOrders() | |
{ | |
$actives = array(); | |
foreach($this->getChildOrders() as $order) | |
{ | |
if (!$order->getEnabled()) continue; | |
$actives[] = $order; | |
} | |
return $actives; | |
} | |
/** | |
* | |
* @return mixed | |
*/ | |
public function isCancellationEnabled() | |
{ | |
return $this->getData('cancellation_fee_enabled'); | |
} | |
/** | |
* | |
* @return Mage_Category_Model_Product | |
*/ | |
public function getCancellationProduct() | |
{ | |
if (!$this->isCancellationEnabled()) return Mage::getModel('catalog/product'); | |
return Mage::getModel('catalog/product')->load($this->getData('cancellation_product_id')); | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public function getCancellationFee() | |
{ | |
if ($this->getData('cancellation_fee')) { | |
return $this->getData('cancellation_fee'); | |
} | |
$product = $this->getCancellationProduct(); | |
if (!$product->getId()) return 0; | |
return $product->getPrice(); | |
} | |
/** | |
* | |
* @return string | |
*/ | |
public function getCancellationFeeFormatted() | |
{ | |
$fee = $this->getCancellationFee(); | |
if (!$fee) return ''; | |
return Mage::helper('core')->currency($fee, true, false); | |
} | |
/** | |
* | |
* @return Ambigous <boolean, unknown> | |
*/ | |
public function getIntervalType() | |
{ | |
$types = Mage::helper('mp_recurring')->getIntervalTypeOptions(); | |
return isset($types[$this->getData('interval_type')]) ?$types[$this->getData('interval_type')] :false; | |
} | |
/** | |
* Return an array of order IDs that are associated with this plan | |
* | |
* @return Ambigous <object, boolean, Mage_Core_Model_Abstract, false> | |
*/ | |
public function getChildOrders() | |
{ | |
$collection = Mage::getModel('mp_recurring/order')->getCollection(); | |
$collection | |
->addFieldToSelect('*') | |
->addFieldToFilter('plan_id', $this->getId()); | |
return $collection; | |
} | |
/** | |
* | |
* | |
* @param array $productIds | |
* @return boolean | |
*/ | |
public function hasTriggers( $productIds ) | |
{ | |
if (!$this->getProductsTrigger()) return false; | |
$has = array(); | |
foreach($this->getProductsTrigger() as $id => $qty) | |
{ | |
if (array_key_exists($id, $productIds)) | |
{ | |
$has[$id] = $productIds[$id]; | |
} | |
} | |
return $has; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getTriggerSkus() | |
{ | |
return array_keys($this->getFormattedTriggers()); | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getFormattedTriggers() | |
{ | |
$_triggerSkus = array(); | |
$products = $this->getProductsTrigger(); | |
if (is_array($products)) | |
{ | |
foreach($products as $_productId => $_qty) | |
{ | |
$_product = Mage::getModel('catalog/product')->load($_productId); | |
if (!$_product->getId()) continue; | |
$_triggerSkus[$_product->getSku()] = $_qty; | |
} | |
} | |
return $_triggerSkus; | |
} | |
/** | |
* | |
* @return mixed | |
*/ | |
public function getProductsTrigger() | |
{ | |
$products = $this->getData('products_trigger'); | |
$products = unserialize($products); | |
return $products; | |
} | |
/** | |
* | |
* @param unknown $skus | |
*/ | |
public function setProductsTrigger($skus) | |
{ | |
foreach ($skus as $k => $v){ | |
if ($v) continue; | |
unset($skus[$k]); | |
} | |
$this->setData('products_trigger', serialize($skus)); | |
} | |
/** | |
* | |
* @param unknown $sku | |
* @return number | |
*/ | |
public function deleteProductsTrigger( $sku ) | |
{ | |
$skus = $this->getProductsTrigger(); | |
unset($skus[$sku]); | |
$this->setProductsTrigger($skus); | |
return count($this->getProductsTrigger()); | |
} | |
/** | |
* | |
* @param unknown $sku | |
* @param unknown $qty | |
*/ | |
public function updateProductsTrigger($sku, $qty) | |
{ | |
$skus = $this->getProductsTrigger(); | |
$skus[$sku] = $qty; | |
$this->setProductsTrigger($skus); | |
} | |
/** | |
* | |
* @param unknown $sku | |
* @param unknown $qty | |
*/ | |
public function addProductsTrigger($sku, $qty) | |
{ | |
$skus = $this->getProductsTrigger(); | |
if (isset($skus[$sku])) { | |
$skus[$sku] = $skus[$sku] + $qty; | |
} else { | |
$skus[$sku] = $qty; | |
} | |
$this->setProductsTrigger($skus); | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public function getInfinite() | |
{ | |
return $this->getData('infinite')?true:false; | |
} | |
/** | |
* | |
*/ | |
public function getProductsRecurring() | |
{ | |
$products = $this->getData('products_recurring'); | |
$products = unserialize($products); | |
return $products; | |
} | |
/** | |
* | |
* @param unknown $skus | |
*/ | |
public function setProductsRecurring($skus) | |
{ | |
foreach ($skus as $k => $v){ | |
if ($v) continue; | |
unset($skus[$k]); | |
} | |
$this->setData('products_recurring', serialize($skus)); | |
} | |
/** | |
* | |
* @param unknown $sku | |
* @return number | |
*/ | |
public function deleteProductsRecurring( $sku ) | |
{ | |
$skus = $this->getProductsRecurring(); | |
unset($skus[$sku]); | |
$this->setProductsRecurring($skus); | |
return count($this->getProductsRecurring()); | |
} | |
/** | |
* | |
* @param unknown $sku | |
* @param unknown $qty | |
*/ | |
public function updateProductsRecurring($sku, $qty) | |
{ | |
$qty = abs($qty); | |
$skus = $this->getProductsRecurring(); | |
$skus[$sku] = $qty; | |
$this->setProductsRecurring($skus); | |
} | |
/** | |
* | |
* @param unknown $sku | |
* @param unknown $qty | |
*/ | |
public function addProductsRecurring($sku, $qty) | |
{ | |
$qty = abs($qty); | |
$skus = $this->getProductsRecurring(); | |
if (isset($skus[$sku])) { | |
$skus[$sku] = $skus[$sku] + $qty; | |
} else { | |
$skus[$sku] = $qty; | |
} | |
$this->setProductsRecurring($skus); | |
} | |
/** | |
* If model has created_on and updated_on then this fields are automatically filled when model is saved | |
* | |
* @return Mage_Core_Model_Abstract | |
*/ | |
protected function _beforeSave() | |
{ | |
if (!$this->getId()) { | |
$this->setData('created_on', now()); | |
} | |
return parent::_beforeSave(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment