Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

Commit

Permalink
new action for qb
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Nov 6, 2018
1 parent 347df2f commit bf2a41c
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 44 deletions.
31 changes: 15 additions & 16 deletions bin/teamcity-keykeeper
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,32 @@
use JBZoo\TeamcityKeyKeeper\KeyGetCommand;
use JBZoo\TeamcityKeyKeeper\KeyRestoreCommand;
use JBZoo\TeamcityKeyKeeper\KeySaveCommand;
use JBZoo\TeamcityKeyKeeper\QbUpdateCommand;
use JBZoo\Utils\Sys;
use Symfony\Component\Console\Application;

umask(0000);
set_time_limit(0);

define('PATH_ROOT', __DIR__ . '/../');
define('PATH_STORAGE', PATH_ROOT . '/storage');
define('PATH_ROOT', dirname(__DIR__) . '/');
require_once PATH_ROOT . '/vendor/autoload.php';

$vendorPaths = [
realpath(__DIR__ . '/vendor/autoload.php'),
realpath(__DIR__ . '/../vendor/autoload.php'),
realpath(__DIR__ . '/../../vendor/autoload.php'),
realpath(__DIR__ . '/../../../vendor/autoload.php'),
realpath(__DIR__ . '/../../../../vendor/autoload.php'),
realpath(__DIR__ . '/../../../../../vendor/autoload.php'),
realpath('./vendor/autoload.php'),
];
if ($homePath = Sys::getHome()) {
define('PATH_STORAGE_DEFAULT', $homePath . '/.teamcity-keykeeper');
} else {
define('PATH_STORAGE_DEFAULT', PATH_ROOT . '/storage');
}

foreach ($vendorPaths as $vendorPath) {
if (file_exists($vendorPath)) {
require_once $vendorPath;
break;
}
if (!is_dir(PATH_STORAGE_DEFAULT) &&
!mkdir($concurrentDirectory = PATH_STORAGE_DEFAULT, 0777, true) &&
!is_dir($concurrentDirectory)
) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
}

$application = new Application();
$application->add(new KeySaveCommand());
$application->add(new KeyRestoreCommand());
$application->add(new KeyGetCommand());
$application->add(new QbUpdateCommand());
$application->run();
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
},

"require-dev" : {
"jbzoo/phpunit" : "^2.0",
"symfony/var-dumper" : "^4.1"
"jbzoo/phpunit" : "^2.0",
"symfony/var-dumper" : "^4.1",
"quickbooks/v3-php-sdk" : "^5.0.3"
},

"bin" : [
Expand Down
6 changes: 3 additions & 3 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
class Helper
{
public const DEFAULT_GROUP = 'default';
const DEFAULT_GROUP = 'default';

/**
* @param string $key
Expand Down Expand Up @@ -86,7 +86,7 @@ public static function getStorage($storageName = self::DEFAULT_GROUP)
private static function getStoragePath($group)
{
$group = self::cleanGroup($group);
return PATH_STORAGE . "/{$group}.json";
return PATH_STORAGE_DEFAULT . "/{$group}.json";
}

/**
Expand Down Expand Up @@ -127,7 +127,7 @@ private static function log($message, $group)
{
$group = self::cleanGroup($group);
/** @noinspection ForgottenDebugOutputInspection */
error_log(date('Y-m-d H:i:s') . " {$message}\n", 3, PATH_STORAGE . "/{$group}_log.log");
error_log(date('Y-m-d H:i:s') . " {$message}\n", 3, PATH_STORAGE_DEFAULT . "/{$group}_log.log");
}

/**
Expand Down
83 changes: 83 additions & 0 deletions src/QbUpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* JBZoo TeamcityKeyKeeper
*
* This file is part of the JBZoo CCK package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package TeamcityKeyKeeper
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/TeamcityKeyKeeper
*/

namespace JBZoo\TeamcityKeyKeeper;

use JBZoo\Utils\Cli;
use QuickBooksOnline\API\Core\CoreConstants;
use QuickBooksOnline\API\DataService\DataService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class QbUpdateCommand
* @package JBZoo\TeamcityKeyKeeper
*/
class QbUpdateCommand extends Command
{
protected function configure()
{
$this
->setName('qb:update')
->setDescription('Update tokens for QuickBooks');
}

/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$scope = 'com.intuit.quickbooks.accounting';

$config = [
'auth_mode' => CoreConstants::OAUTH2,
'scope' => $scope,
'ClientID' => $this->getConfig('qb_client_id'),
'ClientSecret' => $this->getConfig('qb_client_secret'),
'QBORealmID' => $this->getConfig('qb_realm_id'),
'RedirectURI' => $this->getConfig('qb_redirect_url'),
'baseUrl' => CoreConstants::DEVELOPMENT_SANDBOX,
'accessTokenKey' => $this->getConfig('qb_access_token'),
'refreshTokenKey' => $this->getConfig('qb_refresh_token'),
];

$dataService = DataService::Configure($config);

$qbToken = $dataService->getOAuth2LoginHelper()->refreshToken();
$this->saveConfig('qb_access_token', $qbToken->getAccessToken());
$this->saveConfig('qb_refresh_token', $qbToken->getRefreshToken());

Cli::out($dataService->getCompanyInfo()->CompanyName);
}

/**
* @param string $keyName
* @return string
*/
protected function getConfig($keyName)
{
return trim(Cli::exec('teamcity-keykeeper key:get', ['name' => $keyName]));
}

/**
* @param string $keyName
* @param string $value
*/
protected function saveConfig($keyName, $value)
{
$response = trim(Cli::exec('teamcity-keykeeper key:save', ['name' => $keyName, 'value' => $value]));
Cli::out($response);
}
}
44 changes: 27 additions & 17 deletions tests/KeyKeeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use JBZoo\Utils\Cli;
use JBZoo\Utils\Str;
use JBZoo\Utils\Sys;

/**
* Class PackageTest
Expand All @@ -28,17 +29,19 @@ class KeyKeeperTest extends PHPUnit
*/
protected $bin = '';

protected $testGroup = 'phpunit';

public static function setUpBeforeClass()
{
$storageFile = __DIR__ . '/../storage/default_log.log';
$storageFile = Sys::getHome() . '/.teamcity-keykeeper/phpuni_log.log';
if (file_exists($storageFile)) {
unlink($storageFile);
}
}

public function setUp()
{
$storageFile = __DIR__ . '/../storage/default.json';
$storageFile = Sys::getHome() . '/.teamcity-keykeeper/phpunit.json';
if (file_exists($storageFile)) {
unlink($storageFile);
}
Expand All @@ -51,11 +54,13 @@ public function testSaveAndRestoreKey()
$value = Str::random(10000);
$name = Str::random();

$saveResult = trim(Cli::exec("{$this->bin} key:save --name='{$name}' --value='{$value}'"));
$saveResult = trim(Cli::exec("{$this->bin} key:save " .
"--name='{$name}' --value='{$value}' --group='{$this->testGroup}'"
));
$name = strtoupper($name);
isContain("Key '{$name}' saved", $saveResult);

$restoreResult = trim(Cli::exec("{$this->bin} key:restore --name='{$name}'"));
$restoreResult = trim(Cli::exec("{$this->bin} key:restore --name='{$name}' --group='{$this->testGroup}'"));
isSame("##teamcity[setParameter name='env.{$name}' value='{$value}']", $restoreResult);
}

Expand All @@ -64,39 +69,44 @@ public function testSpecialChars()
$value = 'qwerty1234567890-!@#$%^&*()_+.<>,;{}№';
$name = 'qwerty1234567890';

$saveResult = trim(Cli::exec("{$this->bin} key:save", ['name' => $name, 'value' => $value]));
$saveResult = trim(Cli::exec("{$this->bin} key:save", [
'group' => $this->testGroup,
'name' => $name,
'value' => $value,
]));

$name = strtoupper($name);
isContain("Key '{$name}' saved", $saveResult);

$restoreResult = trim(Cli::exec("{$this->bin} key:restore", ['name' => $name]));
$restoreResult = trim(Cli::exec("{$this->bin} key:restore", ['name' => $name, 'group' => $this->testGroup]));
isSame("##teamcity[setParameter name='env.{$name}' value='{$value}']", $restoreResult);
}

public function testGetAllKeys()
{
Cli::exec("{$this->bin} key:save --name='key1' --value='value1'");
Cli::exec("{$this->bin} key:save --name='key2' --value='value2'");
Cli::exec("{$this->bin} key:save --name='key1' --value='value1' --group='{$this->testGroup}'");
Cli::exec("{$this->bin} key:save --name='key2' --value='value2' --group='{$this->testGroup}'");

$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all"));
$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all --group='{$this->testGroup}'"));
isContain("##teamcity[setParameter name='env.KEY1' value='value1']", $restoreResult);
isContain("##teamcity[setParameter name='env.KEY2' value='value2']", $restoreResult);
}

public function testRemoveKeysWithOption()
{
Cli::exec("{$this->bin} key:save --name='key' --value='value1'");
Cli::exec("{$this->bin} key:save --name='key' --value=''");
Cli::exec("{$this->bin} key:save --name='key' --value='value1' --group='{$this->testGroup}'");
Cli::exec("{$this->bin} key:save --name='key' --value='' --group='{$this->testGroup}'");

$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all"));
$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all --group='{$this->testGroup}'"));
isContain("##teamcity[setParameter name='env.KEY' value='']", $restoreResult);
}

public function testRemoveKeysWithoutOption()
{
Cli::exec("{$this->bin} key:save --name='key' --value='value1'");
Cli::exec("{$this->bin} key:save --name='key'");
Cli::exec("{$this->bin} key:save --name='key' --value='value1' --group='{$this->testGroup}'");
Cli::exec("{$this->bin} key:save --name='key' --group='{$this->testGroup}'");

$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all"));
$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all --group='{$this->testGroup}'"));
isContain("##teamcity[setParameter name='env.KEY' value='']", $restoreResult);
}

Expand All @@ -105,8 +115,8 @@ public function testGetCleanValue()
$key = Str::random();
$value = 'qwerty1234567890-!@#$%^&*()_+.<>,;{}№\'"';

Cli::exec("{$this->bin} key:save", ['name' => $key, 'value' => $value]);
Cli::exec("{$this->bin} key:save", ['name' => $key, 'value' => $value, 'group' => $this->testGroup]);

isSame($value, Cli::exec("{$this->bin} key:get --name='{$key}'"));
isSame($value, Cli::exec("{$this->bin} key:get --name='{$key}' --group='{$this->testGroup}'"));
}
}
30 changes: 30 additions & 0 deletions tests/QuickBooksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* JBZoo TeamcityKeyKeeper
*
* This file is part of the JBZoo CCK package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package TeamcityKeyKeeper
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/TeamcityKeyKeeper
*/

namespace JBZoo\PHPUnit;

use JBZoo\Utils\Cli;

/**
* Class QuickBooksTest
* @package JBZoo\PHPUnit
*/
class QuickBooksTest extends PHPUnit
{
public function testUpdate()
{
$saveResult = Cli::exec('teamcity-keykeeper qb:update');
isContain("DON'T TOUCH IT! IT'S FOR CI!", $saveResult);
}
}
6 changes: 0 additions & 6 deletions tests/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,3 @@
echo 'Please execute "composer update" !' . PHP_EOL;
exit(1);
}


// Just example
if ($fixturesPath = realpath(PROJECT_TESTS . '/fixtures.php')) {
require_once PROJECT_TESTS . '/fixtures.php';
}

0 comments on commit bf2a41c

Please sign in to comment.