web_tester.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,541 行 · 第 1/4 页

PHP
1,541
字号
     *    @return boolean            True if pass.
     *    @access public
     */
    function assertFieldById($id, $expected = true, $message = '%s') {
        $value = $this->_browser->getFieldById($id);
        return $this->_assertFieldValue($id, $value, $expected, $message);
    }
    
    /**
     *    Tests the field value against the expectation.
     *    @param string $identifier      Name, ID or label.
     *    @param mixed $value            Current field value.
     *    @param mixed $expected         Expected value to match.
     *    @param string $message         Failure message.
     *    @return boolean                True if pass
     *    @access protected
     */
    function _assertFieldValue($identifier, $value, $expected, $message) {
        if ($expected === true) {
            return $this->assertTrue(
                    isset($value),
                    sprintf($message, "Field [$identifier] should exist"));
        }
        if (! SimpleExpectation::isExpectation($expected)) {
            $identifier = str_replace('%', '%%', $identifier);
            $expected = new FieldExpectation(
                    $expected,
                    "Field [$identifier] should match with [%s]");
        }
        return $this->assert($expected, $value, $message);
    }
    
    /**
     *    Checks the response code against a list
     *    of possible values.
     *    @param array $responses    Possible responses for a pass.
     *    @param string $message     Message to display. Default
     *                               can be embedded with %s.
     *    @return boolean            True if pass.
     *    @access public
     */
    function assertResponse($responses, $message = '%s') {
        $responses = (is_array($responses) ? $responses : array($responses));
        $code = $this->_browser->getResponseCode();
        $message = sprintf($message, "Expecting response in [" .
                implode(", ", $responses) . "] got [$code]");
        return $this->assertTrue(in_array($code, $responses), $message);
    }
    
    /**
     *    Checks the mime type against a list
     *    of possible values.
     *    @param array $types      Possible mime types for a pass.
     *    @param string $message   Message to display.
     *    @return boolean          True if pass.
     *    @access public
     */
    function assertMime($types, $message = '%s') {
        $types = (is_array($types) ? $types : array($types));
        $type = $this->_browser->getMimeType();
        $message = sprintf($message, "Expecting mime type in [" .
                implode(", ", $types) . "] got [$type]");
        return $this->assertTrue(in_array($type, $types), $message);
    }
    
    /**
     *    Attempt to match the authentication type within
     *    the security realm we are currently matching.
     *    @param string $authentication   Usually basic.
     *    @param string $message          Message to display.
     *    @return boolean                 True if pass.
     *    @access public
     */
    function assertAuthentication($authentication = false, $message = '%s') {
        if (! $authentication) {
            $message = sprintf($message, "Expected any authentication type, got [" .
                    $this->_browser->getAuthentication() . "]");
            return $this->assertTrue(
                    $this->_browser->getAuthentication(),
                    $message);
        } else {
            $message = sprintf($message, "Expected authentication [$authentication] got [" .
                    $this->_browser->getAuthentication() . "]");
            return $this->assertTrue(
                    strtolower($this->_browser->getAuthentication()) == strtolower($authentication),
                    $message);
        }
    }
    
    /**
     *    Checks that no authentication is necessary to view
     *    the desired page.
     *    @param string $message     Message to display.
     *    @return boolean            True if pass.
     *    @access public
     */
    function assertNoAuthentication($message = '%s') {
        $message = sprintf($message, "Expected no authentication type, got [" .
                $this->_browser->getAuthentication() . "]");
        return $this->assertFalse($this->_browser->getAuthentication(), $message);
    }
    
    /**
     *    Attempts to match the current security realm.
     *    @param string $realm     Name of security realm.
     *    @param string $message   Message to display.
     *    @return boolean          True if pass.
     *    @access public
     */
    function assertRealm($realm, $message = '%s') {
        if (! SimpleExpectation::isExpectation($realm)) {
            $realm = new EqualExpectation($realm);
        }
        return $this->assert(
                $realm,
                $this->_browser->getRealm(),
                "Expected realm -> $message");
    }
    
    /**
     *    Checks each header line for the required value. If no
     *    value is given then only an existence check is made.
     *    @param string $header    Case insensitive header name.
     *    @param mixed $value      Case sensitive trimmed string to
     *                             match against. An expectation object
     *                             can be used for pattern matching.
     *    @return boolean          True if pass.
     *    @access public
     */
    function assertHeader($header, $value = false, $message = '%s') {
        return $this->assert(
                new HttpHeaderExpectation($header, $value),
                $this->_browser->getHeaders(),
                $message);
    }
        
    /**
     *    @deprecated
     */
    function assertHeaderPattern($header, $pattern, $message = '%s') {
        return $this->assert(
                new HttpHeaderExpectation($header, new PatternExpectation($pattern)),
                $this->_browser->getHeaders(),
                $message);
    }

    /**
     *    Confirms that the header type has not been received.
     *    Only the landing page is checked. If you want to check
     *    redirect pages, then you should limit redirects so
     *    as to capture the page you want.
     *    @param string $header    Case insensitive header name.
     *    @return boolean          True if pass.
     *    @access public
     */
    function assertNoHeader($header, $message = '%s') {
        return $this->assert(
                new NoHttpHeaderExpectation($header),
                $this->_browser->getHeaders(),
                $message);
    }
        
    /**
     *    @deprecated
     */
    function assertNoUnwantedHeader($header, $message = '%s') {
        return $this->assertNoHeader($header, $message);
    }
    
    /**
     *    Tests the text between the title tags.
     *    @param string/SimpleExpectation $title    Expected title.
     *    @param string $message                    Message to display.
     *    @return boolean                           True if pass.
     *    @access public
     */
    function assertTitle($title = false, $message = '%s') {
        if (! SimpleExpectation::isExpectation($title)) {
            $title = new EqualExpectation($title);
        }
        return $this->assert($title, $this->_browser->getTitle(), $message);
    }
    
    /**
     *    Will trigger a pass if the text is found in the plain
     *    text form of the page.
     *    @param string $text       Text to look for.
     *    @param string $message    Message to display.
     *    @return boolean           True if pass.
     *    @access public
     */
    function assertText($text, $message = '%s') {
        return $this->assert(
                new TextExpectation($text),
                $this->_browser->getContentAsText(),
                $message);
    }
    
    /**
     *    @deprecated
     */
    function assertWantedText($text, $message = '%s') {
        return $this->assertText($text, $message);
    }
    
    /**
     *    Will trigger a pass if the text is not found in the plain
     *    text form of the page.
     *    @param string $text       Text to look for.
     *    @param string $message    Message to display.
     *    @return boolean           True if pass.
     *    @access public
     */
    function assertNoText($text, $message = '%s') {
        return $this->assert(
                new NoTextExpectation($text),
                $this->_browser->getContentAsText(),
                $message);
    }
    
    /**
     *    @deprecated
     */
    function assertNoUnwantedText($text, $message = '%s') {
        return $this->assertNoText($text, $message);
    }
    
    /**
     *    Will trigger a pass if the Perl regex pattern
     *    is found in the raw content.
     *    @param string $pattern    Perl regex to look for including
     *                              the regex delimiters.
     *    @param string $message    Message to display.
     *    @return boolean           True if pass.
     *    @access public
     */
    function assertPattern($pattern, $message = '%s') {
        return $this->assert(
                new PatternExpectation($pattern),
                $this->_browser->getContent(),
                $message);
    }
    
    /**
     *    @deprecated
     */
    function assertWantedPattern($pattern, $message = '%s') {
        return $this->assertPattern($pattern, $message);
    }
    
    /**
     *    Will trigger a pass if the perl regex pattern
     *    is not present in raw content.
     *    @param string $pattern    Perl regex to look for including
     *                              the regex delimiters.
     *    @param string $message    Message to display.
     *    @return boolean           True if pass.
     *    @access public
     */
    function assertNoPattern($pattern, $message = '%s') {
        return $this->assert(
                new NoPatternExpectation($pattern),
                $this->_browser->getContent(),
                $message);
    }
    
    /**
     *    @deprecated
     */
    function assertNoUnwantedPattern($pattern, $message = '%s') {
        return $this->assertNoPattern($pattern, $message);
    }
    
    /**
     *    Checks that a cookie is set for the current page
     *    and optionally checks the value.
     *    @param string $name        Name of cookie to test.
     *    @param string $expected    Expected value as a string or
     *                               false if any value will do.
     *    @param string $message     Message to display.
     *    @return boolean            True if pass.
     *    @access public
     */
    function assertCookie($name, $expected = false, $message = '%s') {
        $value = $this->getCookie($name);
        if (! $expected) {
            return $this->assertTrue(
                    $value,
                    sprintf($message, "Expecting cookie [$name]"));
        }
        if (! SimpleExpectation::isExpectation($expected)) {
            $expected = new EqualExpectation($expected);
        }
        return $this->assert($expected, $value, "Expecting cookie [$name] -> $message");
    }
    
    /**
     *    Checks that no cookie is present or that it has
     *    been successfully cleared.
     *    @param string $name        Name of cookie to test.
     *    @param string $message     Message to display.
     *    @return boolean            True if pass.
     *    @access public
     */
    function assertNoCookie($name, $message = '%s') {
        return $this->assertTrue(
                $this->getCookie($name) === false,
                sprintf($message, "Not expecting cookie [$name]"));
    }

    /**
     *    Called from within the test methods to register
     *    passes and failures.
     *    @param boolean $result    Pass on true.
     *    @param string $message    Message to display describing
     *                              the test state.
     *    @return boolean           True on pass
     *    @access public
     */
    function assertTrue($result, $message = false) {
        return $this->assert(new TrueExpectation(), $result, $message);
    }

    /**
     *    Will be true on false and vice versa. False
     *    is the PHP definition of false, so that null,
     *    empty strings, zero and an empty array all count
     *    as false.
     *    @param boolean $result    Pass on false.
     *    @param string $message    Message to display.
     *    @return boolean           True on pass
     *    @access public
     */
    function assertFalse($result, $message = '%s') {
        return $this->assert(new FalseExpectation(), $result, $message);
    }
    
    /**
     *    Will trigger a pass if the two parameters have
     *    the same value only. Otherwise a fail. This
     *    is for testing hand extracted text, etc.
     *    @param mixed $first          Value to compare.
     *    @param mixed $second         Value to compare.
     *    @param string $message       Message to display.
     *    @return boolean              True on pass
     *    @access public
     */
    function assertEqual($first, $second, $message = '%s') {
        return $this->assert(
                new EqualExpectation($first),
                $second,
                $message);
    }
    
    /**
     *    Will trigger a pass if the two parameters have
     *    a different value. Otherwise a fail. This
     *    is for testing hand extracted text, etc.
     *    @param mixed $first           Value to compare.
     *    @param mixed $second          Value to compare.
     *    @param string $message        Message to display.
     *    @return boolean               True on pass
     *    @access public
     */
    function assertNotEqual($first, $second, $message = '%s') {
        return $this->assert(
                new NotEqualExpectation($first),
                $second,
                $message);
    }

    /**
     *    Uses a stack trace to find the line of an assertion.
     *    @return string           Line number of first assert*
     *                             method embedded in format string.
     *    @access public
     */
    function getAssertionLine() {
        $trace = new SimpleStackTrace(array('assert', 'click', 'pass', 'fail'));
        return $trace->traceMethod();
    }
}
?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?