Skip to content

Commit

Permalink
Add menus and tests for everhour
Browse files Browse the repository at this point in the history
  • Loading branch information
godbout committed Jun 23, 2019
1 parent 7a6c9d3 commit 9a94837
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/Menus/Everhour/SetupApikey.php
@@ -0,0 +1,40 @@
<?php

namespace Godbout\Alfred\Time\Menus\Everhour;

use Godbout\Alfred\Workflow\Icon;
use Godbout\Alfred\Workflow\Item;
use Godbout\Alfred\Time\Menus\Menu;
use Godbout\Alfred\Workflow\ScriptFilter;

class SetupApikey extends Menu
{
public static function scriptFilter()
{
ScriptFilter::add(
self::apikey(),
self::back()
);
}

private static function apikey()
{
global $argv;

return Item::create()
->title('Enter your API token above')
->subtitle('Save ' . self::userInput())
->arg('everhour_setup_apikey_save')
->variable('everhour_apikey', self::userInput())
->icon(Icon::create('resources/icons/everhour.png'));
}

private static function back()
{
return Item::create()
->title('Back')
->subtitle('Go back to Everhour options')
->arg('everhour_setup')
->icon(Icon::create('resources/icons/everhour.png'));
}
}
46 changes: 46 additions & 0 deletions src/Menus/Everhour/SetupApikeySave.php
@@ -0,0 +1,46 @@
<?php

namespace Godbout\Alfred\Time\Menus\Everhour;

use Godbout\Alfred\Time\Workflow;
use Godbout\Alfred\Workflow\Icon;
use Godbout\Alfred\Workflow\Item;
use Godbout\Alfred\Time\Menus\Menu;
use Godbout\Alfred\Workflow\ScriptFilter;

class SetupApikeySave extends Menu
{
public static function scriptFilter()
{
self::saveApikey();

ScriptFilter::add(
self::apikeySaved(),
self::back()
);
}

private static function saveApikey()
{
Workflow::getConfig()->write('everhour.api_token', getenv('everhour_apikey'));
}

private static function apikeySaved()
{
return Item::create()
->title('API KEY SAVED!')
->subtitle('You can just press Enter.')
->icon(Icon::create('resources/icons/everhour.png'))
->arg('do')
->variable('timer_action', 'exit');
}

private static function back()
{
return Item::create()
->title('Back')
->subtitle('Go back to Everhour Setup')
->arg('everhour_setup')
->icon(Icon::create('resources/icons/everhour.png'));
}
}
63 changes: 63 additions & 0 deletions src/Menus/Everhour/SetupState.php
@@ -0,0 +1,63 @@
<?php

namespace Godbout\Alfred\Time\Menus\Everhour;

use Godbout\Alfred\Time\Workflow;
use Godbout\Alfred\Workflow\Icon;
use Godbout\Alfred\Workflow\Item;
use Godbout\Alfred\Time\Menus\Menu;
use Godbout\Alfred\Workflow\ScriptFilter;

class SetupState extends Menu
{
public static function scriptFilter()
{
self::saveState();

ScriptFilter::add(
self::stateSaved(),
self::back()
);
}

private static function saveState()
{
Workflow::disableAllServices();

self::toEnable() ? Workflow::enableService('everhour') : Workflow::disableService('everhour');
}

private static function stateSaved()
{
return Item::create()
->title(self::stateTitle())
->subtitle(self::stateSubtitle())
->icon(Icon::create('resources/icons/everhour.png'))
->arg('do')
->variable('timer_action', 'exit');
}

private static function stateTitle()
{
return 'Everhour ' . (self::toEnable() ? 'ENABLED!' : 'DISABLED!');
}

protected static function stateSubtitle()
{
return (self::toEnable() ? 'Other services disabled. ' : '') . 'You may press enter to quit the workflow';
}

private static function back()
{
return Item::create()
->title('Back')
->subtitle('Go back to Everhour options')
->arg('everhour_setup')
->icon(Icon::create('resources/icons/everhour.png'));
}

protected static function toEnable()
{
return getenv('everhour_enabled') === 'true';
}
}
105 changes: 103 additions & 2 deletions tests/Feature/EverhourSetupMenusTest.php
Expand Up @@ -7,6 +7,16 @@

class EverhourSetupMenusTest extends TestCase
{
/** @test */
public function it_proposes_to_enter_service_options_if_service_setup_is_chosen_and_offers_a_go_back_option()
{
$output = $this->reachEverhourSetupMenu();

$this->assertStringContainsString('everhour_setup_apikey"', $output);
$this->assertStringContainsString('everhour_setup_state"', $output);
$this->assertStringContainsString('"setup"', $output);
}

/** @test */
public function it_shows_setting_an_api_key_if_none_is_saved_in_the_config_yet()
{
Expand All @@ -18,8 +28,99 @@ public function it_shows_setting_an_api_key_if_none_is_saved_in_the_config_yet()
}

/** @test */
public function some_more_test()
public function it_shows_updating_an_api_key_is_one_is_found_in_the_config()
{
$this->everhourApikey('507f-ef41-c355b1-992023-06d0dff9');

$output = $this->reachEverhourSetupMenu();

$this->assertStringContainsString('"subtitle":"Current API KEY: 507f-ef41-c..."', $output);
}

/** @test */
public function it_proposes_to_save_service_api_key_if_service_setup_apikey_is_chosen_and_offers_a_go_back_options()
{
$output = $this->reachEverhourApikeySetupMenu();

$this->assertStringContainsString('everhour_setup_apikey_save"', $output);
$this->assertStringContainsString('everhour_setup"', $output);
}

/** @test */
public function it_can_save_the_api_key_of_the_user_in_the_config_file()
{
$apiKey = '507f-ef41-c355b1-992023-06d0dff9';

$output = $this->reachEverhourApikeySavedMenu("everhour_apikey=$apiKey");

$fileContentAsArray = json_decode(file_get_contents($this->configFile), true);
$this->assertArrayHasKey('api_token', $fileContentAsArray['everhour']);
$this->assertSame($apiKey, $fileContentAsArray['everhour']['api_token']);
}

/** @test */
public function it_can_enable_the_service()
{
$output = $this->reachEverhourStateSavedMenu('everhour_enabled=true');

$fileContentAsArray = json_decode(file_get_contents($this->configFile), true);
$this->assertArrayHasKey('is_active', $fileContentAsArray['everhour']);
$this->assertSame(true, $fileContentAsArray['everhour']['is_active']);
}

/** @test */
public function it_can_disable_the_service()
{
$this->markTestIncomplete('lots of test to add for everhour');
putenv('everhour_enabled=false');

$output = $this->reachEverhourStateSavedMenu();

$fileContentAsArray = json_decode(file_get_contents($this->configFile), true);
$this->assertArrayHasKey('is_active', $fileContentAsArray['everhour']);
$this->assertSame(false, $fileContentAsArray['everhour']['is_active']);
}

/** @test */
public function it_shows_the_state_as_disabled_if_the_is_disabled()
{
Workflow::disableService('everhour');

$output = $this->reachEverhourSetupMenu();

$this->assertStringContainsString('"title":"Enable"', $output);
$this->assertStringContainsString('"subtitle":"Currently disabled"', $output);
}

/** @test */
public function it_shows_the_state_as_enabled_if_the_service_is_enabled()
{
Workflow::enableService('everhour');

$output = $this->reachEverhourSetupMenu();

$this->assertStringContainsString('"title":"Disable"', $output);
$this->assertStringContainsString('"subtitle":"Currently enabled"', $output);
}

/** @test */
public function it_allows_to_quit_the_workflow_after_apikey_is_saved()
{
Workflow::enableService('everhour');

$output = $this->reachEverhourApikeySavedMenu();

$this->assertStringContainsString('"arg":"do"', $output);
$this->assertStringContainsString('"timer_action":"exit"', $output);
}

/** @test */
public function it_allows_to_quit_the_workflow_after_status_is_changed()
{
Workflow::enableService('everhour');

$output = $this->reachEverhourStateSavedMenu();

$this->assertStringContainsString('"arg":"do"', $output);
$this->assertStringContainsString('"timer_action":"exit"', $output);
}
}
19 changes: 19 additions & 0 deletions tests/TestCase.php
Expand Up @@ -136,6 +136,11 @@ protected function reachHarvestCredentialsSetupMenu()
return $this->reachWorkflowMenu('action=harvest_setup_credentials');
}

protected function reachEverhourApikeySetupMenu()
{
return $this->reachWorkflowMenu('action=everhour_setup_apikey');
}

protected function reachTogglStateSavedMenu($envVariables = [])
{
$envVariables = array_merge(['action=toggl_setup_state'], (array) $envVariables);
Expand All @@ -150,6 +155,13 @@ protected function reachHarvestStateSavedMenu($envVariables = [])
return $this->reachWorkflowMenu($envVariables);
}

protected function reachEverhourStateSavedMenu($envVariables = [])
{
$envVariables = array_merge(['action=everhour_setup_state'], (array) $envVariables);

return $this->reachWorkflowMenu($envVariables);
}

protected function reachTogglApikeySavedMenu($envVariables = [])
{
$envVariables = array_merge(['action=toggl_setup_apikey_save'], (array) $envVariables);
Expand All @@ -164,6 +176,13 @@ protected function reachHarvestApitokenSavedMenu($envVariables = [])
return $this->reachWorkflowMenu($envVariables);
}

protected function reachEverhourApikeySavedMenu($envVariables = [])
{
$envVariables = array_merge(['action=everhour_setup_apikey_save'], (array) $envVariables);

return $this->reachWorkflowMenu($envVariables);
}

protected function reachHarvestAccountIdSavedMenu($envVariables = [])
{
$envVariables = array_merge(['action=harvest_setup_account_id_save'], (array) $envVariables);
Expand Down

0 comments on commit 9a94837

Please sign in to comment.