Skip to content

Commit 3f875ed

Browse files
committed
Organized tests and tested the console command
1 parent f56cfa8 commit 3f875ed

17 files changed

+115
-27
lines changed

src/ShopifyApp/Console/WebhookJobMakeCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function fire()
5959
'webhooks' => [
6060
[
6161
'topic' => '{$this->argument('topic')}',
62-
'address' => 'https://your-domain.com/webhook/{$this->getUrlFromName()}'
62+
'address' => 'https://your-domain.com/webhook/{$this->getUrlFromName($this->getNameInput())}'
6363
]
6464
]
6565
");
@@ -68,11 +68,12 @@ public function fire()
6868
/**
6969
* Converts the job class name into a URL endpoint
7070
*
71+
* @param string $name The name of the job
72+
*
7173
* @return string
7274
*/
73-
protected function getUrlFromName()
75+
protected function getUrlFromName(string $name)
7476
{
75-
$name = $this->getNameInput();
7677
if (Str::endsWith($name, 'Job')) {
7778
$name = substr($name, 0, -3);
7879
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Console;
2+
3+
use \ReflectionMethod;
4+
use OhMyBrew\ShopifyApp\Test\TestCase;
5+
use Symfony\Component\Console\Application as ConsoleApplication;
6+
use Symfony\Component\Console\Tester\CommandTester;
7+
use OhMyBrew\ShopifyApp\Console\WebhookJobMakeCommand;
8+
9+
class WebhookJobMakeCommandTest extends TestCase
10+
{
11+
public function testItShouldRun()
12+
{
13+
$application = new ConsoleApplication;
14+
15+
$testedCommand = $this->app->make(WebhookJobMakeCommand::class);
16+
$testedCommand->setLaravel($this->app);
17+
$application->add($testedCommand);
18+
19+
$command = $application->find('shopify-app:make:webhook');
20+
$commandTester = new CommandTester($command);
21+
22+
$commandTester->execute([
23+
'command' => $command->getName(),
24+
'name' => 'OrdersCreateJob',
25+
'topic' => 'orders/create'
26+
]);
27+
28+
$output = $commandTester->getDisplay();
29+
30+
$this->assertContains("Don't forget to register the webhook in config/shopify-app.php", $output);
31+
$this->assertContains("'address' => 'https://your-domain.com/webhook/orders-create'", $output);
32+
$this->assertContains("'topic' => 'orders/create',", $output);
33+
}
34+
35+
public function testShouldMakeUrlFromName()
36+
{
37+
$application = new ConsoleApplication;
38+
$testedCommand = $this->app->make(WebhookJobMakeCommand::class);
39+
$testedCommand->setLaravel($this->app);
40+
$application->add($testedCommand);
41+
42+
$command = $application->find('shopify-app:make:webhook');
43+
44+
$method = new ReflectionMethod($command, 'getUrlFromName');
45+
$method->setAccessible(true);
46+
47+
$result = $method->invoke($command, 'OrdersCreateJob');
48+
$result2 = $method->invoke($command, 'OrdersCreate');
49+
$result3 = $method->invoke($command, 'OrdersCreateCustomJob');
50+
51+
$this->assertEquals($result, 'orders-create');
52+
$this->assertEquals($result2, 'orders-create');
53+
$this->assertEquals($result3, 'orders-create-custom');
54+
}
55+
56+
public function testShouldReturnStub()
57+
{
58+
$application = new ConsoleApplication;
59+
$testedCommand = $this->app->make(WebhookJobMakeCommand::class);
60+
$testedCommand->setLaravel($this->app);
61+
$application->add($testedCommand);
62+
63+
$command = $application->find('shopify-app:make:webhook');
64+
65+
$method = new ReflectionMethod($command, 'getStub');
66+
$method->setAccessible(true);
67+
68+
$result = $method->invoke($command);
69+
70+
$this->assertContains('/stubs/webhook-job.stub', $result);
71+
}
72+
}

tests/AuthControllerTest.php renamed to tests/Controllers/AuthControllerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Controllers;
22

33
use Illuminate\Support\Facades\Queue;
44
use OhMyBrew\ShopifyApp\Jobs\WebhookInstaller;
55
use OhMyBrew\ShopifyApp\Jobs\ScripttagInstaller;
66
use OhMyBrew\ShopifyApp\Facades\ShopifyApp;
77
use OhMyBrew\ShopifyApp\Models\Shop;
8+
use OhMyBrew\ShopifyApp\Test\TestCase;
9+
use OhMyBrew\ShopifyApp\Test\Stubs\ApiStub;
810

911
class AuthControllerTest extends TestCase
1012
{

tests/HomeControllerTest.php renamed to tests/Controllers/HomeControllerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Controllers;
2+
3+
use OhMyBrew\ShopifyApp\Test\TestCase;
4+
use OhMyBrew\ShopifyApp\Test\Stubs\ApiStub;
25

36
class HomeControllerTest extends TestCase
47
{

tests/WebhookControllerTest.php renamed to tests/Controllers/WebhookControllerTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Controllers;
22

33
use \ReflectionMethod;
44
use Illuminate\Support\Facades\Queue;
5+
use OhMyBrew\ShopifyApp\Test\TestCase;
56

67
if (!class_exists('App\Jobs\OrdersCreateJob')) {
7-
require 'OrdersCreateJobStub.php';
8+
require __DIR__.'/../Stubs/OrdersCreateJobStub.php';
89
}
910

1011
class WebhookControllerTest extends TestCase
@@ -29,7 +30,7 @@ public function testShouldReturn201ResponseOnSuccess()
2930
'/webhook/orders-create',
3031
[], [], [],
3132
$this->headers,
32-
file_get_contents(__DIR__.'/fixtures/webhook.json')
33+
file_get_contents(__DIR__.'/../fixtures/webhook.json')
3334
);
3435
$response->assertStatus(201);
3536

@@ -44,7 +45,7 @@ public function testShouldReturnErrorResponseOnFailure()
4445
'/webhook/products-create',
4546
[], [], [],
4647
$this->headers,
47-
file_get_contents(__DIR__.'/fixtures/webhook.json')
48+
file_get_contents(__DIR__.'/../fixtures/webhook.json')
4849
);
4950
$response->assertStatus(500);
5051
$this->assertEquals('Missing webhook job: \App\Jobs\ProductsCreateJob', $response->exception->getMessage());
@@ -76,7 +77,7 @@ public function testWebhookShouldRecieveData()
7677
'/webhook/orders-create',
7778
[], [], [],
7879
$this->headers,
79-
file_get_contents(__DIR__.'/fixtures/webhook.json')
80+
file_get_contents(__DIR__.'/../fixtures/webhook.json')
8081
);
8182
$response->assertStatus(201);
8283

tests/ShopAppFacadeTest.php renamed to tests/Facades/ShopAppFacadeTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Facades;
22

33
use \ReflectionMethod;
44
use OhMyBrew\ShopifyApp\Facades\ShopifyApp;
5+
use OhMyBrew\ShopifyApp\Test\TestCase;
56

67
class ShopifyAppFacadeTest extends TestCase
78
{

tests/ScripttagInstallerJobTest.php renamed to tests/Jobs/ScripttagInstallerJobTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Jobs;
22

33
use \ReflectionObject;
44
use \ReflectionMethod;
55
use Illuminate\Support\Facades\Queue;
66
use OhMyBrew\ShopifyApp\Jobs\ScripttagInstaller;
77
use OhMyBrew\ShopifyApp\Models\Shop;
8+
use OhMyBrew\ShopifyApp\Test\TestCase;
9+
use OhMyBrew\ShopifyApp\Test\Stubs\ApiStub;
810

911
class ScripttagInstallerJobTest extends TestCase
1012
{

tests/WebhookInstallerJobTest.php renamed to tests/Jobs/WebhookInstallerJobTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Jobs;
22

33
use \ReflectionObject;
44
use \ReflectionMethod;
55
use Illuminate\Support\Facades\Queue;
66
use OhMyBrew\ShopifyApp\Jobs\WebhookInstaller;
77
use OhMyBrew\ShopifyApp\Models\Shop;
8+
use OhMyBrew\ShopifyApp\Test\TestCase;
9+
use OhMyBrew\ShopifyApp\Test\Stubs\ApiStub;
810

911
class WebhookInstallerJobTest extends TestCase
1012
{

tests/AuthShopMiddlewareTest.php renamed to tests/Middleware/AuthShopMiddlewareTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Middleware;
22

33
use OhMyBrew\ShopifyApp\Middleware\AuthShop;
44
use Illuminate\Support\Facades\Input;
5+
use OhMyBrew\ShopifyApp\Test\TestCase;
56

67
class AuthShopMiddlewareTest extends TestCase
78
{

tests/AuthWebhookMiddlewareTest.php renamed to tests/Middleware/AuthWebhookMiddlewareTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Middleware;
22

33
use OhMyBrew\ShopifyApp\Middleware\AuthWebhook;
44
use Illuminate\Support\Facades\Queue;
5+
use OhMyBrew\ShopifyApp\Test\TestCase;
56

67
if (!class_exists('App\Jobs\OrdersCreateJob')) {
7-
require 'OrdersCreateJobStub.php';
8+
require __DIR__.'/../Stubs/OrdersCreateJobStub.php';
89
}
910

1011
class AuthWebhookMiddlewareTest extends TestCase
@@ -42,7 +43,7 @@ public function testRuns()
4243
'HTTP_X_SHOPIFY_SHOP_DOMAIN' => 'example.myshopify.com',
4344
'HTTP_X_SHOPIFY_HMAC_SHA256' => '8432614ea1ce63b77959195b0e5e1e8469bfb7890e40ab51fb9c3ac26f8b050c', // Matches fixture data and API secret
4445
],
45-
file_get_contents(__DIR__.'/fixtures/webhook.json')
46+
file_get_contents(__DIR__.'/../fixtures/webhook.json')
4647
);
4748
$response->assertStatus(201);
4849
}
@@ -60,7 +61,7 @@ public function testInvalidHmacWontRun()
6061
'HTTP_X_SHOPIFY_SHOP_DOMAIN' => 'example.myshopify.com',
6162
'HTTP_X_SHOPIFY_HMAC_SHA256' => '8432614ea1ce63b77959195b0e5e1e8469bfb7890e40ab51fb9c3ac26f8b050c', // Matches fixture data and API secret
6263
],
63-
file_get_contents(__DIR__.'/fixtures/webhook.json') . 'invalid'
64+
file_get_contents(__DIR__.'/../fixtures/webhook.json') . 'invalid'
6465
);
6566
$response->assertStatus(401);
6667
}

tests/ShopModelTest.php renamed to tests/Models/ShopModelTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Models;
22

33
use OhMyBrew\ShopifyApp\Models\Shop;
4+
use OhMyBrew\ShopifyApp\Test\TestCase;
45

56
class ShopModelTest extends TestCase
67
{

tests/ShopifyAppTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use OhMyBrew\ShopifyApp\ShopifyApp;
44
use OhMyBrew\ShopifyApp\Models\Shop;
5+
use OhMyBrew\ShopifyApp\Test\Stubs\ShopModelStub;
56

67
class ShopifyAppControllerTest extends TestCase
78
{
@@ -80,10 +81,10 @@ public function testShouldUseDefaultModel()
8081
public function testShouldAllowForModelOverride()
8182
{
8283
session(['shopify_domain' => 'example.myshopify.com']);
83-
config(['shopify-app.shop_model' => 'OhMyBrew\ShopifyApp\Test\ShopModelStub']);
84+
config(['shopify-app.shop_model' => 'OhMyBrew\ShopifyApp\Test\Stubs\ShopModelStub']);
8485

8586
$shop = $this->shopifyApp->shop();
86-
$this->assertEquals('OhMyBrew\ShopifyApp\Test\ShopModelStub', get_class($shop));
87+
$this->assertEquals('OhMyBrew\ShopifyApp\Test\Stubs\ShopModelStub', get_class($shop));
8788
$this->assertEquals('hello', $shop->hello());
8889
}
8990
}

tests/ApiStub.php renamed to tests/Stubs/ApiStub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Stubs;
22

33
use OhMyBrew\BasicShopifyAPI;
44

@@ -7,7 +7,7 @@ class ApiStub extends BasicShopifyAPI
77
public function request(string $method, string $path, array $params = null)
88
{
99
$path = str_replace('/', '_', parse_url($path, PHP_URL_PATH));
10-
$filePath = __DIR__.'/fixtures/'.strtolower($method).$path;
10+
$filePath = __DIR__.'/../fixtures/'.strtolower($method).$path;
1111

1212
$responseJSON = null;
1313
if (file_exists($filePath)) {
@@ -22,7 +22,7 @@ public function request(string $method, string $path, array $params = null)
2222

2323
public function requestAccessToken(string $code)
2424
{
25-
$filePath = __DIR__.'/fixtures/post_admin_access_token.json';
25+
$filePath = __DIR__.'/../fixtures/post_admin_access_token.json';
2626
return json_decode(file_get_contents($filePath))->access_token;
2727
}
2828
}

tests/Kernel.php renamed to tests/Stubs/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Stubs;
22

33
class Kernel extends \Orchestra\Testbench\Http\Kernel
44
{
File renamed without changes.

tests/ShopModelStub.php renamed to tests/Stubs/ShopModelStub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace OhMyBrew\ShopifyApp\Test;
1+
<?php namespace OhMyBrew\ShopifyApp\Test\Stubs;
22

33
use OhMyBrew\ShopifyApp\Models\Shop as BaseShop;
44

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function getPackageAliases($app)
3636
protected function resolveApplicationHttpKernel($app)
3737
{
3838
// For adding custom the shop middleware
39-
$app->singleton('Illuminate\Contracts\Http\Kernel', 'OhMyBrew\ShopifyApp\Test\Kernel');
39+
$app->singleton('Illuminate\Contracts\Http\Kernel', 'OhMyBrew\ShopifyApp\Test\Stubs\Kernel');
4040
}
4141

4242
protected function getEnvironmentSetUp($app)

0 commit comments

Comments
 (0)