I
I
Ilya Loopashko2020-12-21 17:50:37
PHP
Ilya Loopashko, 2020-12-21 17:50:37

How to check a class using assert?

I have a Task.php class and I need to check it using assert. I don't have any validation, how do I set up the validation correctly?

php.ini settings

assert.active               = on
assert.bail                 = on
assert.callback             = ""
assert.exception            = off
assert.quiet_eval           = off
assert.warning              = on
zend.assertions              = 1


task.php
<?php

class Task
{

    const STATUS_NEW = 'new';
    const STATUS_IN_WORK = 'in_work';
    const STATUS_DONE = 'done';
    const STATUS_FAILED = 'failed';
    const STATUS_CANCEL = 'cancel';

    const ACTION_CANCEL = 'cancel_task';
    const ACTION_ANSWER = 'answer';
    const ACTION_FINISHED = 'finished';
    const ACTION_DECLINE = 'decline';
    const ACTION_ACCEPT = 'accept';

    const ROLE_IMPLEMENT = 'implementer';
    const ROLE_CUSTOMER = 'customer';

    const STAUS_NAME  = [
        self::STATUS_NEW => 'Новое',
        self::STATUS_IN_WORK => 'В работе',
        self::STATUS_DONE => 'Выполнено',
        self::STATUS_FAILED => 'Провалено',
        self::STATUS_CANCEL => 'Отменено'
    ];

    const ACTION_NAME = [
        self::ACTION_CANCEL => 'Отменить',
        self::ACTION_ANSWER => 'Откликнуться',
        self::ACTION_FINISHED => 'Выполнено',
        self::ACTION_DECLINE => 'Отказаться',
        self::ACTION_ACCEPT => 'Принять'
    ];

    protected $nextActionAndNextStatus = [
        self::ACTION_CANCEL => self::STATUS_CANCEL,
        self::ACTION_ANSWER => null,
        self::ACTION_FINISHED => self::STATUS_DONE,
        self::ACTION_DECLINE => self::STATUS_FAILED,
        self::ACTION_ACCEPT => self::STATUS_IN_WORK,
        self::STATUS_NEW => [
            self::ROLE_IMPLEMENT => self::ACTION_ANSWER,
            self::ROLE_CUSTOMER => self::ACTION_CANCEL
        ],
        self::STATUS_IN_WORK => [
            self::ROLE_IMPLEMENT => self::ACTION_DECLINE,
            self::ROLE_CUSTOMER => self::ACTION_FINISHED
        ],
        self::STATUS_DONE => null,
        self::STATUS_FAILED => null,
        self::STATUS_CANCEL => null,
    ];
    public $user = '';

    protected $idTask = null;
    protected $idStatus = null;


    public function __construct(int $idTask, int $idStatus)
    {
        $this->idTask = $idTask;
        $this->idStatus = $idStatus;
    }

    public function getNextStatus(string $action)
    {
        if (!$action) {
            return null;
        }
        return $this->nextActionAndNextStatus[$action];
    }

    public function getNextAction(string $status)
    {
        if (!$status) {
            return null;
        }
        return $this->nextActionAndNextStatus[$status][$this->user];
    }
}


index.php
<?php
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);

require 'src\Task.php';

$task = new Task(1, 2);

  echo '<pre>';

  print_r($task::STAUS_NAME);

  print_r($task::ACTION_NAME);

    print_r($task->getNextStatus("new"));


assert($task->getNextStatus("done") == Task::STATUS_DONE, 'Ожидайте действие: "in work"');
assert($task->getNextStatus("cancel") == Task::STATUS_CANCEL, 'Ожидайте действие: "cancel"');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
index0h, 2020-12-21
@index0h

Don't use public properties, check all arguments of all methods.
And do not crutch with assert

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question