Question

I'm new to testing and I'm using phpspec to test a service class which validates input. I'm not sure how to test the class because it uses static::$base_rules and static::$create_rules. Those values are determined by the child class extending Acme\Services\Validation\Validator.

I realize I'm doing things a little backwards with phpspec, but I wanted to practice with this established class and I'm not sure if it's possible or if my Acme\Services\Validation\Validator class is poorly coded.

Class I want to test with phpspec

<?php namespace Acme\Services\Validation;

use Illuminate\Validation\Factory as Validation;

abstract class Validator
{

    /**
     * @var array
     */
    protected $rules;

    /**
     * @var object
     */
    protected $validation;

    /**
     * @var Validator
     */
    private $validator;

    /**
     * @param Validation $validator
     */
    public function __construct(Validation $validator)
    {
        $this->rules = isset(static::$base_rules) ? static::$base_rules : [];
        $this->validator = $validator;
    }

    /**
     * @param $input
     * @return \Illuminate\Validation\Validator
     */
    public function validateCreateRules($input)
    {
        $rules = array_merge(static::$create_rules, $this->rules);

        return $this->validate($input, $rules);
    }

    /**
     * @param $input
     * @param $rules
     * @return \Illuminate\Validation\Validator
     * @throws FormValidationException
     */
    public function validate($input, $rules)
    {
        $this->validation = $this->validator->make($input, $rules);

        if ($this->validation->fails())
        {
            throw new FormValidationException('Validation failed', $this->getValidationErrors());
        }
        return $this->validation;
    }

    /**
     * @return mixed
     */
    public function getValidationErrors()
    {
        return $this->validation->errors();
    }
}

Here's an example class that extends Validator

<?php namespace Acme\Services\Validation;

class MessageValidator extends Validator {

    static $base_rules = array(
        'type'              => 'required',
        'title'             => 'required',
        'message'           => 'required'
    );

    static $create_rules = array(
        'user'              => 'required'
    );

}

How could Acme\Services\Validation\Validator be tested using phpspec?

Was it helpful?

Solution

I'll ignore the use of statics...

As others already suggested you should spec concrete implementations. You could provide one just for the purpose of speccing (for example if you only provide the abstract class for others to use).

You'll have to tell phpspec to use the concrete implementation instead of the abstract class:

$this->beAnInstanceOf('My\Not\Abstract\Implementation');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top