1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2014 eBay Enterprise, Inc. |
4
|
|
|
* |
5
|
|
|
* NOTICE OF LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
8
|
|
|
* that is bundled with this package in the file LICENSE.md. |
9
|
|
|
* It is also available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* @copyright Copyright (c) 2013-2014 eBay Enterprise, Inc. (http://www.ebayenterprise.com/) |
13
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
require_once 'EbayEnterprise/GiftCard/controllers/CartController.php'; |
17
|
|
|
|
18
|
|
|
class EbayEnterprise_GiftCard_Test_Model_CartControllerTest extends EbayEnterprise_Eb2cCore_Test_Base |
19
|
|
|
{ |
20
|
|
|
const REDIRECT_PATH = EbayEnterprise_GiftCard_CartController::REDIRECT_PATH; |
21
|
|
|
|
22
|
|
|
protected $container; |
23
|
|
|
protected $giftCard; |
24
|
|
|
protected $helper; |
25
|
|
|
protected $controller; |
26
|
|
|
protected $giftCardNumber = 'somecardnumber'; |
27
|
|
|
protected $giftCardPin = '12345678'; |
28
|
|
|
protected $checkoutSession; |
29
|
|
|
|
30
|
|
|
// prepare mocks |
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->helper = $this->getHelperMock('ebayenterprise_giftcard/data'); |
34
|
|
|
$this->helper->expects($this->any()) |
35
|
|
|
->method('__')->with($this->isType('string'))->will($this->returnArgument(0)); |
36
|
|
|
// disable constructor to prevent having to mock dependencies |
37
|
|
|
$this->checkoutSession = $this->getModelMockBuilder('checkout/session')->disableOriginalConstructor() |
38
|
|
|
->setMethods(null)->getMock(); |
39
|
|
|
$this->giftCard = $this->getMock('EbayEnterprise_GiftCard_Model_IGiftcard'); |
40
|
|
|
$this->giftCard->expects($this->any()) |
41
|
|
|
->method('setPin')->with($this->isType('string'))->will($this->returnSelf()); |
42
|
|
|
// disable constructor to avoid mocking dependencies |
43
|
|
|
$this->container = $this->getMockBuilder('EbayEnterprise_GiftCard_Model_IContainer')->disableOriginalConstructor() |
44
|
|
|
->getMock(); |
45
|
|
|
$this->container->expects($this->any()) |
46
|
|
|
->method('getGiftCard')->will($this->returnValue($this->giftCard)); |
47
|
|
|
// disable constructor to avoid mocking dependencies |
48
|
|
|
$this->controller = $this->getMockBuilder('EbayEnterprise_GiftCard_CartController')->disableOriginalConstructor() |
49
|
|
|
->setMethods(['_redirect', '_rewrite', 'setFlag', '_getCardInfoFromRequest'])->getMock(); |
50
|
|
|
$this->controller->expects($this->once()) |
51
|
|
|
->method('_getCardInfoFromRequest')->will($this->returnValue([$this->giftCardNumber, $this->giftCardPin])); |
52
|
|
|
// inject mocked dependencies |
53
|
|
|
EcomDev_Utils_Reflection::setRestrictedPropertyValue($this->controller, '_container', $this->container); |
54
|
|
|
EcomDev_Utils_Reflection::setRestrictedPropertyValue($this->controller, '_helper', $this->helper); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* verify: |
59
|
|
|
* - controller uses account number and pin to get a giftcard instance to work with. |
60
|
|
|
* - controller will call the check balance function only once |
61
|
|
|
* - controller will add the giftCard back to the container on success. |
62
|
|
|
* - controller will add a translated success message to the session. |
63
|
|
|
* - controller will attempt to redirect back to cart page when done. |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function testAddAction() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$this->giftCard->expects($this->once()) |
68
|
|
|
->method('setPin') |
69
|
|
|
->with($this->identicalTo($this->giftCardPin)) |
70
|
|
|
->will($this->returnSelf()); |
71
|
|
|
$this->helper |
72
|
|
|
->expects($this->once()) |
73
|
|
|
->method('addGiftCardToOrder') |
74
|
|
|
->with($this->identicalTo($this->giftCard), $this->identicalTo($this->container)) |
75
|
|
|
->will($this->returnSelf()); |
76
|
|
|
$this->controller |
77
|
|
|
->expects($this->once()) |
78
|
|
|
->method('_redirect') |
79
|
|
|
->with($this->identicalTo(self::REDIRECT_PATH)) |
80
|
|
|
->willReturnSelf(); |
81
|
|
|
|
82
|
|
|
// inject the session mock |
83
|
|
|
$this->replaceByMock('singleton', 'checkout/session', $this->checkoutSession); |
84
|
|
|
$this->controller->addAction(); |
85
|
|
|
|
86
|
|
|
// Successfully adding gift card to order should result in success message |
87
|
|
|
// being added to checkout session. |
88
|
|
|
$this->assertCount(1, $this->checkoutSession->getMessages()->getItemsByType(Mage_Core_Model_Message::SUCCESS)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* verify: |
93
|
|
|
* - when gift card cannot be added to the order, an error message is added |
94
|
|
|
* to the checkout session |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function testAddFail() |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$this->giftCard->expects($this->once()) |
99
|
|
|
->method('setPin') |
100
|
|
|
->with($this->identicalTo($this->giftCardPin)) |
101
|
|
|
->will($this->returnSelf()); |
102
|
|
|
$this->helper |
103
|
|
|
->expects($this->once()) |
104
|
|
|
->method('addGiftCardToOrder') |
105
|
|
|
->with($this->identicalTo($this->giftCard), $this->identicalTo($this->container)) |
106
|
|
|
->willThrowException(new EbayEnterprise_GiftCard_Exception('TEST EXCEPTION: ' . __METHOD__)); |
107
|
|
|
$this->controller |
108
|
|
|
->expects($this->once()) |
109
|
|
|
->method('_redirect') |
110
|
|
|
->with($this->identicalTo(self::REDIRECT_PATH)) |
111
|
|
|
->will($this->returnSelf()); |
112
|
|
|
|
113
|
|
|
// inject the session mock |
114
|
|
|
$this->replaceByMock('singleton', 'checkout/session', $this->checkoutSession); |
115
|
|
|
$this->controller->addAction(); |
116
|
|
|
|
117
|
|
|
// Failure to add gift card to the order should result in an error message |
118
|
|
|
// being added to the checkout session. |
119
|
|
|
$this->assertCount(1, $this->checkoutSession->getMessages()->getErrors()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* verify: |
124
|
|
|
* - controller uses account number and pin to get a giftcard instance to work with. |
125
|
|
|
* - controller will use the container to remove the gift card. |
126
|
|
|
* - controller will attempt to redirect back to cart page when done. |
127
|
|
|
*/ |
128
|
|
|
public function testRemoveAction() |
129
|
|
|
{ |
130
|
|
|
$this->container->expects($this->once()) |
131
|
|
|
->method('removeGiftCard')->with($this->isInstanceOf('EbayEnterprise_GiftCard_Model_IGiftcard')) |
132
|
|
|
->will($this->returnSelf()); |
133
|
|
|
$this->controller->expects($this->once()) |
134
|
|
|
->method('_redirect')->with($this->identicalTo(self::REDIRECT_PATH))->will($this->returnSelf()); |
135
|
|
|
|
136
|
|
|
// inject the session mock |
137
|
|
|
$this->replaceByMock('singleton', 'checkout/session', $this->checkoutSession); |
138
|
|
|
$this->controller->removeAction(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.