|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Translation\Tests\Formatter; |
| 13 | + |
| 14 | +use Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 15 | +use Symfony\Component\Translation\Exception\LogicException; |
| 16 | +use Symfony\Component\Translation\Formatter\ChoiceMessageFormatterInterface; |
| 17 | +use Symfony\Component\Translation\Formatter\FallbackFormatter; |
| 18 | +use Symfony\Component\Translation\Formatter\MessageFormatterInterface; |
| 19 | + |
| 20 | +class FallbackFormatterTest extends \PHPUnit\Framework\TestCase |
| 21 | +{ |
| 22 | + public function testFormatSame() |
| 23 | + { |
| 24 | + $first = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 25 | + $first |
| 26 | + ->expects($this->once()) |
| 27 | + ->method('format') |
| 28 | + ->with('foo', 'en', array(2)) |
| 29 | + ->willReturn('foo'); |
| 30 | + |
| 31 | + $second = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 32 | + $second |
| 33 | + ->expects($this->once()) |
| 34 | + ->method('format') |
| 35 | + ->with('foo', 'en', array(2)) |
| 36 | + ->willReturn('bar'); |
| 37 | + |
| 38 | + $this->assertEquals('bar', (new FallbackFormatter($first, $second))->format('foo', 'en', array(2))); |
| 39 | + } |
| 40 | + |
| 41 | + public function testFormatDifferent() |
| 42 | + { |
| 43 | + $first = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 44 | + $first |
| 45 | + ->expects($this->once()) |
| 46 | + ->method('format') |
| 47 | + ->with('foo', 'en', array(2)) |
| 48 | + ->willReturn('new value'); |
| 49 | + |
| 50 | + $second = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 51 | + $second |
| 52 | + ->expects($this->exactly(0)) |
| 53 | + ->method('format') |
| 54 | + ->withAnyParameters(); |
| 55 | + |
| 56 | + $this->assertEquals('new value', (new FallbackFormatter($first, $second))->format('foo', 'en', array(2))); |
| 57 | + } |
| 58 | + |
| 59 | + public function testFormatException() |
| 60 | + { |
| 61 | + $first = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 62 | + $first |
| 63 | + ->expects($this->once()) |
| 64 | + ->method('format') |
| 65 | + ->willThrowException(new InvalidArgumentException()); |
| 66 | + |
| 67 | + $second = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 68 | + $second |
| 69 | + ->expects($this->once()) |
| 70 | + ->method('format') |
| 71 | + ->with('foo', 'en', array(2)) |
| 72 | + ->willReturn('bar'); |
| 73 | + |
| 74 | + $this->assertEquals('bar', (new FallbackFormatter($first, $second))->format('foo', 'en', array(2))); |
| 75 | + } |
| 76 | + |
| 77 | + public function testChoiceFormatSame() |
| 78 | + { |
| 79 | + $first = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock(); |
| 80 | + $first |
| 81 | + ->expects($this->once()) |
| 82 | + ->method('choiceFormat') |
| 83 | + ->with('foo', 1, 'en', array(2)) |
| 84 | + ->willReturn('foo'); |
| 85 | + |
| 86 | + $second = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock(); |
| 87 | + $second |
| 88 | + ->expects($this->once()) |
| 89 | + ->method('choiceFormat') |
| 90 | + ->with('foo', 1, 'en', array(2)) |
| 91 | + ->willReturn('bar'); |
| 92 | + |
| 93 | + $this->assertEquals('bar', (new FallbackFormatter($first, $second))->choiceFormat('foo', 1, 'en', array(2))); |
| 94 | + } |
| 95 | + |
| 96 | + public function testChoiceFormatDifferent() |
| 97 | + { |
| 98 | + $first = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock(); |
| 99 | + $first |
| 100 | + ->expects($this->once()) |
| 101 | + ->method('choiceFormat') |
| 102 | + ->with('foo', 1, 'en', array(2)) |
| 103 | + ->willReturn('new value'); |
| 104 | + |
| 105 | + $second = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock(); |
| 106 | + $second |
| 107 | + ->expects($this->exactly(0)) |
| 108 | + ->method('choiceFormat') |
| 109 | + ->withAnyParameters() |
| 110 | + ->willReturn('bar'); |
| 111 | + |
| 112 | + $this->assertEquals('new value', (new FallbackFormatter($first, $second))->choiceFormat('foo', 1, 'en', array(2))); |
| 113 | + } |
| 114 | + |
| 115 | + public function testChoiceFormatException() |
| 116 | + { |
| 117 | + $first = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock(); |
| 118 | + $first |
| 119 | + ->expects($this->once()) |
| 120 | + ->method('choiceFormat') |
| 121 | + ->willThrowException(new InvalidArgumentException()); |
| 122 | + |
| 123 | + $second = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock(); |
| 124 | + $second |
| 125 | + ->expects($this->once()) |
| 126 | + ->method('choiceFormat') |
| 127 | + ->with('foo', 1, 'en', array(2)) |
| 128 | + ->willReturn('bar'); |
| 129 | + |
| 130 | + $this->assertEquals('bar', (new FallbackFormatter($first, $second))->choiceFormat('foo', 1, 'en', array(2))); |
| 131 | + } |
| 132 | + |
| 133 | + public function testChoiceFormatOnlyFirst() |
| 134 | + { |
| 135 | + // Implements both interfaces |
| 136 | + $first = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock(); |
| 137 | + $first |
| 138 | + ->expects($this->once()) |
| 139 | + ->method('choiceFormat') |
| 140 | + ->with('foo', 1, 'en', array(2)) |
| 141 | + ->willReturn('bar'); |
| 142 | + |
| 143 | + // Implements only one interface |
| 144 | + $second = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 145 | + $second |
| 146 | + ->expects($this->exactly(0)) |
| 147 | + ->method('format') |
| 148 | + ->withAnyParameters() |
| 149 | + ->willReturn('error'); |
| 150 | + |
| 151 | + $this->assertEquals('bar', (new FallbackFormatter($first, $second))->choiceFormat('foo', 1, 'en', array(2))); |
| 152 | + } |
| 153 | + |
| 154 | + public function testChoiceFormatOnlySecond() |
| 155 | + { |
| 156 | + // Implements only one interface |
| 157 | + $first = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 158 | + $first |
| 159 | + ->expects($this->exactly(0)) |
| 160 | + ->method('format') |
| 161 | + ->withAnyParameters() |
| 162 | + ->willReturn('error'); |
| 163 | + |
| 164 | + // Implements both interfaces |
| 165 | + $second = $this->getMockBuilder(SuperFormatterInterface::class)->setMethods(array('format', 'choiceFormat'))->getMock(); |
| 166 | + $second |
| 167 | + ->expects($this->once()) |
| 168 | + ->method('choiceFormat') |
| 169 | + ->with('foo', 1, 'en', array(2)) |
| 170 | + ->willReturn('bar'); |
| 171 | + |
| 172 | + $this->assertEquals('bar', (new FallbackFormatter($first, $second))->choiceFormat('foo', 1, 'en', array(2))); |
| 173 | + } |
| 174 | + |
| 175 | + public function testChoiceFormatNoChoiceFormat() |
| 176 | + { |
| 177 | + // Implements only one interface |
| 178 | + $first = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 179 | + $first |
| 180 | + ->expects($this->exactly(0)) |
| 181 | + ->method('format'); |
| 182 | + |
| 183 | + // Implements both interfaces |
| 184 | + $second = $this->getMockBuilder(MessageFormatterInterface::class)->setMethods(array('format'))->getMock(); |
| 185 | + $second |
| 186 | + ->expects($this->exactly(0)) |
| 187 | + ->method('format'); |
| 188 | + |
| 189 | + $this->expectException(LogicException::class); |
| 190 | + $this->assertEquals('bar', (new FallbackFormatter($first, $second))->choiceFormat('foo', 1, 'en', array(2))); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +interface SuperFormatterInterface extends MessageFormatterInterface, ChoiceMessageFormatterInterface |
| 195 | +{ |
| 196 | +} |
0 commit comments