Completed
Push — try/use_composer_extra_replace... ( 8bec3d...e5998e )
by
unknown
06:48
created

Mock_Package::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
3
namespace Automattic\Jetpack\Config;
4
5
use Automattic\Jetpack\Config\Textdomain_Customizer;
6
use phpmock\Mock;
7
use phpmock\MockBuilder;
8
use phpmock\spy\Spy;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * Contains unit tests for the Textdomain_Customizer class.
13
 */
14
class Test_Textdomain_Customizer extends TestCase {
15
16
	/**
17
	 * Setup function.
18
	 */
19
	public function setUp() {
20
21
		$this->textdomain_customizer = $this->getMockBuilder( 'Automattic\Jetpack\Config\Textdomain_Customizer' )
22
			->disableOriginalConstructor()
23
			->setMethods( array( 'get_packages', 'set_vendor_dir', 'get_root_extra' ) )
24
			->getMock();
25
26
		$this->textdomain_customizer->expects( $this->once() )
27
			->method( 'set_vendor_dir' )
28
			->will( $this->returnValue( '' ) );
29
30
		$this->mock_function( 'file_exists', true );
31
		$this->mock_function( 'is_dir', false );
32
		$this->mock_function( 'is_file', true );
33
	}
34
35
	/**
36
	 * Teardown function.
37
	 */
38
	public function tearDown() {
39
		unset( $this->textdomain_customizer );
40
		Mock::disableAll();
41
	}
42
43
	/**
44
	 * Test customize_textdomain_in_packages.
45
	 */
46
	public function test_customizer_textdomain_in_packages() {
47
48
		$input  = "__( 'text to be translated', 'JETPACK_CUSTOMIZE_TEXTDOMAIN' )";
49
		$output = "__( 'text to be translated', 'test_textdomain' )";
50
51
		$this->mock_function( 'file_get_contents', $input );
52
53
		$file_put_contents_spy = new Spy(
54
			__NAMESPACE__,
55
			'file_put_contents',
56
			function() {
57
				return true;
58
			}
59
		);
60
		$file_put_contents_spy->enable();
61
62
		$test_package1 = new Mock_Package(
63
			'automattic/jetpack-test_package1',
64
			array( 'translatable' => 'test1' )
65
		);
66
67
		$packages = array( $test_package1 );
68
69
		$this->textdomain_customizer->expects( $this->once() )
70
			->method( 'get_packages' )
71
			->will( $this->returnValue( $packages ) );
72
73
		$this->textdomain_customizer->expects( $this->once() )
74
			->method( 'get_root_extra' )
75
			->will( $this->returnValue( array( 'textdomain' => 'test_textdomain' ) ) );
76
77
		$this->textdomain_customizer->customize_textdomain_in_packages();
78
79
		$file_put_contents_invocs = $file_put_contents_spy->getInvocations();
80
		$this->assertCount( 1, $file_put_contents_invocs );
81
82
		$file_put_contents_args = $file_put_contents_invocs[0]->getArguments();
83
		$this->assertEquals( $output, $file_put_contents_args[1] );
84
	}
85
86
	/**
87
	 * Mock a global function and make it return a certain value.
88
	 *
89
	 * @param string $function_name Name of the function.
90
	 * @param mixed  $return_value  Return value of the function.
91
	 * @return phpmock\Mock The mock object.
92
	 */
93 View Code Duplication
	protected function mock_function( $function_name, $return_value = null ) {
94
		$builder = new MockBuilder();
95
		$builder->setNamespace( __NAMESPACE__ )
96
			->setName( $function_name )
97
			->setFunction(
98
				function() use ( &$return_value ) {
99
					return $return_value;
100
				}
101
			);
102
		$mock_function = $builder->build();
103
		$mock_function->enable();
104
		return $mock_function;
105
	}
106
}
107
108
//phpcs:disable Generic.Files.OneObjectStructurePerFile
109
/**
110
 *
111
 * A class to create minimal mock Composers\Package objects for the unit tests.
112
 * The class provides implementations for the getName() and getExtra() methods.
113
 */
114
class Mock_Package {
115
	/**
116
	 * The constructor.
117
	 *
118
	 * @param string $name The package name.
119
	 * @param mixed  $extra The package extra.
120
	 */
121
	public function __construct( $name, $extra ) {
122
		$this->name  = $name;
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
123
		$this->extra = $extra;
0 ignored issues
show
Bug introduced by
The property extra does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
124
	}
125
126
	//phpcs:disable WordPress.NamingConventions
127
	/**
128
	 * Returns the mock package's name.
129
	 */
130
	public function getName() {
131
		return $this->name;
132
	}
133
134
	/**
135
	 * Returns the mock package's extra value.
136
	 */
137
	public function getExtra() {
138
		return $this->extra;
139
	}
140
141
}
142