xref: /dokuwiki/_test/tests/inc/io_replaceinfile.test.php (revision 6c0002048504e43b399abece0668afa2b5c87a07)
1<?php
2
3class io_replaceinfile_test extends DokuWikiTest {
4
5    /*
6     * dependency for tests needing zlib extension to pass
7     */
8    public function test_ext_zlib() {
9        if (!extension_loaded('zlib')) {
10            $this->markTestSkipped('skipping all zlib tests.  Need zlib extension');
11        }
12    }
13
14    /*
15     * dependency for tests needing zlib extension to pass
16     */
17    public function test_ext_bz2() {
18        if (!extension_loaded('bz2')) {
19            $this->markTestSkipped('skipping all bzip2 tests.  Need bz2 extension');
20        }
21    }
22
23    function _write($file){
24        $contents = "The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012";
25        io_saveFile($file, $contents);
26        // Replace one, no regex
27        $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete00\012", false, 1));
28        $this->assertEquals("The\012Delete00\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file));
29        // Replace all, no regex
30        $this->assertTrue(io_replaceInFile($file, "Delete\012", "DeleteX\012", false, -1));
31        $this->assertEquals("The\012Delete00\012DeleteX\012Delete01\012Delete02\012DeleteX\012DeleteX\012Test\012", io_readFile($file));
32        // Replace two, regex and backreference
33        $this->assertTrue(io_replaceInFile($file, "#Delete(\\d+)\012#", "\\1\012", true, 2));
34        $this->assertEquals("The\01200\012DeleteX\01201\012Delete02\012DeleteX\012DeleteX\012Test\012", io_readFile($file));
35        // Delete and insert, no regex
36        $this->assertTrue(io_replaceInFile($file, "DeleteX\012", "Replace\012", false, 0));
37        $this->assertEquals("The\01200\01201\012Delete02\012Test\012Replace\012", io_readFile($file));
38    }
39
40    function test_replace(){
41        $this->_write(TMP_DIR.'/test.txt');
42    }
43
44    /**
45     * @depends test_ext_zlib
46     */
47    function test_gzwrite(){
48        $this->_write(TMP_DIR.'/test.txt.gz');
49    }
50
51    /**
52     * @depends test_ext_bz2
53     */
54    function test_bzwrite(){
55        $this->_write(TMP_DIR.'/test.txt.bz2');
56    }
57
58}
59