Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 22... Línea 22...
22
 * @author     Tony Levi <tony.levi@blackboard.com>
22
 * @author     Tony Levi <tony.levi@blackboard.com>
23
 * @copyright  2015 Blackboard (http://www.blackboard.com)
23
 * @copyright  2015 Blackboard (http://www.blackboard.com)
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
25
 */
Línea -... Línea 26...
-
 
26
 
-
 
27
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
Línea 26... Línea 28...
26
 
28
use PHPUnit\Framework\TestCase;
27
 
29
 
28
/**
30
/**
29
 * Base class for PHPUnit test cases customised for Moodle
31
 * Base class for PHPUnit test cases customised for Moodle
Línea 51... Línea 53...
51
     * @param  boolean $ishtml
53
     * @param  boolean $ishtml
52
     *
54
     *
53
     * @deprecated 3.0
55
     * @deprecated 3.0
54
     */
56
     */
55
    public static function assertTag($matcher, $actual, $message = '', $ishtml = true) {
57
    public static function assertTag($matcher, $actual, $message = '', $ishtml = true) {
-
 
58
        if ($ishtml) {
-
 
59
            $dom = self::loadHTML($actual);
-
 
60
        } else {
56
        $dom = (new PHPUnit\Util\Xml\Loader)->load($actual, $ishtml);
61
            $dom = (new PHPUnit\Util\Xml\Loader)->load($actual);
-
 
62
        }
57
        $tags = self::findNodes($dom, $matcher, $ishtml);
63
        $tags = self::findNodes($dom, $matcher, $ishtml);
58
        $matched = (is_array($tags) && count($tags) > 0) && $tags[0] instanceof DOMNode;
64
        $matched = (is_array($tags) && count($tags) > 0) && $tags[0] instanceof DOMNode;
59
        self::assertTrue($matched, $message);
65
        self::assertTrue($matched, $message);
60
    }
66
    }
Línea 61... Línea 67...
61
 
67
 
-
 
68
    /**
-
 
69
     * Load HTML into a DomDocument.
-
 
70
     *
-
 
71
     * Note: THis is a replacement for functionality removed from PHPUnit 10.
-
 
72
     *
-
 
73
     * @param string $actual
-
 
74
     * @throws \PHPUnit\Util\Xml\XmlException
-
 
75
     * @return DOMDocument
-
 
76
     */
-
 
77
    public static function loadHTML(string $actual): DOMDocument {
-
 
78
        if ($actual === '') {
-
 
79
            throw new \PHPUnit\Util\Xml\XmlException('Could not load XML from empty string');
-
 
80
        }
-
 
81
 
-
 
82
        $document = new DOMDocument;
-
 
83
        $document->preserveWhiteSpace = false;
-
 
84
 
-
 
85
        $internal  = libxml_use_internal_errors(true);
-
 
86
        $message   = '';
-
 
87
        $reporting = error_reporting(0);
-
 
88
 
-
 
89
        $loaded = $document->loadHTML($actual);
-
 
90
 
-
 
91
        foreach (libxml_get_errors() as $error) {
-
 
92
            $message .= "\n" . $error->message;
-
 
93
        }
-
 
94
 
-
 
95
        libxml_use_internal_errors($internal);
-
 
96
        error_reporting($reporting);
-
 
97
 
-
 
98
        if ($loaded === false) {
-
 
99
            if ($message === '') {
-
 
100
                $message = 'Could not load XML for unknown reason';
-
 
101
            }
-
 
102
 
-
 
103
            throw new \PHPUnit\Util\Xml\XmlException($message);
-
 
104
        }
-
 
105
 
-
 
106
        return $document;
-
 
107
    }
-
 
108
 
62
    /**
109
    /**
63
     * Note: we are overriding this method to remove the deprecated error
110
     * Note: we are overriding this method to remove the deprecated error
64
     * @see https://tracker.moodle.org/browse/MDL-47129
111
     * @see https://tracker.moodle.org/browse/MDL-47129
65
     *
112
     *
66
     * @param  array   $matcher
113
     * @param  array   $matcher
Línea 69... Línea 116...
69
     * @param  boolean $ishtml
116
     * @param  boolean $ishtml
70
     *
117
     *
71
     * @deprecated 3.0
118
     * @deprecated 3.0
72
     */
119
     */
73
    public static function assertNotTag($matcher, $actual, $message = '', $ishtml = true) {
120
    public static function assertNotTag($matcher, $actual, $message = '', $ishtml = true) {
-
 
121
        if ($ishtml) {
-
 
122
            $dom = self::loadHTML($actual);
-
 
123
        } else {
74
        $dom = (new PHPUnit\Util\Xml\Loader)->load($actual, $ishtml);
124
            $dom = (new PHPUnit\Util\Xml\Loader)->load($actual);
-
 
125
        }
75
        $tags = self::findNodes($dom, $matcher, $ishtml);
126
        $tags = self::findNodes($dom, $matcher, $ishtml);
76
        $matched = (is_array($tags) && count($tags) > 0) && $tags[0] instanceof DOMNode;
127
        $matched = (is_array($tags) && count($tags) > 0) && $tags[0] instanceof DOMNode;
77
        self::assertFalse($matched, $message);
128
        self::assertFalse($matched, $message);
78
    }
129
    }
Línea 592... Línea 643...
592
            }
643
            }
593
        }
644
        }
Línea 594... Línea 645...
594
 
645
 
595
        return str_replace('  ', ' ', $result);
646
        return str_replace('  ', ' ', $result);
-
 
647
    }
-
 
648
 
-
 
649
    /**
-
 
650
     * Helper to get the count of invocation.
-
 
651
     *
-
 
652
     * This is required because the method to use changed names in PHPUnit 10.0 in a breaking change.
-
 
653
     *
-
 
654
     * @param \PHPUnit\Framework\MockObject\Rule\InvocationOrder $counter
-
 
655
     * @return int
-
 
656
     */
-
 
657
    protected static function getInvocationCount(InvocationOrder $counter): int {
-
 
658
        if (method_exists($counter, 'numberOfInvocations')) {
-
 
659
            return $counter->numberOfInvocations();
-
 
660
        }
-
 
661
 
-
 
662
        return $counter->getInvocationCount();
596
    }
663
    }
-
 
664
    // phpcs:enable
-
 
665
 
-
 
666
    /**
-
 
667
     * Get an invokable object for testing.
-
 
668
     *
-
 
669
     * This is a helper method to create an invokable object for testing which can be used to
-
 
670
     * track invocations, including arguments provided.
-
 
671
     *
-
 
672
     * This can be useful for modifications to the error handler.
-
 
673
     *
-
 
674
     * @return object
-
 
675
     */
-
 
676
    protected static function get_invokable() {
-
 
677
        return new class {
-
 
678
            private array $invocations = [];
-
 
679
            public function __invoke(...$args) {
-
 
680
                $this->invocations[] = $args;
-
 
681
            }
-
 
682
 
-
 
683
            public function get_invocations(): array {
-
 
684
                return $this->invocations;
-
 
685
            }
-
 
686
 
-
 
687
            public function get_invocation_count(): int {
-
 
688
                return count($this->invocations);
-
 
689
            }
-
 
690
 
-
 
691
            public function reset(): void {
-
 
692
                $this->invocations = [];
-
 
693
            }
-
 
694
        };
-
 
695
    }
-
 
696
 
-
 
697
    /**
-
 
698
     * Determine whether the test is running in isolation.
-
 
699
     *
-
 
700
     * Note: This was previously a public method of the TestCase, but as removed in PHPUnit 10.
-
 
701
     * There is no direct replacement, but we can use reflection to access the protected property.
-
 
702
     * @return bool
-
 
703
     */
-
 
704
    public function isInIsolation(): bool {
-
 
705
        $rc = new \ReflectionClass(TestCase::class);
-
 
706
        $rcp = $rc->getProperty('inIsolation');
-
 
707
 
-
 
708
        return $rcp->getValue($this);
597
    // phpcs:enable
709
    }