1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\DataTypes\Tests; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\SerializerBuilder; |
6
|
|
|
use SixtyNine\DataTypes\Box; |
7
|
|
|
use SixtyNine\DataTypes\Vector; |
8
|
|
|
|
9
|
|
|
class BoxTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
public function testConstructor() |
12
|
|
|
{ |
13
|
|
|
$box = new Box(1, 2, 3, 4); |
14
|
|
|
$this->assertInstanceOf(Box::class, $box); |
15
|
|
|
$this->assertEquals(1, $box->getX()); |
16
|
|
|
$this->assertEquals(2, $box->getY()); |
17
|
|
|
$this->assertEquals(3, $box->getWidth()); |
18
|
|
|
$this->assertEquals(4, $box->getHeight()); |
19
|
|
|
$this->assertEquals(1, $box->getLeft()); |
20
|
|
|
$this->assertEquals(4, $box->getRight()); |
21
|
|
|
$this->assertEquals(2, $box->getTop()); |
22
|
|
|
$this->assertEquals(6, $box->getBottom()); |
23
|
|
|
$this->assertEquals(new Vector(1, 2), $box->getPosition()); |
24
|
|
|
$this->assertEquals(new Vector(3, 4), $box->getDimensions()); |
25
|
|
|
$this->assertEquals(new Vector(2.5, 4), $box->getCenter()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testCreate() |
29
|
|
|
{ |
30
|
|
|
$box = Box::create(1, 2, 3, 4); |
31
|
|
|
$this->assertEquals(new Box(1, 2, 3, 4), $box); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testToString() |
35
|
|
|
{ |
36
|
|
|
$box = Box::create(11, 22, 33, 44); |
37
|
|
|
$this->assertEquals('[11, 22] x [33, 44]', (string)$box); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testMove() |
41
|
|
|
{ |
42
|
|
|
$box = Box::create(1, 2, 3, 4)->move(10, 20); |
43
|
|
|
$this->assertEquals(Box::create(11, 22, 3, 4), $box); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testResize() |
47
|
|
|
{ |
48
|
|
|
$box = Box::create(1, 2, 3, 4)->resize(1); |
49
|
|
|
$this->assertEquals(Box::create(0, 1, 5, 6), $box); |
50
|
|
|
|
51
|
|
|
$box = Box::create(1, 2, 3, 4)->resize(-1); |
52
|
|
|
$this->assertEquals(Box::create(2, 3, 1, 2), $box); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @dataProvider intersectBoxesProvider |
57
|
|
|
*/ |
58
|
|
|
public function testIntersect(Box $b1, Box $b2, $shouldCollide) |
59
|
|
|
{ |
60
|
|
|
$collide = $b1->intersects($b2); |
61
|
|
|
|
62
|
|
|
if ($collide && !$shouldCollide) { |
63
|
|
|
$this->fail('Collision not expected'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$collide && $shouldCollide) { |
67
|
|
|
$this->fail('Collision expected'); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function intersectBoxesProvider() |
75
|
|
|
{ |
76
|
|
|
return array( |
77
|
|
|
array(new Box(1, 1, 1, 1), new Box(2, 2, 1, 1), false), |
78
|
|
|
array(new Box(1, 1, 1, 1), new Box(1, 1, 1, 1), true), |
79
|
|
|
array(new Box(10, 10, 100, 50), new Box(5, 5, 5, 50), false), |
80
|
|
|
array(new Box(10, 10, 100, 50), new Box(5, 5, 50, 50), true), |
81
|
|
|
array(new Box(0, 10, 100, 10), new Box(10, 0, 10, 100), true), |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @dataProvider insideBoxesProvider |
87
|
|
|
*/ |
88
|
|
|
public function testInside(Box $b1, Box $b2, $shouldBeInside) |
89
|
|
|
{ |
90
|
|
|
$this->assertTrue($shouldBeInside === $b2->inside($b1)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function insideBoxesProvider() |
97
|
|
|
{ |
98
|
|
|
return array( |
99
|
|
|
array(Box::create(10, 10, 10, 10), Box::create(15, 15, 2, 2), true), |
100
|
|
|
array(Box::create(15, 15, 2, 2), Box::create(10, 10, 10, 10), false), |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testSerialize() |
105
|
|
|
{ |
106
|
|
|
$data = SerializerBuilder::create() |
107
|
|
|
->build() |
108
|
|
|
->serialize(Box::create(123, 321, 111, 222), 'json') |
109
|
|
|
; |
110
|
|
|
$this->assertEquals('{"x":123,"y":321,"width":111,"height":222}', $data); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|