Completed
Push — add/changelog-tooling ( c37726...963953 )
by
unknown
371:25 queued 360:46
created

ParserTest::testFormatToFile()   A

Complexity

Conditions 1
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 3
nop 0
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Tests for the changelog Parser base class.
4
 *
5
 * @package automattic/jetpack-changelogger
6
 */
7
8
// phpcs:disable WordPress.WP.AlternativeFunctions
9
10
namespace Automattic\Jetpack\Changelog\Tests;
11
12
use Automattic\Jetpack\Changelog\Changelog;
13
use Automattic\Jetpack\Changelog\Parser;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * Tests for the changelog Parser base class.
18
 *
19
 * @covers \Automattic\Jetpack\Changelog\Parser
20
 */
21
class ParserTest extends TestCase {
22
23
	/**
24
	 * Test parseFromFile.
25
	 */
26
	public function testParseFromFile() {
27
		$mock = $this->getMockBuilder( Parser::class )->getMockForAbstractClass();
28
		$mock->method( 'parse' )->will( $this->returnArgument( 0 ) );
29
30
		$temp = tempnam( sys_get_temp_dir(), 'phpunit-testParseFromFile-' );
31
		try {
32
			file_put_contents( $temp, 'Foo bar?' );
33
			$this->assertSame( 'Foo bar?', $mock->parseFromFile( $temp ) );
34
		} finally {
35
			unlink( $temp );
36
		}
37
38
		$fp = fopen( 'php://memory', 'w+' );
39
		fputs( $fp, 'Foo baz?' );
40
		rewind( $fp );
41
		$this->assertSame( 'Foo baz?', $mock->parseFromFile( $fp ) );
42
	}
43
44
	/**
45
	 * Test formatToFile.
46
	 */
47
	public function testFormatToFile() {
48
		$mock      = $this->getMockBuilder( Parser::class )->getMockForAbstractClass();
49
		$changelog = new Changelog();
50
		$mock->method( 'format' )->with( $this->identicalTo( $changelog ) )->willReturn( 'Formatted?' );
51
52
		$temp = tempnam( sys_get_temp_dir(), 'phpunit-testFormatToFile-' );
53
		try {
54
			file_put_contents( $temp, 'Foo bar?' );
55
			$this->assertTrue( $mock->formatToFile( $temp, $changelog ) );
56
			$this->assertSame( 'Formatted?', file_get_contents( $temp ) );
57
		} finally {
58
			unlink( $temp );
59
		}
60
61
		$fp = fopen( 'php://memory', 'w+' );
62
		fputs( $fp, 'Foo baz?' );
63
		$this->assertTrue( $mock->formatToFile( $fp, $changelog ) );
64
		fputs( $fp, '!' );
65
		rewind( $fp );
66
		$this->assertSame( 'Foo baz?Formatted?!', stream_get_contents( $fp ) );
67
68
		$fp = fopen( 'php://memory', 'r' );
69
		$this->assertFalse( $mock->formatToFile( $fp, $changelog ) );
70
	}
71
72
}
73