Completed
Push — master ( 6c0735...dd8543 )
by Dennis
06:47
created

RelationsTest::test_create()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
1
<?php
2
3
namespace lloc\Msls\ContentImport;
4
5
6
use lloc\Msls\MslsOptions;
7
use Prophecy\Argument;
8
use Prophecy\Prophecy\ObjectProphecy;
9
10
class RelationsTest extends \Msls_UnitTestCase {
11
12
	/**
13
	 * @var \lloc\Msls\ContentImport\ImportCoordinates
14
	 */
15
	public $import_coordinates;
16
17
	/**
18
	 * @test
19
	 * it should be instantiatable
20
	 */
21
	public function it_should_be_instantiatable() {
22
		$sut = $this->make_instance();
23
24
		$this->assertInstanceOf( Relations::class, $sut );
25
	}
26
27
	/**
28
	 * @return Relations
29
	 */
30
	private function make_instance() {
31
		return new Relations( $this->import_coordinates->reveal() );
0 ignored issues
show
Bug introduced by
The method reveal() does not seem to exist on object<lloc\Msls\ContentImport\ImportCoordinates>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
	}
33
34
	public function test_merge() {
35
		$creator   = $this->prophesize( MslsOptions::class );
36
		$dest_lang = 'de_DE';
37
		$obj_1     = $this->make_instance();
38
		$obj_2     = $this->make_instance();
39
40
		$obj_1->should_create( $creator->reveal(), $dest_lang, $this->factory->post->create() );
41
		$obj_1->should_create( $creator->reveal(), $dest_lang, $this->factory->post->create() );
42
		$obj_1->should_create( $creator->reveal(), $dest_lang, $this->factory->post->create() );
43
44
		$this->assertCount( 3, $obj_1->get_data() );
45
46
		$obj_2->should_create( $creator->reveal(), $dest_lang, $this->factory->post->create() );
47
		$obj_2->should_create( $creator->reveal(), $dest_lang, $this->factory->post->create() );
48
		$obj_2->should_create( $creator->reveal(), $dest_lang, $this->factory->post->create() );
49
50
		$this->assertCount( 3, $obj_2->get_data() );
51
52
		$obj_1->merge( $obj_2 );
53
54
		$this->assertCount( 6, $obj_1->get_data() );
55
	}
56
57
	public function test_create() {
58
		$source_lang = 'it_IT';
59
		$dest_lang   = 'de_DE';
60
		$post_ids    = $this->factory->post->create_many( 3 );
61
		list( $creator_1, $creator_2, $creator_3 ) = array_map( function ( ObjectProphecy $c ) use ( $source_lang, $dest_lang, $post_ids ) {
62
			static $i;
63
			$i = $i === null ? 0 : ++$i;
64
65
			$c->save( [ $dest_lang => $post_ids[ $i ] ] )->shouldBeCalled();
66
			$c->get_arg( Argument::type( 'int' ), $post_ids[ $i ] )->willReturn( $post_ids[ $i ] );
67
68
			return $c;
69
		}, [
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 8.
Loading history...
70
			$this->prophesize( MslsOptions::class ),
71
			$this->prophesize( MslsOptions::class ),
72
			$this->prophesize( MslsOptions::class ),
73
		] );
74
		$inverted = [];
75
		add_filter( 'msls_content_import_relation_local_to_source_create', function ( $_, $local_id, $source_id ) use ( &$inverted ) {
0 ignored issues
show
Unused Code introduced by
The parameter $source_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
			$inverted[ ] = $local_id;
0 ignored issues
show
introduced by
Array keys should NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
77
			return true;
78
		}, 10, 3 );
79
80
		$obj = $this->make_instance();
81
82
		$obj->should_create( $creator_1->reveal(), $dest_lang, $post_ids[0] );
83
		$obj->should_create( $creator_2->reveal(), $dest_lang, $post_ids[1] );
84
		$obj->should_create( $creator_3->reveal(), $dest_lang, $post_ids[2] );
85
86
		$obj->create();
87
88
		$this->assertEquals( $post_ids, $inverted );
89
	}
90
91
	function setUp() {
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
92
		parent::setUp();
93
		$this->import_coordinates = $this->prophesize( ImportCoordinates::class );
94
	}
95
}
96