should_shallow_create_hierarchical_tax_terms()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
1
<?php
2
3
namespace lloc\Msls\ContentImport\Importers\Terms;
4
5
6
use lloc\Msls\ContentImport\ImportCoordinates;
7
use lloc\Msls\ContentImport\TestCase;
8
use lloc\Msls\MslsOptionsTax;
9
use PHPUnit\Framework\Assert;
10
use Prophecy\Argument;
11
12
class ShallowDuplicatingTest extends TestCase {
13
14
	/**
15
	 * It should assign non-hierarchical tax terms to destination post
16
	 *
17
	 * @test
18
	 */
19
	public function should_assign_non_hierarchical_tax_terms_to_destination_post() {
20
		/** @var ImportCoordinates $import_coordinates */
21
		list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest();
22
23
		switch_to_blog( $import_coordinates->source_blog_id );
24
		$source_term_ids = $this->factory()->category->create_many( 3 );
25
		wp_set_object_terms( $import_coordinates->source_post_id, $source_term_ids, 'category' );
26
		foreach ( $source_term_ids as $source_term_id ) {
27
			$relations->should_create( Argument::type( MslsOptionsTax::class ), $import_coordinates->dest_lang, Argument::type( 'int' ) )->will( function ( array $args ) use ( &$source_term_ids ) {
28
				/** @var MslsOptionsTax $option */
29
				list( $option, $lang, $id ) = $args;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $id is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
30
				$this_source_term_id = $option->get_arg( 0, 23 );
31
				Assert::assertTrue( in_array( $this_source_term_id, $source_term_ids ) );
32
				unset( $source_term_ids[ array_search( $this_source_term_id, $source_term_ids, true ) ] );
33
34
				return true;
35
			} );
36
		}
37
38
		restore_current_blog();
39
40
		$obj = new ShallowDuplicating( $import_coordinates, $logger->reveal(), $relations->reveal() );
41
42
		$mutated = $obj->import( $dest_post_data );
43
44
		$this->assertEquals( $dest_post_data, $mutated );
45
46
		switch_to_blog($import_coordinates->dest_blog_id);
47
48
		$dest_terms = wp_get_object_terms( $import_coordinates->dest_post_id, 'category' );
49
		$this->assertCount( 3, $dest_terms );
50
		$this->assertEmpty($source_term_ids);
51
	}
52
53
	/**
54
	 * It should assign hierarchical tax terms to destination post
55
	 *
56
	 * @test
57
	 */
58
	public function should_assign_hierarchical_tax_terms_to_destination_post() {
59
		/** @var ImportCoordinates $import_coordinates */
60
		list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest();
61
62
		switch_to_blog( $import_coordinates->source_blog_id );
63
		$source_term_ids = $this->factory()->tag->create_many( 3 );
64
		wp_set_object_terms( $import_coordinates->source_post_id, [], 'category' );
65
		wp_set_object_terms( $import_coordinates->source_post_id, $source_term_ids, 'post_tag' );
66
		foreach ( $source_term_ids as $source_term_id ) {
67
			$relations->should_create( Argument::type( MslsOptionsTax::class ), $import_coordinates->dest_lang, Argument::type( 'int' ) )->will( function ( array $args ) use ( &$source_term_ids ) {
68
				/** @var MslsOptionsTax $option */
69
				list( $option, $lang, $id ) = $args;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $id is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
70
				$this_source_term_id = $option->get_arg( 0, 23 );
71
				Assert::assertTrue( in_array( $this_source_term_id, $source_term_ids ) );
72
				unset( $source_term_ids[ array_search( $this_source_term_id, $source_term_ids, true ) ] );
73
74
				return true;
75
			} );
76
		}
77
78
		restore_current_blog();
79
80
		$obj = new ShallowDuplicating( $import_coordinates, $logger->reveal(), $relations->reveal() );
81
82
		$mutated = $obj->import( $dest_post_data );
83
84
		$this->assertEquals( $dest_post_data, $mutated );
85
86
		switch_to_blog($import_coordinates->dest_blog_id);
87
88
		$dest_terms = wp_get_object_terms( $import_coordinates->dest_post_id, 'post_tag' );
89
		$this->assertCount( 3, $dest_terms );
90
		$this->assertEmpty($source_term_ids);
91
	}
92
93
	/**
94
	 * It should shallow create hierarchical tax terms
95
	 *
96
	 * @test
97
	 */
98
	public function should_shallow_create_hierarchical_tax_terms() {
99
		/** @var ImportCoordinates $import_coordinates */
100
		list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest();
101
102
		switch_to_blog( $import_coordinates->source_blog_id );
103
		$parent_source_term_id    = $this->factory()->tag->create();
104
		$children_source_term_ids = $this->factory()->tag->create_many( 3, [ 'parent' => $parent_source_term_id ] );
105
		wp_set_object_terms( $import_coordinates->source_post_id, [], 'category' );
106
		wp_set_object_terms( $import_coordinates->source_post_id, $children_source_term_ids, 'post_tag' );
107
		foreach ( $children_source_term_ids as $source_term_id ) {
108
			$relations->should_create( Argument::type( MslsOptionsTax::class ), $import_coordinates->dest_lang, Argument::type( 'int' ) )->will( function ( array $args ) use ( &$children_source_term_ids ) {
109
				/** @var MslsOptionsTax $option */
110
				list( $option, $lang, $id ) = $args;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $id is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
111
				$this_source_term_id = $option->get_arg( 0, 23 );
112
				Assert::assertTrue( in_array( $this_source_term_id, $children_source_term_ids ) );
113
				unset( $children_source_term_ids[ array_search( $this_source_term_id, $children_source_term_ids, true ) ] );
114
115
				return true;
116
			} );
117
		}
118
119
		restore_current_blog();
120
121
		$obj = new ShallowDuplicating( $import_coordinates, $logger->reveal(), $relations->reveal() );
122
123
		$mutated = $obj->import( $dest_post_data );
124
125
		$this->assertEquals( $dest_post_data, $mutated );
126
127
		switch_to_blog($import_coordinates->dest_blog_id);
128
		$dest_terms = wp_get_object_terms( $import_coordinates->dest_post_id, 'post_tag' );
129
		$this->assertCount( 3, $dest_terms );
130
	}
131
132
	/**
133
	 * It should create a new term in the destination blog if source term is linked to non-existent
134
	 *
135
	 * @test
136
	 */
137
	public function should_create_a_new_term_in_the_destination_blog_if_source_term_is_linked_to_non_existent() {
138
		/** @var ImportCoordinates $import_coordinates */
139
		list( $import_coordinates, $logger, $relations, $dest_post_data ) = $this->setup_source_and_dest();
140
141
		switch_to_blog( $import_coordinates->source_blog_id );
142
		$source_term_id = $this->factory()->category->create();
143
		wp_set_object_terms( $import_coordinates->source_post_id, $source_term_id, 'category' );
144
		$option = MslsOptionsTax::create( $source_term_id );
145
		$option->set( $import_coordinates->dest_lang, 23 );
0 ignored issues
show
Unused Code introduced by
The call to MslsOptionsTax::set() has too many arguments starting with 23.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
147
		$relations->should_create( Argument::type( MslsOptionsTax::class ), $import_coordinates->dest_lang, Argument::type( 'int' ) )->will( function ( array $args ) use ( $source_term_id ) {
148
			/** @var MslsOptionsTax $option */
149
			list( $option, $lang, $id ) = $args;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $id is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
150
			$this_source_term_id = $option->get_arg( 0, 23 );
151
			Assert::assertEquals( $source_term_id, $this_source_term_id );
152
153
			return true;
154
		} );
155
156
		restore_current_blog();
157
158
		$obj = new ShallowDuplicating( $import_coordinates, $logger->reveal(), $relations->reveal() );
159
160
		$mutated = $obj->import( $dest_post_data );
161
162
		$this->assertEquals( $dest_post_data, $mutated );
163
164
		switch_to_blog($import_coordinates->dest_blog_id);
165
166
		$dest_terms = wp_get_object_terms( $import_coordinates->dest_post_id, 'category' );
167
		$this->assertCount( 1, $dest_terms );
168
		MslsOptionsTax::create($dest_terms[0]->term_id);
169
	}
170
}
171