99
1010use Magento \Framework \ObjectManager \ObjectManager ;
1111use Magento \SalesRule \Model \Coupon ;
12+ use Magento \SalesRule \Model \ResourceModel \Coupon \Collection as CouponCollection ;
13+ use Magento \SalesRule \Model \ResourceModel \Coupon \CollectionFactory as CouponCollectionFactory ;
1214use Magento \SalesRule \Model \Rule ;
1315use Magento \Setup \Fixtures \CartPriceRulesFixture ;
1416use Magento \Setup \Fixtures \CouponCodesFixture ;
@@ -43,6 +45,11 @@ class CouponCodesFixtureTest extends TestCase
4345 */
4446 private $ couponCodeFactoryMock ;
4547
48+ /**
49+ * @var CouponCollectionFactory|MockObject
50+ */
51+ private $ couponCollectionFactoryMock ;
52+
4653 /**
4754 * setUp
4855 */
@@ -54,10 +61,12 @@ protected function setUp(): void
5461 \Magento \SalesRule \Model \CouponFactory::class,
5562 ['create ' ]
5663 );
64+ $ this ->couponCollectionFactoryMock = $ this ->createMock (CouponCollectionFactory::class);
5765 $ this ->model = new CouponCodesFixture (
5866 $ this ->fixtureModelMock ,
5967 $ this ->ruleFactoryMock ,
60- $ this ->couponCodeFactoryMock
68+ $ this ->couponCodeFactoryMock ,
69+ $ this ->couponCollectionFactoryMock
6170 );
6271 }
6372
@@ -66,6 +75,14 @@ protected function setUp(): void
6675 */
6776 public function testExecute ()
6877 {
78+ $ couponCollectionMock = $ this ->createMock (CouponCollection::class);
79+ $ this ->couponCollectionFactoryMock ->expects ($ this ->once ())
80+ ->method ('create ' )
81+ ->willReturn ($ couponCollectionMock );
82+ $ couponCollectionMock ->expects ($ this ->once ())
83+ ->method ('getSize ' )
84+ ->willReturn (0 );
85+
6986 $ websiteMock = $ this ->createMock (Website::class);
7087 $ websiteMock ->expects ($ this ->once ())
7188 ->method ('getId ' )
@@ -127,6 +144,14 @@ public function testExecute()
127144 */
128145 public function testNoFixtureConfigValue ()
129146 {
147+ $ couponCollectionMock = $ this ->createMock (CouponCollection::class);
148+ $ this ->couponCollectionFactoryMock ->expects ($ this ->once ())
149+ ->method ('create ' )
150+ ->willReturn ($ couponCollectionMock );
151+ $ couponCollectionMock ->expects ($ this ->once ())
152+ ->method ('getSize ' )
153+ ->willReturn (0 );
154+
130155 $ ruleMock = $ this ->createMock (Rule::class);
131156 $ ruleMock ->expects ($ this ->never ())->method ('save ' );
132157
@@ -148,6 +173,28 @@ public function testNoFixtureConfigValue()
148173 $ this ->model ->execute ();
149174 }
150175
176+ public function testFixtureAlreadyCreated ()
177+ {
178+ $ couponCollectionMock = $ this ->createMock (CouponCollection::class);
179+ $ this ->couponCollectionFactoryMock ->expects ($ this ->once ())
180+ ->method ('create ' )
181+ ->willReturn ($ couponCollectionMock );
182+ $ couponCollectionMock ->expects ($ this ->once ())
183+ ->method ('getSize ' )
184+ ->willReturn (1 );
185+
186+ $ this ->fixtureModelMock
187+ ->expects ($ this ->once ())
188+ ->method ('getValue ' )
189+ ->willReturn (1 );
190+
191+ $ this ->fixtureModelMock ->expects ($ this ->never ())->method ('getObjectManager ' );
192+ $ this ->ruleFactoryMock ->expects ($ this ->never ())->method ('create ' );
193+ $ this ->couponCodeFactoryMock ->expects ($ this ->never ())->method ('create ' );
194+
195+ $ this ->model ->execute ();
196+ }
197+
151198 /**
152199 * testGetActionTitle
153200 */
0 commit comments