/** * @expectedException yii\base\InvalidConfigException */ public function testAttachBehaviorFails3() { //$this->specify('the status attribute must exist in the owner model', function () { $model = new Item01(); $model->detachBehavior('workflow'); $model->attachBehavior('workflow', ['class' => ActiveWorkflowBehavior::className(), 'statusAttribute' => 'not_found']); //},['throws' => 'yii\base\InvalidConfigException']); }
public function testConfiguredWorkflowId() { $this->specify('use the configured workflow Id', function () { /** @var ActiveWorkflowBehavior|Item01 $model */ $model = new Item01(); $model->attachBehavior('workflow', ['class' => ActiveWorkflowBehavior::className(), 'defaultWorkflowId' => 'myWorkflow']); expect('model should have workflow id set to "myWorkflow"', $model->getDefaultWorkflowId() == 'myWorkflow')->true(); }); }
protected function setup() { parent::setUp(); $this->eventsBefore = []; $this->eventsAfter = []; Yii::$app->set('workflowFactory', ['class' => 'fproject\\workflow\\core\\ArrayWorkflowItemFactory', 'workflowSourceNamespace' => 'tests\\codeception\\unit\\models']); Yii::$app->set('eventSequence', ['class' => 'fproject\\workflow\\events\\ReducedEventSequence']); $this->model = new Item04(); $this->model->attachBehavior('workflow', ['class' => ActiveWorkflowBehavior::className()]); }
public function testConvertionOnLeaveWorkflow() { /** @var Item04|ActiveWorkflowBehavior $item */ $item = new Item04(); $item->attachBehavior('workflow', ['class' => ActiveWorkflowBehavior::className(), 'statusConverter' => 'converter']); $this->assertEquals(null, $item->status); $this->assertEquals('Item04Workflow/B', $item->getWorkflowStatus()->getId()); $this->specify('convertion is done when leaving workflow', function () use($item) { $item->sendToStatus(null); expect('item to not be in a workflow', $item->getWorkflow())->equals(null); expect('item to not have status', $item->hasWorkflowStatus())->false(); expect('status attribut to be converted into 55', $item->status)->equals(55); }); }
public function testInitStatusAfterFindSuccess() { $this->specify('status initialisation when reading model from db (after find)', function () { /** @var Item01|ActiveWorkflowBehavior $model */ $model = new Item01(); $model->detachBehavior('workflow'); $model->id = 1; $model->name = 'name'; $model->status = 'Workflow1/B'; $model->save(false); $model = Item01::findOne(1); $model->attachBehavior('workflow', ['class' => ActiveWorkflowBehavior::className(), 'defaultWorkflowId' => 'Workflow1']); verify('current model status is "B"', $model->getWorkflowStatus()->getId())->equals('Workflow1/B'); }); }
/** * Apply active validators for the current workflow event sequence. * * If a workflow event sequence is about to occur, this method scan all validators defined in the * owner model, and applies the ones which are valid for the upcomming events. * @param Model|ActiveWorkflowBehavior $object * @param string $attribute * @throws WorkflowException * * @see Validator::validateAttribute() * @see IEventSequence */ public function validateAttribute($object, $attribute) { if (!ActiveWorkflowBehavior::isAttachedTo($object)) { throw new WorkflowException('Validation error : the model does not have the ActiveWorkflowBehavior'); } try { $scenarioList = $object->getScenarioSequence($object->{$attribute}); } catch (WorkflowException $e) { $object->addError($attribute, 'Workflow validation failed : ' . $e->getMessage()); $scenarioList = []; } if (count($scenarioList) != 0) { foreach ($object->getValidators() as $validator) { foreach ($scenarioList as $scenario) { if ($this->_isActiveValidator($validator, $scenario)) { $validator->validateAttributes($object); } } } } }
public function testGetNextStatusFails() { /** @var Item04|ActiveWorkflowBehavior $item */ $item = new Item04(); $item->detachBehavior('workflow'); $item->attachBehavior('workflowForTest', ['class' => ActiveWorkflowBehavior::className(), 'defaultWorkflowId' => 'INVALID_ID']); $this->specify('getNextStatus throws exception if default workflow Id is invalid', function () use($item) { $this->setExpectedException('fproject\\workflow\\core\\WorkflowException', "Invalid workflow Id : 'INVALID_ID'"); $item->getNextStatuses(); }); }
public function behaviors() { return ['workflow' => ['class' => ActiveWorkflowBehavior::className()]]; }
/** * Tests that a ActiveWorkflowBehavior behavior is attached to the object passed as argument. * * This method returns FALSE if $model is not an instance of BaseActiveRecord (has ActiveWorkflowBehavior can only be attached * to instances of this class) or if none of its attached behaviors is a or inherit from ActiveWorkflowBehavior. * * @param Model|ActiveWorkflowBehavior $model the model to test. * @return bool TRUE if at least one ActiveWorkflowBehavior behavior is attached to $model, FALSE otherwise * @throws WorkflowException */ public static function isAttachedTo($model) { if ($model instanceof Model) { foreach ($model->getBehaviors() as $behavior) { if ($behavior instanceof ActiveWorkflowBehavior) { return true; } } } else { throw new WorkflowException('Invalid argument type : $model must be an instance of yii\\base\\Model'); } return false; }
/** * @inheritdoc */ public function parseWorkflowStatus($val, $wfId, $model, &$wfDef = null) { if (empty($val) || !is_string($val)) { throw new WorkflowException('Not a valid status id : a non-empty string is expected - status = ' . VarDumper::dumpAsString($val)); } $tokens = array_map('trim', explode(self::SEPARATOR_STATUS_NAME, $val)); $tokenCount = count($tokens); if ($tokenCount == 1) { $tokens[1] = $tokens[0]; $tokens[0] = null; if (isset($wfId) && is_string($wfId)) { $tokens[0] = $wfId; } elseif (isset($model) && ($model instanceof ActiveWorkflowBehavior || ActiveWorkflowBehavior::isAttachedTo($model)) && $model->hasWorkflowStatus()) { $tokens[0] = $model->getWorkflowStatus()->getWorkflowId(); } if ($tokens[0] === null) { throw new WorkflowException('Not a valid status id format: failed to get workflow id / status = ' . VarDumper::dumpAsString($val)); } } elseif ($tokenCount != 2) { throw new WorkflowException('Not a valid status id format: ' . VarDumper::dumpAsString($val)); } elseif (!isset($wfDef) && isset($model) && ($model instanceof ActiveWorkflowBehavior || ActiveWorkflowBehavior::isAttachedTo($model)) && $model->hasWorkflowStatus()) { $wfDef = $this->getWorkflowDefinition($wfId, $model); } if (!$this->isValidWorkflowId($tokens[0])) { throw new WorkflowException('Not a valid status id : incorrect workflow id format in ' . VarDumper::dumpAsString($val)); } elseif (!$this->isValidStatusLocalId($tokens[1])) { throw new WorkflowException('Not a valid status id : incorrect status local id format in ' . VarDumper::dumpAsString($val)); } return $tokens; }
/** * @expectedException fproject\workflow\core\WorkflowException * @expectedExceptionMessage Failed to load workflow definition : Class tests\codeception\unit\models\NOTFOUNDSource does not exist */ public function testAutoInsertFails2() { $o = new Item00(); $o->attachBehavior('workflow', ['class' => ActiveWorkflowBehavior::className(), 'autoInsert' => 'NOTFOUND', 'defaultWorkflowId' => 'Item04Workflow']); }
/** * (non-PHPdoc) * @see \yii\base\Component::behaviors() */ public function behaviors() { return ['w1' => ['class' => ActiveWorkflowBehavior::className(), 'defaultWorkflowId' => 'Item08Workflow1'], 'w2' => ['class' => ActiveWorkflowBehavior::className(), 'statusAttribute' => 'status_ex', 'defaultWorkflowId' => 'Item08Workflow2']]; }
public function behaviors() { return ['workflow' => ['class' => ActiveWorkflowBehavior::className(), 'statusAttribute' => 'statusAlias', 'statusAccessor' => 'status_accessor']]; }
/** * (non-PHPdoc) * @see \yii\base\Component::behaviors() */ public function behaviors() { return ['wf' => ['class' => ActiveWorkflowBehavior::className(), 'idAccessor' => 'wfIdAccessor']]; }
/** * Displays the status for the model passed as argument. * * This method assumes that the status includes a metadata value called 'labelTemplate' that contains * the HTML template of the rendering status. In this template the string '{label}' will be replaced by the * status label. * * Example : * 'status' => [ * 'draft' => [ * 'label' => 'Draft', * 'transition' => ['ready' ], * 'metadata' => [ * 'labelTemplate' => '<span class="label label-default">{label}</span>' * ] * ], * * @param ActiveWorkflowBehavior $model * @return string|NULL the HTML rendered status or null if not labelTemplate is found */ public static function renderLabel($model) { if ($model->hasWorkflowStatus()) { $labelTemplate = $model->getWorkflowStatus()->getMetadata('labelTemplate'); if (!empty($labelTemplate)) { return strtr($labelTemplate, ['{label}' => $model->getWorkflowStatus()->getLabel()]); } } return null; }