aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/mikespub/php-epub-meta/test/monocleTest.php
blob: 20cc5a6c58acbf625e1cb7b9379aac23008c9512 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php

use PHPUnit\Framework\TestCase;
use SebLucas\EPubMeta\EPub;

/**
 * Test for EPUB methods used by Monocle in COPS
 *
 * Source: https://github.com/mikespub-org/seblucas
 */
class MonocleTest extends TestCase
{
    public const TEST_EPUB = __DIR__ . '/data/eng.epub';
    public const TEST_EPUB_COPY = __DIR__ . '/data/eng.copy.epub';
    public const TEST_CONTENTS = __DIR__ . '/data/eng.contents.json';
    public const TEST_COMPONENTS = __DIR__ . '/data/eng.components.json';
    public const TEST_EPUB3 = __DIR__ . '/data/eng3.epub';

    private static EPub $book;

    public static function setUpBeforeClass(): void
    {
        // sometime I might have accidentally broken the test file
        if (filesize(static::TEST_EPUB) != 22664) {
            die('test.epub has wrong size, make sure it\'s unmodified');
        }

        // we work on a copy to test saving
        if (!copy(static::TEST_EPUB, static::TEST_EPUB_COPY)) {
            die('failed to create copy of the test book');
        }

        // @see https://github.com/sebastianbergmann/phpunit/issues/5062#issuecomment-1416362657
        set_error_handler(
            static function (int $errno, string $errstr) {
                throw new \Exception($errstr, $errno);
            },
            E_ALL
        );

        self::$book = new Epub(static::TEST_EPUB_COPY);
        self::$book->initSpineComponent();
    }

    public static function tearDownAfterClass(): void
    {
        restore_error_handler();

        unlink(static::TEST_EPUB_COPY);
    }

    public function testComponents(): void
    {
        $data = self::$book->components();
        $contents = file_get_contents(static::TEST_COMPONENTS);
        $check = json_decode($contents, true);
        $encoder = $this->provideEncodeReplace();
        $check = str_replace($encoder[0], $encoder[1], $check);
        $this->assertEquals($check, $data);
    }

    public function testContents(): void
    {
        $data = self::$book->contents();
        $contents = file_get_contents(static::TEST_CONTENTS);
        $check = json_decode($contents, true);
        $encoder = $this->provideEncodeReplace();
        foreach (array_keys($check) as $idx) {
            $check[$idx] = $this->encodeItem($check[$idx], $encoder);
        }
        $this->assertEquals($check, $data);
    }

    /**
     * Summary of testComponent
     * @param string $component
     * @return void
     */
    public function testComponent($component = 'text/titlepage.xhtml')
    {
        $data = self::$book->component($component);
        $check = 641;
        $this->assertEquals($check, strlen($data));
    }

    /**
     * Summary of testGetComponentName
     * @param string $component
     * @param string $element
     * @return void
     */
    public function testGetComponentName($component = 'text/titlepage.xhtml', $element = '../images/cover.jpg')
    {
        $data = self::$book->getComponentName($component, $element);
        $check = 'images~SLASH~cover.jpg';
        $this->assertEquals($check, $data);
    }

    /**
     * Summary of testComponentContentType
     * @param string $component
     * @return void
     */
    public function testComponentContentType($component = 'text/titlepage.xhtml')
    {
        $data = self::$book->componentContentType($component);
        $check = 'application/xhtml+xml';
        $this->assertEquals($check, $data);
    }

    public function testContentsEpub3(): void
    {
        $epub = new EPub(__DIR__ . '/data/eng3.epub');
        $epub->initSpineComponent();
        $data = $epub->contents();
        $contents = file_get_contents(static::TEST_CONTENTS);
        $check = json_decode($contents, true);
        $encoder = $this->provideEncodeReplace();
        foreach (array_keys($check) as $idx) {
            $check[$idx] = $this->encodeItem($check[$idx], $encoder);
        }
        $this->assertEquals($check, $data);
    }

    /**
     * Summary of encodeItem
     * @param array<mixed> $item
     * @param array<mixed> $encoder
     * @return array<mixed>
     */
    protected function encodeItem($item, $encoder)
    {
        $item['src'] = str_replace($encoder[0], $encoder[1], $item['src']);
        if (!empty($item['children'])) {
            foreach (array_keys($item['children']) as $idx) {
                $item['children'][$idx] = $this->encodeItem($item['children'][$idx], $encoder);
            }
        }
        return $item;
    }

    /**
     * Summary of provideEncodeReplace
     * @return array<mixed>
     */
    public function provideEncodeReplace()
    {
        return EPub::$encodeNameReplace;
    }

    /**
     * Summary of provideDecodeReplace
     * @return array<mixed>
     */
    public function provideDecodeReplace()
    {
        return EPub::$decodeNameReplace;
    }
}