PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Unit/Inspector/ConstructorAnnotationsInspectorTest.php

https://gitlab.com/nstwf.pro/json-mapper
PHP | 413 lines | 291 code | 83 blank | 39 comment | 0 complexity | 922b5ab8b17e5703cacfa57ddf76e5c0 MD5 | raw file
  1. <?php
  2. namespace Nstwf\JsonMapper\Unit\Inspector;
  3. use Nstwf\JsonMapper\Asserts\ObjectDescriptorAsserts;
  4. use Nstwf\JsonMapper\Cache\NullCache;
  5. use Nstwf\JsonMapper\Inspector\ConstructorAnnotationsInspector;
  6. use Nstwf\JsonMapper\Inspector\ConstructorParametersInspector;
  7. use Nstwf\JsonMapper\Object\ObjectDescriptor;
  8. use Nstwf\JsonMapper\Reflection\ReflectionWrapper;
  9. use Nstwf\JsonMapper\Unit\Implementation\ArrayOfClassesObject;
  10. use Nstwf\JsonMapper\Unit\Implementation\ArrayOfEnumObject;
  11. use Nstwf\JsonMapper\Unit\Implementation\ArrayOfScalarTypesObject;
  12. use Nstwf\JsonMapper\Unit\Implementation\Enum\IntEnum;
  13. use Nstwf\JsonMapper\Unit\Implementation\Enum\StringEnum;
  14. use Nstwf\JsonMapper\Unit\Implementation\EnumObject;
  15. use Nstwf\JsonMapper\Unit\Implementation\MixedTypeObject;
  16. use Nstwf\JsonMapper\Unit\Implementation\Nested\SimpleObject;
  17. use Nstwf\JsonMapper\Unit\Implementation\Nested\SimpleOtherObject;
  18. use Nstwf\JsonMapper\Unit\Implementation\NestedSimpleObject;
  19. use Nstwf\JsonMapper\Unit\Implementation\NestedUnionSimpleObject;
  20. use Nstwf\JsonMapper\Unit\Implementation\NoTypeObject;
  21. use Nstwf\JsonMapper\Unit\Implementation\ScalarTypesObject;
  22. use Nstwf\JsonMapper\Unit\Implementation\UnionEnumObject;
  23. use Nstwf\JsonMapper\Unit\Implementation\UnionScalarTypesObject;
  24. use PHPUnit\Framework\TestCase;
  25. use Psr\SimpleCache\CacheInterface;
  26. class ConstructorAnnotationsInspectorTest extends TestCase
  27. {
  28. public function testInspectScalarTypes()
  29. {
  30. // Arrange
  31. $reflection = new ReflectionWrapper(ScalarTypesObject::class);
  32. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  33. // Act
  34. $objectDescriptor = $inspector->handle($reflection);
  35. // Assert
  36. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  37. $objectDescriptorAsserts->propertyMapAsserts()
  38. ->assertProperty('intProperty')
  39. ->assertType('int', false)
  40. ->assertIsNullable(false);
  41. $objectDescriptorAsserts->propertyMapAsserts()
  42. ->assertProperty('stringProperty')
  43. ->assertType('string', false)
  44. ->assertIsNullable(false);
  45. $objectDescriptorAsserts->propertyMapAsserts()
  46. ->assertProperty('floatProperty')
  47. ->assertType('float', false)
  48. ->assertIsNullable(false);
  49. $objectDescriptorAsserts->propertyMapAsserts()
  50. ->assertProperty('boolProperty')
  51. ->assertType('bool', false)
  52. ->assertIsNullable(false);
  53. $objectDescriptorAsserts->propertyMapAsserts()
  54. ->assertProperty('arrayProperty')
  55. ->assertType('mixed', true)
  56. ->assertIsNullable(false);
  57. $objectDescriptorAsserts->propertyMapAsserts()
  58. ->assertProperty('nullableIntProperty')
  59. ->assertType('int', false)
  60. ->assertIsNullable(true);
  61. }
  62. public function testInspectArrayOfScalarTypes()
  63. {
  64. // Arrange
  65. $reflection = new ReflectionWrapper(ArrayOfScalarTypesObject::class);
  66. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  67. // Act
  68. $objectDescriptor = $inspector->handle($reflection);
  69. // Assert
  70. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  71. $objectDescriptorAsserts->propertyMapAsserts()
  72. ->assertProperty('intArray')
  73. ->assertType('int', true)
  74. ->assertIsNullable(false);
  75. $objectDescriptorAsserts->propertyMapAsserts()
  76. ->assertProperty('stringArray')
  77. ->assertType('string', true)
  78. ->assertIsNullable(false);
  79. $objectDescriptorAsserts->propertyMapAsserts()
  80. ->assertProperty('floatArray')
  81. ->assertType('float', true)
  82. ->assertIsNullable(false);
  83. $objectDescriptorAsserts->propertyMapAsserts()
  84. ->assertProperty('boolArray')
  85. ->assertType('bool', true)
  86. ->assertIsNullable(false);
  87. }
  88. public function testInspectUnionScalarTypes()
  89. {
  90. // Arrange
  91. $reflection = new ReflectionWrapper(UnionScalarTypesObject::class);
  92. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  93. // Act
  94. $objectDescriptor = $inspector->handle($reflection);
  95. // Assert
  96. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  97. $objectDescriptorAsserts->propertyMapAsserts()
  98. ->assertProperty('union')
  99. ->assertType('int', false)
  100. ->assertType('float', false)
  101. ->assertType('string', false)
  102. ->assertType('bool', false)
  103. ->assertIsNullable(false);
  104. $objectDescriptorAsserts->propertyMapAsserts()
  105. ->assertProperty('unionWithArray')
  106. ->assertType('int', false)
  107. ->assertType('float', false)
  108. ->assertType('string', false)
  109. ->assertType('bool', false)
  110. ->assertType('mixed', true)
  111. ->assertIsNullable(false);
  112. $objectDescriptorAsserts->propertyMapAsserts()
  113. ->assertProperty('nullableUnion')
  114. ->assertType('int', false)
  115. ->assertType('float', false)
  116. ->assertType('string', false)
  117. ->assertType('bool', false)
  118. ->assertIsNullable(true);
  119. $objectDescriptorAsserts->propertyMapAsserts()
  120. ->assertProperty('unionArray')
  121. ->assertType('int', true)
  122. ->assertType('float', true)
  123. ->assertType('string', true)
  124. ->assertType('bool', true)
  125. ->assertIsNullable(false);
  126. }
  127. public function testInspectClasses()
  128. {
  129. // Arrange
  130. $reflection = new ReflectionWrapper(NestedSimpleObject::class);
  131. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  132. // Act
  133. $objectDescriptor = $inspector->handle($reflection);
  134. // Assert
  135. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  136. $objectDescriptorAsserts->propertyMapAsserts()
  137. ->assertProperty('simpleObject')
  138. ->assertType(SimpleObject::class, false)
  139. ->assertIsNullable(false);
  140. $objectDescriptorAsserts->propertyMapAsserts()
  141. ->assertProperty('simpleNullableObject')
  142. ->assertType(SimpleObject::class, false)
  143. ->assertIsNullable(true);
  144. }
  145. public function testInspectArrayOfClasses()
  146. {
  147. // Arrange
  148. $reflection = new ReflectionWrapper(ArrayOfClassesObject::class);
  149. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  150. // Act
  151. $objectDescriptor = $inspector->handle($reflection);
  152. // Assert
  153. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  154. $objectDescriptorAsserts->propertyMapAsserts()
  155. ->assertProperty('simpleObjects')
  156. ->assertType(SimpleObject::class, true)
  157. ->assertIsNullable(false);
  158. $objectDescriptorAsserts->propertyMapAsserts()
  159. ->assertProperty('simpleOtherObjects')
  160. ->assertType(SimpleOtherObject::class, true)
  161. ->assertIsNullable(false);
  162. }
  163. public function testInspectUnionClasses()
  164. {
  165. // Arrange
  166. $reflection = new ReflectionWrapper(NestedUnionSimpleObject::class);
  167. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  168. // Act
  169. $objectDescriptor = $inspector->handle($reflection);
  170. // Assert
  171. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  172. $objectDescriptorAsserts->propertyMapAsserts()
  173. ->assertProperty('simpleObject')
  174. ->assertType(SimpleObject::class, false)
  175. ->assertType(SimpleOtherObject::class, false)
  176. ->assertIsNullable(false);
  177. $objectDescriptorAsserts->propertyMapAsserts()
  178. ->assertProperty('simpleNullableObject')
  179. ->assertType(SimpleObject::class, false)
  180. ->assertType(SimpleOtherObject::class, false)
  181. ->assertIsNullable(true);
  182. }
  183. public function testInspectEnums()
  184. {
  185. // Arrange
  186. $reflection = new ReflectionWrapper(EnumObject::class);
  187. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  188. // Act
  189. $objectDescriptor = $inspector->handle($reflection);
  190. // Assert
  191. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  192. $objectDescriptorAsserts->propertyMapAsserts()
  193. ->assertProperty('enum')
  194. ->assertType(IntEnum::class, false)
  195. ->assertIsNullable(false);
  196. $objectDescriptorAsserts->propertyMapAsserts()
  197. ->assertProperty('nullableEnum')
  198. ->assertType(IntEnum::class, false)
  199. ->assertIsNullable(true);
  200. }
  201. public function testInspectArrayOfEnums()
  202. {
  203. // Arrange
  204. $reflection = new ReflectionWrapper(ArrayOfEnumObject::class);
  205. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  206. // Act
  207. $objectDescriptor = $inspector->handle($reflection);
  208. // Assert
  209. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  210. $objectDescriptorAsserts->propertyMapAsserts()
  211. ->assertProperty('intEnums')
  212. ->assertType(IntEnum::class, true)
  213. ->assertIsNullable(false);
  214. $objectDescriptorAsserts->propertyMapAsserts()
  215. ->assertProperty('stringEnums')
  216. ->assertType(StringEnum::class, true)
  217. ->assertIsNullable(false);
  218. }
  219. public function testInspectUnionEnums()
  220. {
  221. // Arrange
  222. $reflection = new ReflectionWrapper(UnionEnumObject::class);
  223. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  224. // Act
  225. $objectDescriptor = $inspector->handle($reflection);
  226. // Assert
  227. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  228. $objectDescriptorAsserts->propertyMapAsserts()
  229. ->assertProperty('enum')
  230. ->assertType(IntEnum::class, false)
  231. ->assertType(StringEnum::class, false)
  232. ->assertIsNullable(false);
  233. $objectDescriptorAsserts->propertyMapAsserts()
  234. ->assertProperty('nullableEnum')
  235. ->assertType(IntEnum::class, false)
  236. ->assertType(StringEnum::class, false)
  237. ->assertIsNullable(true);
  238. }
  239. public function testInspectMixed()
  240. {
  241. // Arrange
  242. $reflection = new ReflectionWrapper(MixedTypeObject::class);
  243. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  244. // Act
  245. $objectDescriptor = $inspector->handle($reflection);
  246. // Assert
  247. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  248. $objectDescriptorAsserts->propertyMapAsserts()
  249. ->assertProperty('mixed')
  250. ->assertType('mixed', false)
  251. ->assertType('mixed', true)
  252. ->assertIsNullable(true);
  253. }
  254. public function testInspectNoType()
  255. {
  256. // Arrange
  257. $reflection = new ReflectionWrapper(NoTypeObject::class);
  258. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  259. // Act
  260. $objectDescriptor = $inspector->handle($reflection);
  261. // Assert
  262. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  263. $objectDescriptorAsserts->propertyMapAsserts()
  264. ->assertNotHasProperty('noType')
  265. ->assertNotHasProperty('noTypeNoDoc');
  266. }
  267. public function testInspectWithoutConstructor()
  268. {
  269. // Arrange
  270. $class = new class {
  271. public int $value;
  272. };
  273. $reflection = new ReflectionWrapper(get_class($class));
  274. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  275. // Act
  276. $objectDescriptor = $inspector->handle($reflection);
  277. // Assert
  278. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  279. $objectDescriptorAsserts->propertyMapAsserts()
  280. ->assertCount(0);
  281. }
  282. public function testInspectWithoutConstructorDocComment()
  283. {
  284. // Arrange
  285. $class = new class {
  286. public function __construct()
  287. {
  288. }
  289. };
  290. $reflection = new ReflectionWrapper(get_class($class));
  291. $inspector = new ConstructorAnnotationsInspector(new NullCache());
  292. // Act
  293. $objectDescriptor = $inspector->handle($reflection);
  294. // Assert
  295. $objectDescriptorAsserts = new ObjectDescriptorAsserts($objectDescriptor);
  296. $objectDescriptorAsserts->propertyMapAsserts()
  297. ->assertCount(0);
  298. }
  299. public function testSetCacheOnSuccessfullyInspect()
  300. {
  301. $cache = $this->createMock(CacheInterface::class);
  302. $cache->method('has')
  303. ->with(sprintf('%s-%s', ConstructorAnnotationsInspector::class, NoTypeObject::class))
  304. ->willReturn(false);
  305. $cache->expects($this->once())
  306. ->method('set')
  307. ->with(sprintf('%s-%s', ConstructorAnnotationsInspector::class, NoTypeObject::class));
  308. $reflection = new ReflectionWrapper(NoTypeObject::class);
  309. $inspector = new ConstructorAnnotationsInspector($cache);
  310. $inspector->handle($reflection);
  311. }
  312. public function testGetCachedValueIfItExists()
  313. {
  314. $reflection = $this->createMock(ReflectionWrapper::class);
  315. $reflection->method('getClassName')->willReturn('MockClass');
  316. $reflection->expects($this->never())->method('getReflectionClass');
  317. $objectDescriptor = new ObjectDescriptor();
  318. $cache = $this->createMock(CacheInterface::class);
  319. $cache->method('has')
  320. ->with(sprintf('%s-%s', ConstructorAnnotationsInspector::class, 'MockClass'))
  321. ->willReturn(true);
  322. $cache->method('get')
  323. ->with(sprintf('%s-%s', ConstructorAnnotationsInspector::class, 'MockClass'))
  324. ->willReturn($objectDescriptor);
  325. $cache->expects($this->never())
  326. ->method('set');
  327. $inspector = new ConstructorAnnotationsInspector($cache);
  328. $inspector->handle($reflection);
  329. }
  330. }