mirror of
https://github.com/wearepixel/laravel-quickbooks.git
synced 2026-11-14 14:01:46 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
|
||||
namespace Wearepixel\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 QuickBooksOnline\API\DataService\DataService;
|
||||
use Wearepixel\QuickBooks\Client as QuickBooks;
|
||||
use Wearepixel\QuickBooks\TestCase;
|
||||
|
||||
/**
|
||||
* Class ControllerTest
|
||||
*/
|
||||
class ControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Controller
|
||||
*/
|
||||
protected $controller;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $data_service_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $quickbooks_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $redirector_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $request_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $session_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $view_factory_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $view_mock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->data_service_mock = Mockery::mock(DataService::class);
|
||||
$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);
|
||||
|
||||
$this->controller = new Controller();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_can_be_constructed()
|
||||
{
|
||||
$this->assertInstanceOf(Controller::class, $this->controller);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_shows_view_to_connect_if_account_not_linked()
|
||||
{
|
||||
$this->data_service_mock
|
||||
->shouldReceive('getCompanyInfo')
|
||||
->once()
|
||||
->withNoArgs()
|
||||
->andReturn([
|
||||
'name' => 'Company',
|
||||
]);
|
||||
|
||||
$this->quickbooks_mock
|
||||
->shouldReceive('hasValidRefreshToken')
|
||||
->once()
|
||||
->withNoArgs()
|
||||
->andReturnTrue();
|
||||
|
||||
$this->quickbooks_mock
|
||||
->shouldReceive('getDataService')
|
||||
->once()
|
||||
->withNoArgs()
|
||||
->andReturn($this->data_service_mock);
|
||||
|
||||
$this->view_factory_mock
|
||||
->shouldReceive('make')
|
||||
->once()
|
||||
->with('quickbooks::disconnect')
|
||||
->andReturn($this->view_mock);
|
||||
|
||||
$this->view_mock
|
||||
->shouldReceive('with')
|
||||
->once()
|
||||
->withArgs([
|
||||
'company',
|
||||
[
|
||||
'name' => 'Company',
|
||||
],
|
||||
])
|
||||
->andReturnSelf();
|
||||
|
||||
$this->controller->connect($this->quickbooks_mock, $this->view_factory_mock);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_shows_view_to_disconnect_if_account_linked()
|
||||
{
|
||||
$this->quickbooks_mock
|
||||
->shouldReceive('hasValidRefreshToken')
|
||||
->once()
|
||||
->withNoArgs()
|
||||
->andReturnFalse();
|
||||
|
||||
$this->quickbooks_mock
|
||||
->shouldReceive('authorizationUri')
|
||||
->once()
|
||||
->withNoArgs()
|
||||
->andReturn('http://uri');
|
||||
|
||||
$this->view_factory_mock
|
||||
->shouldReceive('make')
|
||||
->once()
|
||||
->with('quickbooks::connect')
|
||||
->andReturn($this->view_mock);
|
||||
|
||||
$this->view_mock
|
||||
->shouldReceive('with')
|
||||
->once()
|
||||
->withArgs(['authorization_uri', 'http://uri'])
|
||||
->andReturnSelf();
|
||||
|
||||
$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);
|
||||
$realmId = random_int(1, 9999);
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace Wearepixel\QuickBooks\Http\Middleware;
|
||||
|
||||
use Illuminate\Contracts\Routing\UrlGenerator;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Redirector;
|
||||
use Mockery;
|
||||
use Mockery\Mock;
|
||||
use Wearepixel\QuickBooks\Client as QuickBooks;
|
||||
use Wearepixel\QuickBooks\TestCase;
|
||||
|
||||
/**
|
||||
* Class FilterTest
|
||||
*/
|
||||
class FilterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Filter
|
||||
*/
|
||||
protected $filter;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $quickbooks_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $redirector_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $request_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $session_mock;
|
||||
|
||||
/**
|
||||
* @var Mock
|
||||
*/
|
||||
protected $url_generator_mock;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$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(Session::class);
|
||||
$this->url_generator_mock = Mockery::mock(UrlGenerator::class);
|
||||
|
||||
$this->filter = new Filter(
|
||||
$this->quickbooks_mock,
|
||||
$this->redirector_mock,
|
||||
$this->session_mock,
|
||||
$this->url_generator_mock,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_can_be_constructed()
|
||||
{
|
||||
$this->assertInstanceOf(Filter::class, $this->filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_just_passes_the_request_to_the_next_middleware_if_account_linked_to_quickbooks()
|
||||
{
|
||||
$next_middleware = function ($request) {
|
||||
$this->assertEquals($this->request_mock, $request);
|
||||
};
|
||||
|
||||
$this->quickbooks_mock
|
||||
->shouldReceive('hasValidRefreshToken')
|
||||
->once()
|
||||
->withNoArgs()
|
||||
->andReturnTrue();
|
||||
|
||||
$this->filter->handle($this->request_mock, $next_middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_redirects_to_quickbooks_connect_route_after_setting_intended_session_if_account_not_linked()
|
||||
{
|
||||
$this->request_mock
|
||||
->shouldReceive('path')
|
||||
->once()
|
||||
->withNoArgs()
|
||||
->andReturn('path');
|
||||
|
||||
$this->url_generator_mock
|
||||
->shouldReceive('to')
|
||||
->once()
|
||||
->with('path')
|
||||
->andReturn('http://to/path');
|
||||
|
||||
$this->session_mock
|
||||
->shouldReceive('put')
|
||||
->once()
|
||||
->withArgs(['url.intended', 'http://to/path'])
|
||||
->andReturnNull();
|
||||
|
||||
$this->redirector_mock
|
||||
->shouldReceive('route')
|
||||
->once()
|
||||
->with('quickbooks.connect')
|
||||
->andReturnSelf();
|
||||
|
||||
$next_middleware = function ($request) {
|
||||
// If this is called, then fail test
|
||||
$this->assertTrue(false);
|
||||
};
|
||||
|
||||
$this->quickbooks_mock
|
||||
->shouldReceive('hasValidRefreshToken')
|
||||
->once()
|
||||
->withNoArgs()
|
||||
->andReturnFalse();
|
||||
|
||||
$this->filter->handle($this->request_mock, $next_middleware);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user