Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TypeCasterTest.php
Go to the documentation of this file.
1 <?php
7 
11 use \PHPUnit_Framework_MockObject_MockObject as MockObject;
12 
16 class TypeCasterTest extends \PHPUnit\Framework\TestCase
17 {
21  private $model;
22 
26  private $serializer;
27 
31  protected function setUp()
32  {
33  $objectManager = new ObjectManager($this);
34  $this->serializer = $this->getMockBuilder(Json::class)
35  ->getMock();
36 
37  $this->model = $objectManager->getObject(TypeCaster::class, ['serializer' => $this->serializer]);
38  }
39 
48  public function testCastValues($origValue, $typeToCast, $expectedValue)
49  {
50  $this->serializer->expects(self::never())
51  ->method('serialize');
52 
53  $value = $this->model->castValueToType($origValue, $typeToCast);
54  self::assertEquals($expectedValue, $value);
55  }
56 
66  public function testCastValueToType(array $origValue, $typeToCast, $expected)
67  {
68  $this->serializer->expects(self::once())
69  ->method('serialize')
70  ->with(self::equalTo($origValue))
71  ->willReturn(json_encode($origValue));
72 
73  $actual = $this->model->castValueToType($origValue, $typeToCast);
74  self::assertEquals($expected, $actual);
75  }
76 
80  public function typeCastValueProvider()
81  {
82  return [
83  'null' => [null, 'int', null],
84  'int' => ['1', 'int', 1],
85  'integer' => ['1', 'integer', 1],
86  'string' => ['1', 'string', '1'],
87  'bool 0' => ['0', 'bool', false],
88  'bool 1' => ['1', 'bool', true],
89  'boolean 0' => ['0', 'boolean', false],
90  'boolean 1' => ['1', 'boolean', true],
91  'true' => ['1', 'true', true],
92  'false' => ['0', 'false', false],
93  'float' => ['1.03', 'float', 1.03],
94  'double' => ['1.30', 'double', 1.30],
95  'array of objects' => [[1, 2.0, '3b'], \stdClass::class, [1, 2.0, '3b']],
96  'array of interfaces' => [['a', 23, '1.a'], \Traversable::class, ['a', 23, '1.a']],
97  ];
98  }
99 
105  public function arraysDataProvider()
106  {
107  return [
108  [['type' => 'VI', 'masked' => 1111], 'string', '{"type":"VI","masked":1111}'],
109  [['status' => 'processing', 'parent_id' => 2], 'int', '{"status":"processing","parent_id":2}'],
110  [['parent' => ['children' => [1, 2]], 'node' => 2], 'mixed', '{"parent":{"children":[1,2]},"node":2}'],
111  ];
112  }
113 }
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16
testCastValues($origValue, $typeToCast, $expectedValue)
testCastValueToType(array $origValue, $typeToCast, $expected)