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