55namespace PhpKafka \PhpAvroSchemaGenerator \Tests \Unit \Optimizer ;
66
77use PhpKafka \PhpAvroSchemaGenerator \Optimizer \PrimitiveSchemaOptimizer ;
8+ use PhpKafka \PhpAvroSchemaGenerator \Schema \SchemaTemplateInterface ;
89use PHPUnit \Framework \TestCase ;
910
1011class PrimitiveSchemaOptimizerTest extends TestCase
@@ -15,9 +16,27 @@ public function testOptimize(): void
1516
1617 $ expectedResult = json_encode (json_decode ('"string" ' ));
1718
19+
20+ $ schemaTemplate = $ this ->getMockForAbstractClass (SchemaTemplateInterface::class);
21+ $ schemaTemplate
22+ ->expects (self ::once ())
23+ ->method ('getSchemaDefinition ' )
24+ ->willReturn ($ schema );
25+
26+ $ schemaTemplate
27+ ->expects (self ::once ())
28+ ->method ('withSchemaDefinition ' )
29+ ->with ($ expectedResult )
30+ ->willReturn ($ schemaTemplate );
31+
32+ $ schemaTemplate
33+ ->expects (self ::once ())
34+ ->method ('isPrimitive ' )
35+ ->willReturn (true );
36+
1837 $ optimizer = new PrimitiveSchemaOptimizer ();
1938
20- self ::assertEquals ( $ expectedResult , $ optimizer ->optimize ($ schema , true ));
39+ self ::assertInstanceOf (SchemaTemplateInterface::class , $ optimizer ->optimize ($ schemaTemplate ));
2140 }
2241
2342 public function testOptimizeForStringSchema (): void
@@ -26,19 +45,42 @@ public function testOptimizeForStringSchema(): void
2645
2746 $ expectedResult = json_encode (json_decode ('"string" ' ));
2847
48+
49+ $ schemaTemplate = $ this ->getMockForAbstractClass (SchemaTemplateInterface::class);
50+ $ schemaTemplate
51+ ->expects (self ::once ())
52+ ->method ('getSchemaDefinition ' )
53+ ->willReturn ($ schema );
54+
55+ $ schemaTemplate
56+ ->expects (self ::once ())
57+ ->method ('withSchemaDefinition ' )
58+ ->with ($ expectedResult )
59+ ->willReturn ($ schemaTemplate );
60+
61+ $ schemaTemplate
62+ ->expects (self ::once ())
63+ ->method ('isPrimitive ' )
64+ ->willReturn (true );
65+
2966 $ optimizer = new PrimitiveSchemaOptimizer ();
3067
31- self ::assertEquals ( $ expectedResult , $ optimizer ->optimize ($ schema , true ));
68+ self ::assertInstanceOf (SchemaTemplateInterface::class , $ optimizer ->optimize ($ schemaTemplate ));
3269 }
3370
3471 public function testOptimizeForRecordSchema (): void
3572 {
36- $ schema = '{"type":"record","namespace":"com.example","name":"Book","fields":[{"name":"isbn","type":"string"}]} ' ;
73+ $ schemaTemplate = $ this ->getMockForAbstractClass (SchemaTemplateInterface::class);
74+ $ schemaTemplate ->expects (self ::never ())->method ('getSchemaDefinition ' );
75+ $ schemaTemplate ->expects (self ::never ())->method ('withSchemaDefinition ' );
3776
38- $ expectedResult = json_encode (json_decode ($ schema ));
77+ $ schemaTemplate
78+ ->expects (self ::once ())
79+ ->method ('isPrimitive ' )
80+ ->willReturn (false );
3981
4082 $ optimizer = new PrimitiveSchemaOptimizer ();
4183
42- self ::assertEquals ( $ expectedResult , $ optimizer ->optimize ($ schema , false ));
84+ self ::assertInstanceOf (SchemaTemplateInterface::class , $ optimizer ->optimize ($ schemaTemplate ));
4385 }
4486}
0 commit comments