elliotchance / concise

✅ Concise is test framework for using plain English and minimal code, built on PHPUnit.
MIT License
45 stars 3 forks source link

SetUp() and tearDown() once fixtures #305

Open elliotchance opened 8 years ago

elliotchance commented 8 years ago

Theres lots of ways this can be done, but in its most basic form you want to to run methods once, like:

    public function testGetAllAttendance()
    {
        $this->setUpOnce([$this, 'data']);
        ...
    }

    public function setUpOnce(callable $function)
    {
        static $setUp = [];
        $key = get_class($function[0]) . '::' . $function[1];
        if (!array_key_exists($key, $setUp)) {
            $function();
            $setUp[$key] = get_object_vars($this);
        } else {
            foreach ($setUp[$key] as $k => $v) {
                $this->$k = $v;
            }
        }
    }

    public function data()
    {
        $this->company = $this->createCompany();
        ...
    }