Version 1
[yaffs-website] / web / core / modules / rest / tests / src / Unit / Plugin / views / style / SerializerTest.php
1 <?php
2
3 namespace Drupal\Tests\rest\Unit\Plugin\views\style;
4
5 use Drupal\rest\Plugin\views\display\RestExport;
6 use Drupal\rest\Plugin\views\style\Serializer;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\views\ViewExecutable;
9 use Prophecy\Argument;
10 use Symfony\Component\Serializer\SerializerInterface;
11
12 /**
13  * @coversDefaultClass \Drupal\rest\Plugin\views\style\Serializer
14  * @group rest
15  */
16 class SerializerTest extends UnitTestCase {
17
18   /**
19    * The View instance.
20    *
21    * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
22    */
23   protected $view;
24
25   /**
26    * The RestExport display handler.
27    *
28    * @var \Drupal\rest\Plugin\views\display\RestExport|\PHPUnit_Framework_MockObject_MockObject
29    */
30   protected $displayHandler;
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37
38     $this->view = $this->getMockBuilder(ViewExecutable::class)
39       ->disableOriginalConstructor()
40       ->getMock();
41
42     // Make the view result empty so we don't have to mock the row plugin render
43     // call.
44     $this->view->result = [];
45
46     $this->displayHandler = $this->getMockBuilder(RestExport::class)
47       ->disableOriginalConstructor()
48       ->getMock();
49
50     $this->displayHandler->expects($this->any())
51       ->method('getContentType')
52       ->willReturn('json');
53   }
54
55   /**
56    * Tests that the symfony serializer receives style plugin from the render() method.
57    *
58    * @covers ::render
59    */
60   public function testSerializerReceivesOptions() {
61     $mock_serializer = $this->prophesize(SerializerInterface::class);
62
63     // This is the main expectation of the test. We want to make sure the
64     // serializer options are passed to the SerializerInterface object.
65     $mock_serializer->serialize([], 'json', Argument::that(function ($argument) {
66       return isset($argument['views_style_plugin']) && $argument['views_style_plugin'] instanceof Serializer;
67     }))
68       ->willReturn()
69       ->shouldBeCalled();
70
71     $view_serializer_style = new Serializer([], 'dummy_serializer', [], $mock_serializer->reveal(), ['json', 'xml'], ['json' => 'serialization', 'xml' => 'serialization']);
72     $view_serializer_style->options = ['formats' => ['xml', 'json']];
73     $view_serializer_style->view = $this->view;
74     $view_serializer_style->displayHandler = $this->displayHandler;
75     $view_serializer_style->render();
76   }
77
78 }