Skip to content

Commit

Permalink
Merge pull request #70 from tattersoftware/attr
Browse files Browse the repository at this point in the history
Attribute Shorthand
  • Loading branch information
MGatner committed Aug 5, 2022
2 parents 030ac47 + f205585 commit 07f5dbc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/BaseAction.php
Expand Up @@ -2,6 +2,7 @@

namespace Tatter\Workflows;

use OutOfBoundsException;
use Tatter\Users\Interfaces\HasPermission;
use Tatter\Workflows\Controllers\BaseController;
use Tatter\Workflows\Entities\Job;
Expand Down Expand Up @@ -62,6 +63,24 @@ final public static function getAttributes(): array
return $attributes;
}

/**
* Returns a single attribute.
*
* @throws OutOfBoundsException
*
* @return scalar|null
*/
final public static function attr(string $key)
{
$attributes = self::getAttributes();

if (! array_key_exists($key, $attributes)) {
throw new OutOfBoundsException('Attribute does not exist: ' . $key);
}

return $attributes[$key];
}

/**
* Runs on a Job when it progresses through the workflow.
* May throw a WorkflowsException to halt and display a message.
Expand Down
23 changes: 14 additions & 9 deletions tests/unit/BaseActionTest.php
@@ -1,14 +1,5 @@
<?php

/**
* This file is part of Tatter Workflows.
*
* (c) 2021 Tatter Software
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

use CodeIgniter\Config\Factories;
use CodeIgniter\Test\CIUnitTestCase;
use Tatter\Workflows\BaseAction;
Expand Down Expand Up @@ -45,4 +36,18 @@ protected function initialize(): void

$this->assertTrue($action->initialized);
}

public function testAttributes(): void
{
$action = new class (new Job()) extends BaseAction {
public const ATTRIBUTES = [
'name' => 'Banana',
];
};
$result = $action::getAttributes();

$this->assertSame('fas fa-tasks', $result['icon']);
$this->assertSame('Banana', $result['name']);
$this->assertSame('Banana', $action::attr('name'));
}
}

0 comments on commit 07f5dbc

Please sign in to comment.