/** * Setting properties and outputting json. */ public function testSetters() { $obj = new TextTestClass('some text'); $expected = '{"role":"role","text":"some text"}'; $this->assertJsonStringEqualsJsonString($expected, $obj->json()); $expected = '{"role":"role","text":"some other text"}'; $obj->setText('some other text'); $this->assertEquals('some other text', $obj->getText()); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); $expected = '{"role":"role","text":"some other text","format":"markdown"}'; $obj->setFormat('markdown'); $this->assertEquals('markdown', $obj->getFormat()); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); // Test assigning document level objects. $document = new Document('1', 'title', 'en-us', new Layout(2, 512)); $expected = '{"role":"role","text":"some other text","format":"markdown","textStyle":"key"}'; $style = new ComponentTextStyle(); $document->addComponentTextStyle('key', $style); $obj->setTextStyle('key', $document); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); @$obj->setTextStyle('invalid key', $document); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); $expected = '{"role":"role","text":"some other text","format":"markdown","textStyle":"key","inlineTextStyles":[{"rangeStart":0,"rangeLength":1,"textStyle":{}}]}'; $style = new InlineTextStyle(0, 1, new TextStyle()); $obj->addInlineTextStyles($style); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); }
/** * Setting properties and outputting json. * * @depends testJsonRequired */ public function testSetters() { $obj = new Document(1, 'title', 'en', new Layout(7, 1024)); $obj->addComponent(new Body('body text'))->addComponentTextStyle('default', new ComponentTextStyle()); $expected = '{"version":"' . $obj->getVersion() . '","identifier":"1","title":"title","language":"en","layout":{"columns":7,"width":1024},"components":[{"text":"body text","role": "body"}],"componentTextStyles":{"default":{}}}'; $this->assertJsonStringEqualsJsonString($expected, $obj->json()); // Optional properties. $expected = '{"version":"' . $obj->getVersion() . '","identifier":"1","title":"title","subtitle":"subtitle","language":"en","layout":{"columns":7,"width":1024},"components":[{"text":"body text","role": "body"}],"componentTextStyles":{"default":{}}}'; $obj->setSubtitle('subtitle'); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); // Test validation. $obj = new Document(1, 'title', 'en', new Layout(7, 1024)); $this->assertEquals(FALSE, $obj->json()); }
/** * Setting properties and outputting json. */ public function testSetters() { $obj = new ComponentTestClass(); $expected = '{"role":"role"}'; $this->assertJsonStringEqualsJsonString($expected, $obj->json()); // Test assigning document level objects. $expected = '{"role":"role","layout":"key"}'; $layout = new ComponentLayout(); $document = new Document('1', 'title', 'en-us', new Layout(2, 512)); $document->addComponentLayout('key', $layout); $obj->setLayout('key', $document); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); @$obj->setLayout('invalid key', $document); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); }
/** * Setting properties and outputting json. */ public function testSetters() { $obj = new InlineTextStyle(0, 1, new TextStyle()); $expected = '{"rangeStart":0,"rangeLength":1,"textStyle":{}}'; $this->assertJsonStringEqualsJsonString($expected, $obj->json()); $expected = '{"rangeStart":1,"rangeLength":10,"textStyle":{}}'; $obj->setRangeLength(10); $obj->setRangeStart(1); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); // Test assigning document level objects. $document = new Document('1', 'title', 'en-us', new Layout(2, 512)); $expected = '{"rangeStart":1,"rangeLength":10,"textStyle":"key"}'; $style = new TextStyle(); $document->addTextStyle('key', $style); $obj->setTextStyle('key', $document); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); @$obj->setTextStyle('invalid key', $document); $this->assertJsonStringEqualsJsonString($expected, $obj->json()); }
/** * Setter for textStyle. * * @param \ChapterThree\AppleNewsAPI\Document\Styles\ComponentTextStyle|string $text_style * Either a ComponentTextStyle object, or a string reference to one defined * in $document. * @param \ChapterThree\AppleNewsAPI\Document|NULL $document * If required by first parameter. * * @return $this */ public function setTextStyle($text_style, Document $document = NULL) { $class = 'ChapterThree\\AppleNewsAPI\\Document\\Styles\\ComponentTextStyle'; if (is_string($text_style)) { // Check that text_style exists. if ($document && empty($document->getComponentTextStyles()[$text_style])) { $this->triggerError("No ComponentTextStyle \"{$text_style}\" found."); return $this; } } elseif (!$text_style instanceof $class) { $this->triggerError("Style not of class {$class}."); return $this; } $this->textStyle = $text_style; return $this; }