diff --git a/README.md b/README.md index ea89da9..31ba345 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Install QuickBooks PHP Client: $ composer require spinen/laravel-quickbooks-client ``` -The package uses the [auto registration feature](https://laravel.com/docs/5.8/packages#package-discovery) of Laravel 5. +The package uses the [auto registration feature](https://laravel.com/docs/packages#package-discovery) of Laravel. ## Configuration diff --git a/database/migrations/2018_03_11_141103_create_quick_books_tokens_table.php b/database/migrations/2018_03_11_141103_create_quick_books_tokens_table.php index c91e4df..b7cd187 100644 --- a/database/migrations/2018_03_11_141103_create_quick_books_tokens_table.php +++ b/database/migrations/2018_03_11_141103_create_quick_books_tokens_table.php @@ -18,7 +18,7 @@ public function up() $user_id_type = DB::getSchemaBuilder() ->getColumnType('users', 'id') === 'bigint' ? 'unsignedBigInteger' : 'unsignedInteger'; - $table->unsignedBigInteger('id'); + $table->bigIncrements('id'); $table->{$user_id_type}('user_id'); $table->unsignedBigInteger('realm_id'); $table->longtext('access_token'); diff --git a/src/Client.php b/src/Client.php index d9d6831..46dd168 100644 --- a/src/Client.php +++ b/src/Client.php @@ -85,6 +85,7 @@ public function configureLogging() return $this->data_service->enableLog(); } } catch (Exception $e) { + // TODO: Figure out what to do with this exception } return $this->data_service->disableLog(); diff --git a/src/Http/Controllers/Controller.php b/src/Http/Controllers/Controller.php index add5f7b..ff4ce77 100644 --- a/src/Http/Controllers/Controller.php +++ b/src/Http/Controllers/Controller.php @@ -53,13 +53,11 @@ public function connect(QuickBooks $quickbooks, ViewFactory $view_factory) * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View * @throws \Exception */ - public function disconnect(Redirector $redirector, QuickBooks $quickbooks) + public function disconnect(Redirector $redirector, Request $request, QuickBooks $quickbooks) { $quickbooks->deleteToken(); - // TODO: Figure out where to put this in session & remove Facade - Alert::success('Disconnected from QuickBooks') - ->flash(); + $request->session()->flash('success', 'Disconnected from QuickBooks'); return $redirector->back(); } @@ -84,7 +82,8 @@ public function token(Redirector $redirector, Request $request, QuickBooks $quic // TODO: Deal with exceptions $quickbooks->exchangeCodeForToken($request->get('code'), $request->get('realmId')); - return $redirector->intended($url_generator->route('quickbooks.connect')) - ->with('success', 'Connected to QuickBooks'); + $request->session()->flash('success', 'Connected to QuickBooks'); + + return $redirector->intended($url_generator->route('quickbooks.connect')); } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index a9058a9..5d97044 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -137,7 +137,7 @@ public function it_returns_a_data_service_with_oauth_token_when_valid_access_tok /** * @test */ - public function it_returns_a_data_service_with_refreshed_token_when_acccess_token_expired_but_refresh_token_valid() + public function it_returns_a_data_service_with_refreshed_token_when_access_token_expired_but_refresh_token_valid() { $this->expectException(ServiceException::class); diff --git a/tests/HasQuickBooksTokenTest.php b/tests/HasQuickBooksTokenTest.php index 70a4881..888b329 100644 --- a/tests/HasQuickBooksTokenTest.php +++ b/tests/HasQuickBooksTokenTest.php @@ -36,7 +36,7 @@ public function it_can_be_constructed() */ public function it_has_a_hasOne_relationship_to_token() { - // The stub is just returing the relationship name, so making sure that it is the Token class + // The stub is just returning the relationship name, so making sure that it is the Token class $this->assertEquals(Token::class, $this->user->quickBooksToken()); } } diff --git a/tests/Http/Controllers/ControllerTest.php b/tests/Http/Controllers/ControllerTest.php index 78c7d3b..f4fdcf4 100644 --- a/tests/Http/Controllers/ControllerTest.php +++ b/tests/Http/Controllers/ControllerTest.php @@ -2,8 +2,13 @@ namespace Spinen\QuickBooks\Http\Controllers; +use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Contracts\View\View; +use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Request; +use Illuminate\Routing\Redirector; +use Illuminate\Session\Store; use Mockery; use Mockery\Mock; use Spinen\QuickBooks\Client as QuickBooks; @@ -31,6 +36,21 @@ class ControllerTest extends TestCase */ protected $quickbooks_mock; + /** + * @var Mock + */ + protected $redirector_mock; + + /** + * @var Mock + */ + protected $request_mock; + + /** + * @var Mock + */ + protected $session_mock; + /** * @var Mock */ @@ -45,6 +65,9 @@ protected function setUp(): void { $this->data_service_mock = Mockery::mock(); $this->quickbooks_mock = Mockery::mock(QuickBooks::class); + $this->redirector_mock = Mockery::mock(Redirector::class); + $this->request_mock = Mockery::mock(Request::class); + $this->session_mock = Mockery::mock(Store::class); $this->view_factory_mock = Mockery::mock(ViewFactory::class); $this->view_mock = Mockery::mock(View::class); @@ -129,4 +152,78 @@ public function it_shows_view_to_disconnect_if_account_linked() $this->controller->connect($this->quickbooks_mock, $this->view_factory_mock); } + + /** + * @test + */ + public function it_disconnects_from_quickbooks_when_requested() + { + $this->request_mock->shouldReceive('session') + ->once() + ->andReturn($this->session_mock); + + $this->session_mock->shouldReceive('flash') + ->once() + ->withAnyArgs(); + + $this->redirector_mock->shouldReceive('back') + ->once() + ->andReturn(new RedirectResponse('/test', 302)); + + $this->quickbooks_mock->shouldReceive('deleteToken') + ->once() + ->withNoArgs(); + + $result = $this->controller->disconnect($this->redirector_mock, $this->request_mock, $this->quickbooks_mock); + + $this->assertInstanceOf(RedirectResponse::class, $result); + } + + /** + * @test + */ + public function it_finishes_connecting_to_quickbooks_when_given_a_valid_token_by_quickbooks() + { + $this->url_generator_mock = Mockery::mock(UrlGenerator::class); + + $this->quickbooks_mock->shouldReceive('exchangeCodeForToken') + ->once() + ->withArgs(['code', 'realmId']); + + $this->request_mock->shouldReceive('get') + ->once() + ->withArgs(['code']) + ->andReturn('code'); + + $this->request_mock->shouldReceive('get') + ->once() + ->withArgs(['realmId']) + ->andReturn('realmId'); + + $this->request_mock->shouldReceive('session') + ->once() + ->andReturn($this->session_mock); + + $this->session_mock->shouldReceive('flash') + ->once() + ->withAnyArgs(); + + $this->redirector_mock->shouldReceive('intended') + ->once() + ->withAnyArgs() + ->andReturn(new RedirectResponse('/test', 302)); + + $this->url_generator_mock->shouldReceive('route') + ->withArgs(['quickbooks.connect']) + ->once(); + + $result = $this->controller->token( + $this->redirector_mock, + $this->request_mock, + $this->quickbooks_mock, + $this->url_generator_mock + ); + + $this->assertInstanceOf(RedirectResponse::class, $result); + } }