public function testRenderingWithoutRenderer() { $this->removeRendererFromPhrase(); $result = 'some text'; $this->phrase = new \Magento\Framework\Phrase($result); $this->assertEquals($result, $this->phrase->render()); }
/** * Test rendering * * @return void */ public function testRendering() { $text = 'some text'; $arguments = ['arg1', 'arg2']; $result = 'rendered text'; $phrase = new Phrase($text, $arguments); Phrase::setRenderer($this->rendererMock); $this->rendererMock->expects($this->once())->method('render')->with([$text], $arguments)->willReturn($result); $this->assertEquals($result, $phrase->render()); }
/** * Combine submessages delimited by separator and render them with main message * * @param string $mainMessage * @param MessageInterface[] $subMessages * @param string $separator * @return Phrase */ public function create($mainMessage, $subMessages, $separator = '; ') { $renderedErrors = ''; $eol = ''; /** @var MessageInterface $subMessage */ foreach ($subMessages as $subMessage) { if ($subMessage instanceof MessageInterface) { $phrase = new Phrase($subMessage->getText()); } else { $phrase = new Phrase('Cannot render error message!'); } $renderedErrors .= $eol . $phrase->render(); $eol = $separator; } //$mainMessage should contain %1 to be substituted by concatenated errors return new Phrase($mainMessage, [$renderedErrors]); }
/** * Add new error into the list of exceptions * * @param \Magento\Framework\Phrase $phrase * @return $this */ public function addError(Phrase $phrase) { $this->addErrorCalls++; if (empty($this->errors)) { if (1 === $this->addErrorCalls) { // First call: simply overwrite the phrase and message $this->phrase = $phrase; $this->message = $phrase->render(); $this->logMessage = null; } elseif (2 === $this->addErrorCalls) { // Second call: store the error from the first call and the second call in the array // restore the phrase to its original value $this->errors[] = new LocalizedException($this->phrase); $this->errors[] = new LocalizedException($phrase); $this->phrase = $this->originalPhrase; $this->message = $this->originalPhrase->render(); $this->logMessage = null; } } else { // All subsequent calls after the second should reach here $this->errors[] = new LocalizedException($phrase); } return $this; }
/** * Constructor * * @param \Magento\Framework\Phrase $phrase * @param \Exception $cause */ public function __construct(Phrase $phrase, \Exception $cause = null) { $this->phrase = $phrase; parent::__construct($phrase->render(), 0, $cause); }