1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Tests\Builder; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use SixtyNine\Cloud\Builder\CloudBuilder; |
7
|
|
|
use SixtyNine\Cloud\Builder\PalettesBuilder; |
8
|
|
|
use SixtyNine\Cloud\Builder\WordsListBuilder; |
9
|
|
|
use SixtyNine\Cloud\Color\RandomColorGenerator; |
10
|
|
|
use SixtyNine\Cloud\Factory\FontsFactory; |
11
|
|
|
use SixtyNine\Cloud\Model\Cloud; |
12
|
|
|
use SixtyNine\Cloud\Model\CloudWord; |
13
|
|
|
use SixtyNine\Cloud\Model\Word; |
14
|
|
|
use SixtyNine\Cloud\Model\WordsList; |
15
|
|
|
use SixtyNine\Cloud\Serializer; |
16
|
|
|
use SixtyNine\DataTypes\Box; |
17
|
|
|
|
18
|
|
|
class SerializerTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
public function testSaveLoadList() |
21
|
|
|
{ |
22
|
|
|
$colorGenerator = new RandomColorGenerator(PalettesBuilder::create()->getRandomPalette()); |
23
|
|
|
|
24
|
|
|
$list = WordsListBuilder::create() |
25
|
|
|
->randomizeOrientation(50) |
26
|
|
|
->randomizeColors($colorGenerator) |
27
|
|
|
->importWords('foobar foo foo bar') |
28
|
|
|
->sort(WordsList::SORT_COUNT, WordsList::SORT_DESC) |
29
|
|
|
->build('foobar') |
30
|
|
|
; |
31
|
|
|
|
32
|
|
|
$serializer = new Serializer(); |
33
|
|
|
$json = $serializer->saveList($list, true); |
34
|
|
|
|
35
|
|
|
$obj = $serializer->loadList($json); |
36
|
|
|
|
37
|
|
|
$this->assertEquals($list, $obj); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testSaveLoadCloud() |
41
|
|
|
{ |
42
|
|
|
$colorGenerator = new RandomColorGenerator(PalettesBuilder::create()->getRandomPalette()); |
43
|
|
|
|
44
|
|
|
$list = WordsListBuilder::create() |
45
|
|
|
->randomizeOrientation(50) |
46
|
|
|
->randomizeColors($colorGenerator) |
47
|
|
|
->importWords('foobar foo foo bar') |
48
|
|
|
->build('foobar') |
49
|
|
|
; |
50
|
|
|
|
51
|
|
|
$factory = FontsFactory::create(__DIR__ . '/fixtures/fonts'); |
52
|
|
|
$cloud = CloudBuilder::create($factory) |
53
|
|
|
->setFont('Arial.ttf') |
54
|
|
|
->useList($list) |
55
|
|
|
->build() |
56
|
|
|
; |
57
|
|
|
|
58
|
|
|
$serializer = new Serializer(); |
59
|
|
|
$json = $serializer->saveCloud($cloud, true); |
60
|
|
|
$obj = $serializer->loadCloud($json); |
61
|
|
|
|
62
|
|
|
$this->assertEquals($cloud, $obj); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testLoadWordsList() |
66
|
|
|
{ |
67
|
|
|
$serializer = new Serializer(); |
68
|
|
|
$list = $serializer->loadList(file_get_contents(__DIR__ . '/fixtures/wordlist.json')); |
69
|
|
|
|
70
|
|
|
$this->assertInstanceOf(WordsList::class, $list); |
71
|
|
|
$this->assertEquals('foobar', $list->getName()); |
72
|
|
|
$this->assertEquals(2, $list->getWordsCount()); |
73
|
|
|
$this->assertEquals(20, $list->getWordsMaxCount()); |
74
|
|
|
|
75
|
|
|
/** @var Word $word */ |
76
|
|
|
$word = $list->getWords()->first(); |
77
|
|
|
|
78
|
|
|
$this->assertInstanceOf(Word::class, $word); |
79
|
|
|
$this->assertEquals('foo', $word->getText()); |
80
|
|
|
$this->assertEquals(10, $word->getCount()); |
81
|
|
|
$this->assertEquals('#000000', $word->getColor()); |
82
|
|
|
$this->assertEquals(1, $word->getPosition()); |
83
|
|
|
|
84
|
|
|
$word = $list->getWords()->next(); |
85
|
|
|
|
86
|
|
|
$this->assertInstanceOf(Word::class, $word); |
87
|
|
|
$this->assertEquals('bar', $word->getText()); |
88
|
|
|
$this->assertEquals(20, $word->getCount()); |
89
|
|
|
$this->assertEquals('#ffffff', $word->getColor()); |
90
|
|
|
$this->assertEquals(0, $word->getPosition()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testLoadCloud() |
94
|
|
|
{ |
95
|
|
|
$serializer = new Serializer(); |
96
|
|
|
$cloud = $serializer->loadCloud(file_get_contents(__DIR__ . '/fixtures/cloud.json')); |
97
|
|
|
|
98
|
|
|
$this->assertInstanceOf(Cloud::class, $cloud); |
99
|
|
|
$this->assertEquals('#ffffff', $cloud->getBackgroundColor()); |
100
|
|
|
$this->assertEquals(800, $cloud->getWidth()); |
101
|
|
|
$this->assertEquals(600, $cloud->getHeight()); |
102
|
|
|
$this->assertEquals('Arial.ttf', $cloud->getFont()); |
103
|
|
|
|
104
|
|
|
$words = $cloud->getWords(); |
105
|
|
|
|
106
|
|
|
$this->assertInstanceOf(ArrayCollection::class, $words); |
107
|
|
|
$this->assertCount(2, $words); |
108
|
|
|
|
109
|
|
|
/** @var CloudWord $word */ |
110
|
|
|
$word = $words->first(); |
111
|
|
|
$this->assertInstanceOf(CloudWord::class, $word); |
112
|
|
|
$this->assertEquals('foo', $word->getText()); |
113
|
|
|
$this->assertEquals(60, $word->getSize()); |
114
|
|
|
$this->assertEquals(0, $word->getAngle()); |
115
|
|
|
$this->assertEquals('#8c4b47', $word->getColor()); |
116
|
|
|
$this->assertEquals(true, $word->getIsVisible()); |
117
|
|
|
$this->assertTrue(is_array($word->getPosition())); |
118
|
|
|
$this->assertEquals(array(400, 300), $word->getPosition()); |
119
|
|
|
$this->assertInstanceOf(Box::class, $word->getBox()); |
120
|
|
|
$this->assertEquals(400, $word->getBox()->getX()); |
121
|
|
|
$this->assertEquals(300, $word->getBox()->getY()); |
122
|
|
|
$this->assertEquals(112, $word->getBox()->getWidth()); |
123
|
|
|
$this->assertEquals(61, $word->getBox()->getHeight()); |
124
|
|
|
|
125
|
|
|
$word = $words->next(); |
126
|
|
|
$this->assertInstanceOf(CloudWord::class, $word); |
127
|
|
|
$this->assertEquals('bar', $word->getText()); |
128
|
|
|
$this->assertEquals(35, $word->getSize()); |
129
|
|
|
$this->assertEquals(90, $word->getAngle()); |
130
|
|
|
$this->assertEquals('#8c4b47', $word->getColor()); |
131
|
|
|
$this->assertEquals(true, $word->getIsVisible()); |
132
|
|
|
$this->assertTrue(is_array($word->getPosition())); |
133
|
|
|
$this->assertEquals(array(389, 263), $word->getPosition()); |
134
|
|
|
$this->assertInstanceOf(Box::class, $word->getBox()); |
135
|
|
|
$this->assertEquals(389, $word->getBox()->getX()); |
136
|
|
|
$this->assertEquals(263, $word->getBox()->getY()); |
137
|
|
|
$this->assertEquals(68, $word->getBox()->getWidth()); |
138
|
|
|
$this->assertEquals(36, $word->getBox()->getHeight()); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|