Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Dec 14, 2024
1 parent 1bbadf7 commit ff43384
Show file tree
Hide file tree
Showing 26 changed files with 1,366 additions and 97 deletions.
47 changes: 20 additions & 27 deletions src/AutomationRules/Actions/SendMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,26 @@ protected function getRecipientAddress($params)
return [$address => $name];
case 'location':
$location = array_get($params, 'location');
if (empty($location->location_email) && empty($location->location_name)) {
return null;
}

return [$location->location_email => $location->location_name];
case 'staff_group':
if ($groupId = $this->model->staff_group) {
if (!$staffGroup = UserGroup::find($groupId)) {
throw new AutomationException('Unable to find staff group with ID: '.$groupId);
}

return $staffGroup->users->pluck('name', 'email')->all();
$groupId = $this->model->staff_group;
if (!$groupId || !$staffGroup = UserGroup::find($groupId)) {
throw new AutomationException('SendMailTemplate: Unable to find staff group with ID: '.$groupId);
}

return null;
return $staffGroup->users->pluck('name', 'email')->all();
case 'customer_group':
if ($groupId = $this->model->customer_group) {
if (!$customerGroup = CustomerGroup::find($groupId)) {
throw new AutomationException('Unable to find customer group with ID: '.$groupId);
}

return $customerGroup->customers()
->select('email', DB::raw('concat(first_name, last_name) as full_name'))
->whereIsEnabled()
->pluck('full_name', 'email')
->all();
$groupId = $this->model->customer_group;
if (!$groupId || !$customerGroup = CustomerGroup::find($groupId)) {
throw new AutomationException('SendMailTemplate: Unable to find customer group with ID: '.$groupId);
}

return null;
return $customerGroup->customers()
->select('email', DB::raw('concat(first_name, last_name) as full_name'))
->whereIsEnabled()
->pluck('full_name', 'email')
->all();
case 'customer':
$customer = array_get($params, 'customer');
if (!empty($customer->email) && !empty($customer->full_name)) {
Expand All @@ -165,22 +156,24 @@ protected function getRecipientAddress($params)
return [$params['email'] => $fullName];
}

return null;
throw new AutomationException('SendMailTemplate: Missing a valid customer email address');
case 'staff':
$staff = array_get($params, 'staff');
if (!empty($staff->staff_email) && !empty($staff->staff_name)) {
return [$staff->staff_email => $staff->staff_name];
if (!empty($staff->email) && !empty($staff->name)) {
return [$staff->email => $staff->name];
}

$orderOrReservation = array_get($params, 'order', array_get($params, 'reservation'));
if ($orderOrReservation && $staff = $orderOrReservation->assignee) {
return [$staff->staff_email => $staff->staff_name];
return [$staff->email => $staff->name];
}

throw new AutomationException('SendMailTemplate: Missing a valid staff email address');
case 'all_staff':
return User::whereIsEnabled()->pluck('staff_name', 'staff_email')->all();
return User::whereIsEnabled()->pluck('name', 'email')->all();
case 'all_customer':
return Customer::whereIsEnabled()
->select('email', 'concat(first_name, last_name) as full_name')
->selectRaw('email, concat(first_name, last_name) as full_name')
->pluck('full_name', 'email')
->all();
}
Expand Down
12 changes: 2 additions & 10 deletions src/Classes/BaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ public function getEventGroup()
public function getEventIdentifier()
{
$namespace = normalize_class_name(get_called_class());
if (strpos($namespace, '\\') === null) {
return $namespace;
}

$parts = explode('\\', $namespace);
$class = array_pop($parts);
$slice = array_slice($parts, 1, 2);
Expand All @@ -116,16 +112,12 @@ public static function findRulesValues($key = null)
return $automationRules;
}

foreach ($automationRules as $extension => $automationRule) {
foreach ($automationRules as $automationRule) {
if (!$values = array_get($automationRule, $key)) {
continue;
}

if (!is_array($values)) {
$values = [$values];
}

foreach ($values as $index => $value) {
foreach ((array)$values as $index => $value) {
if (is_string($index)) {
$results[$index] = $value;
} else {
Expand Down
18 changes: 8 additions & 10 deletions src/Classes/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ public function getContextParams()
$globals = $this->registeredGlobalParams ?: [];

return [
'isAdmin' => Igniter::runningInAdmin() ? 1 : 0,
'isConsole' => App::runningInConsole() ? 1 : 0,
'appLocale' => App::getLocale(),
] + $globals;
'isAdmin' => Igniter::runningInAdmin() ? 1 : 0,
'isConsole' => App::runningInConsole() ? 1 : 0,
'appLocale' => App::getLocale(),
] + $globals;
}

/**
Expand All @@ -135,12 +135,10 @@ public function getContextParams()
*/
protected function processCallbacks()
{
if ($this->registered) {
return;
}

foreach ($this->callbacks as $callback) {
$callback($this);
if (!$this->registered) {
foreach ($this->callbacks as $callback) {
$callback($this);
}
}

$this->registered = true;
Expand Down
9 changes: 5 additions & 4 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Igniter\Admin\Widgets\Form;
use Igniter\Automation\Classes\EventManager;
use Igniter\Automation\Http\Controllers\Automations;
use Igniter\Automation\Models\AutomationLog;
use Igniter\Automation\Models\RuleAction;
use Igniter\Flame\Igniter;
use Igniter\System\Classes\BaseExtension;
use Illuminate\Console\Scheduling\Schedule;
Expand All @@ -17,6 +19,8 @@ class Extension extends BaseExtension
{
public function register()
{
parent::register();

$this->app->singleton(EventManager::class);

$this->registerConsoleCommand('automation.cleanup', Console\Cleanup::class);
Expand Down Expand Up @@ -94,10 +98,7 @@ public function registerSchedule(Schedule $schedule)
protected function extendActionFormFields()
{
Event::listen('admin.form.extendFieldsBefore', function(Form $form) {
if (!$form->getController() instanceof \Igniter\Automation\Http\Controllers\Automations) {
return;
}
if ($form->model instanceof \Igniter\Automation\Models\RuleAction) {
if ($form->getController() instanceof Automations && $form->model instanceof RuleAction) {
$form->arrayName .= '[options]';
$form->fields = array_get($form->model->getFieldConfig(), 'fields', []);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Models/AutomationLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AutomationLog extends Model
'is_success' => 'boolean',
'message' => 'string',
'params' => 'array',
'exception' => 'array',
'exception' => ['nullable', 'array'],
];

protected $casts = [
Expand Down Expand Up @@ -77,7 +77,7 @@ public function getStatusNameAttribute($value)
{
return lang($this->is_success
? 'igniter.automation::default.text_success'
: 'igniter.automation::default.text_failed'
: 'igniter.automation::default.text_failed',
);
}

Expand All @@ -97,6 +97,8 @@ public function getCreatedSinceAttribute($value)

public function prunable(): Builder
{
return static::query()->where('created_at', '<=', now()->subDays(setting('activity_log_timeout', 60)));
return static::query()
->whereNotNull('created_at')
->where('created_at', '<=', now()->subDays(setting('activity_log_timeout', 60)));
}
}
17 changes: 6 additions & 11 deletions src/Models/AutomationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Igniter\Automation\Models;

use Exception;
use Igniter\Automation\AutomationException;
use Igniter\Automation\Classes\BaseAction;
use Igniter\Automation\Classes\BaseCondition;
use Igniter\Automation\Classes\BaseEvent;
Expand Down Expand Up @@ -50,8 +51,8 @@ class AutomationRule extends Model
public function triggerRule()
{
try {
if (!$this->conditions || !$this->actions) {
return false;
if (!$this->actions || $this->actions->isEmpty()) {
throw new AutomationException('No actions found for this rule');
}

$params = $this->getEventObject()->getEventParams();
Expand Down Expand Up @@ -141,15 +142,9 @@ public function scopeApplyClass($query, $class)
*/
public function applyEventClass($class = null)
{
if (!$class) {
$class = $this->event_class;
}

if (!$class) {
return false;
}
$class ??= $this->event_class;

if (!$this->isClassExtendedWith($class)) {
if ($class && !$this->isClassExtendedWith($class)) {
$this->extendClassWith($class);
}

Expand Down Expand Up @@ -200,7 +195,7 @@ public static function syncAll()
}

if (!array_key_exists($code, $presets)) {
self::whereName($code)->delete();
self::whereCode($code)->delete();

Check failure on line 198 in src/Models/AutomationRule.php

View workflow job for this annotation

GitHub Actions / php-tests (8.3) / PHP Tests

Call to an undefined static method Igniter\Automation\Models\AutomationRule::whereCode().

Check failure on line 198 in src/Models/AutomationRule.php

View workflow job for this annotation

GitHub Actions / php-tests (8.2) / PHP Tests

Call to an undefined static method Igniter\Automation\Models\AutomationRule::whereCode().
}
}

Expand Down
16 changes: 5 additions & 11 deletions src/Models/RuleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ public function applyActionClass($class = null)
$class = $this->class_name;
}

if (!$class) {
return false;
}

if (!$this->isClassExtendedWith($class)) {
if ($class && !$this->isClassExtendedWith($class)) {
$this->extendClassWith($class);
}

Expand Down Expand Up @@ -119,12 +115,10 @@ protected function setCustomData()
}

$config = $actionObj->getFieldConfig();
if (!$fields = array_get($config, 'fields')) {
return;
if ($fields = array_get($config, 'fields')) {
$fieldAttributes = array_keys($fields);
$this->options = array_only($this->getAttributes(), $fieldAttributes);
$this->setRawAttributes(array_except($this->getAttributes(), $fieldAttributes));
}

$fieldAttributes = array_keys($fields);
$this->options = array_only($this->getAttributes(), $fieldAttributes);
$this->setRawAttributes(array_except($this->getAttributes(), $fieldAttributes));
}
}
6 changes: 1 addition & 5 deletions src/Models/RuleCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ public function applyConditionClass($class = null)
$class = $this->class_name;
}

if (!$class) {
return false;
}

if (!$this->isClassExtendedWith($class)) {
if ($class && !$this->isClassExtendedWith($class)) {
$this->extendClassWith($class);
}

Expand Down
Loading

0 comments on commit ff43384

Please sign in to comment.