Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
15 / 18
66.67% covered (warning)
66.67%
6 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
MockPageContent
83.33% covered (warning)
83.33%
15 / 18
66.67% covered (warning)
66.67%
6 / 9
13.78
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getLinkTarget
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRevisionId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRoles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasRole
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkRole
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 getModel
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFormat
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getContent
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Mocks;
5
6use Wikimedia\Parsoid\Config\PageContent;
7use Wikimedia\Parsoid\Core\LinkTarget;
8use Wikimedia\Parsoid\Utils\TitleValue;
9
10class MockPageContent extends PageContent {
11
12    private LinkTarget $title;
13    private ?int $revid;
14
15    /**
16     * Alas, this is public because parserTests is reaching in and altering
17     * the main content when various modes are run.
18     *
19     * @var array
20     */
21    public $data = [];
22
23    /**
24     * @param array $data Page content data. Keys are roles, values are arrays or strings.
25     *  A string value is considered as an array [ 'content' => $value ]. Array keys are:
26     *   - content: (string) The slot's content.
27     *   - contentmodel: (string, default 'wikitext') The slot's content model.
28     *   - contentformat: (string, default 'text/x-wiki') The slot's content format.
29     *   - redirect: (string, optional) The redirect target (same format as PageConfig::getTitle),
30     *     if this content is a redirect.
31     */
32    public function __construct( array $data, ?LinkTarget $title = null, ?int $revid = null ) {
33        $this->title = $title ?? TitleValue::tryNew( 0, 'TestPage' );
34        foreach ( $data as $role => $v ) {
35            $this->data[$role] = is_string( $v ) ? [ 'content' => $v ] : $v;
36        }
37        $this->revid = $revid;
38    }
39
40    /** @inheritDoc */
41    public function getLinkTarget(): LinkTarget {
42        return $this->title;
43    }
44
45    /** @inheritDoc */
46    public function getRevisionId(): ?int {
47        return $this->revid;
48    }
49
50    /** @inheritDoc */
51    public function getRoles(): array {
52        return array_keys( $this->data );
53    }
54
55    /** @inheritDoc */
56    public function hasRole( string $role ): bool {
57        return isset( $this->data[$role] );
58    }
59
60    private function checkRole( string $role ): void {
61        if ( !isset( $this->data[$role] ) ) {
62            throw new \InvalidArgumentException( "Unknown role \"$role\"" );
63        }
64    }
65
66    /** @inheritDoc */
67    public function getModel( string $role ): string {
68        $this->checkRole( $role );
69        return $this->data[$role]['contentmodel'] ?? 'wikitext';
70    }
71
72    /** @inheritDoc */
73    public function getFormat( string $role ): string {
74        $this->checkRole( $role );
75        return $this->data[$role]['contentformat'] ?? 'text/x-wiki';
76    }
77
78    /** @inheritDoc */
79    public function getContent( string $role ): string {
80        $this->checkRole( $role );
81        if ( !isset( $this->data[$role]['content'] ) ) {
82            throw new \InvalidArgumentException( 'Unknown role or missing content failure' );
83        }
84        return $this->data[$role]['content'];
85    }
86
87}