33namespace Osiset \ShopifyApp \Test \Http \Middleware ;
44
55use Illuminate \Auth \AuthManager ;
6+ use Illuminate \Support \Facades \Request ;
67use Osiset \ShopifyApp \Http \Middleware \Billable as BillableMiddleware ;
78use Osiset \ShopifyApp \Storage \Models \Charge ;
89use Osiset \ShopifyApp \Storage \Models \Plan ;
@@ -25,13 +26,15 @@ public function setUp(): void
2526
2627 public function testEnabledBillingWithUnpaidShop (): void
2728 {
29+ $ currentRequest = Request::instance ();
30+ $ newRequest = $ currentRequest ->duplicate (['shop ' => 'mystore123.myshopify.com ' ]);
31+
2832 // Enable billing and set a shop
29- $ shop = factory ($ this ->model )->create ();
30- $ this ->auth ->login ($ shop );
3133 $ this ->app ['config ' ]->set ('shopify-app.billing_enabled ' , true );
34+ factory ($ this ->model )->create (['name ' => 'mystore123.myshopify.com ' ]);
3235
3336 // Run the middleware
34- $ result = $ this ->runMiddleware (BillableMiddleware::class);
37+ $ result = $ this ->runMiddleware (BillableMiddleware::class, $ newRequest );
3538
3639 // Assert it was not called and redirect happened
3740 $ this ->assertFalse ($ result [0 ]);
@@ -40,49 +43,65 @@ public function testEnabledBillingWithUnpaidShop(): void
4043
4144 public function testEnabledBillingWithPaidShop (): void
4245 {
46+ $ currentRequest = Request::instance ();
47+ $ newRequest = $ currentRequest ->duplicate (['shop ' => 'mystore123.myshopify.com ' ]);
48+
4349 // Enable billing and set a shop
50+ $ this ->app ['config ' ]->set ('shopify-app.billing_enabled ' , true );
51+
4452 $ plan = factory (Util::getShopifyConfig ('models.plan ' , Plan::class))->states ('type_recurring ' )->create ();
4553 $ shop = factory ($ this ->model )->create ([
54+ 'name ' => 'mystore123.myshopify.com ' ,
4655 'plan_id ' => $ plan ->getId ()->toNative (),
4756 ]);
57+
4858 factory (Util::getShopifyConfig ('models.charge ' , Charge::class))->states ('type_recurring ' )->create ([
4959 'plan_id ' => $ plan ->getId ()->toNative (),
5060 'user_id ' => $ shop ->getId ()->toNative (),
5161 ]);
5262
53- $ this ->auth ->login ($ shop );
54- $ this ->app ['config ' ]->set ('shopify-app.billing_enabled ' , true );
55-
5663 // Run the middleware
57- $ result = $ this ->runMiddleware (BillableMiddleware::class);
64+ $ result = $ this ->runMiddleware (BillableMiddleware::class, $ newRequest );
5865
5966 // Assert it was called
6067 $ this ->assertTrue ($ result [0 ]);
6168 }
6269
6370 public function testEnabledBillingWithGrandfatheredShop (): void
6471 {
72+ $ currentRequest = Request::instance ();
73+ $ newRequest = $ currentRequest ->duplicate (['shop ' => 'mystore123.myshopify.com ' ]);
74+
6575 // Enable billing and set a shop
66- $ shop = factory ($ this ->model )->states ('grandfathered ' )->create ();
67- $ this ->auth ->login ($ shop );
6876 $ this ->app ['config ' ]->set ('shopify-app.billing_enabled ' , true );
77+ factory ($ this ->model )
78+ ->states ('grandfathered ' )
79+ ->create ([
80+ 'name ' => 'mystore123.myshopify.com ' ,
81+ ]);
6982
7083 // Run the middleware
71- $ result = $ this ->runMiddleware (BillableMiddleware::class);
84+ $ result = $ this ->runMiddleware (BillableMiddleware::class, $ newRequest );
7285
7386 // Assert it was called
7487 $ this ->assertTrue ($ result [0 ]);
7588 }
7689
7790 public function testEnabledBillingWithFreemiumShop (): void
7891 {
92+ $ currentRequest = Request::instance ();
93+ $ newRequest = $ currentRequest ->duplicate (['shop ' => 'mystore123.myshopify.com ' ]);
94+
7995 // Enable billing and set a shop
80- $ shop = factory ($ this ->model )->states ('freemium ' )->create ();
81- $ this ->auth ->login ($ shop );
8296 $ this ->app ['config ' ]->set ('shopify-app.billing_enabled ' , true );
97+ factory ($ this ->model )
98+ ->states ('freemium ' )
99+ ->create ([
100+ 'name ' => 'mystore123.myshopify.com ' ,
101+ ]);
83102
84103 // Run the middleware
85- $ result = $ this ->runMiddleware (BillableMiddleware::class);
104+ $ result = $ this ->runMiddleware (BillableMiddleware::class, $ newRequest );
86105
87106 // Assert it was called
88107 $ this ->assertTrue ($ result [0 ]);
@@ -91,9 +110,8 @@ public function testEnabledBillingWithFreemiumShop(): void
91110 public function testDisabledBillingShouldPassOn (): void
92111 {
93112 // Ensure billing is disabled and set a shop
94- $ shop = factory ($ this ->model )->create ();
95- $ this ->auth ->login ($ shop );
96113 $ this ->app ['config ' ]->set ('shopify-app.billing_enabled ' , false );
114+ factory ($ this ->model )->create ();
97115
98116 // Run the middleware
99117 $ result = $ this ->runMiddleware (BillableMiddleware::class);
0 commit comments