Ticket #92 (new enhancement)

Opened 1 year ago

Solar_Test::assertType()

Reported by: rodrigo moraes <rodrigo.moraes@gmail.com> Assigned to: pmjones
Priority: trivial Component: code
Keywords: Solar_Test Cc:

Description

Hi,

Solar_Test misses a method assertType() to test PHP types. I would help moving tests from PhpUnit and also would be a shortcut to assertSame($var, is_array($var)) etc. Here's a suggested implementation:

<?php
    /**
     *
     * Asserts that a variable is of a certain PHP type.
     *
     * @param mixed $actual The variable to test.
     *
     * @param string $type The type to expect.
     *
     * @return bool The assertion result.
     *
     */
    public function assertType($actual, $expect)
    {
        $this->_assert_count ++;

        $allowed = array('array', 'float', 'int', 'object', 'string');

        if (! in_array($expect, $allowed)) {
            $this->fail(
                'Non-supported type.',
                array(
                    'actual' => $this->_export($actual),
                    'type'   => $expect,
                )
            );
        }

        $function = 'is_' . $expect;

        if(! $function($actual)) {
            $this->fail(
                'Expected ' . $expect . ', actually not . ' $expect,
                array(
                    'actual' => $this->_export($actual),
                )
            );
        } else {
            return true;
        }
    }

Attachments


Add/Change #92 (Solar_Test::assertType())