@@ -529,4 +529,208 @@ public function setZedValue($name, $value)
529
529
$ this ->assertSame ($ expected , $ result );
530
530
531
531
}
532
+
533
+
534
+ public function testClassPropertyDefaultsActivated ()
535
+ {
536
+ $ schemaData = json_decode (<<<'JSON'
537
+ {
538
+ "properties": {
539
+ "stringDefault": {
540
+ "type": "string",
541
+ "default": "list"
542
+ },
543
+ "booleanDefault": {
544
+ "type": "boolean",
545
+ "default": false
546
+ },
547
+ "integerDefault": {
548
+ "type": "integer",
549
+ "default": 1
550
+ },
551
+ "arrayDefault": {
552
+ "type": "array",
553
+ "default": []
554
+ },
555
+ "noDefault": {
556
+ "type": "string"
557
+ }
558
+ }
559
+ }
560
+ JSON
561
+ );
562
+
563
+ $ schema = Schema::import ($ schemaData );
564
+ $ builder = new PhpBuilder ();
565
+ $ builder ->buildSetters = true ;
566
+ $ builder ->declarePropertyDefaults = true ;
567
+ $ class = $ builder ->getClass ($ schema , 'Root ' );
568
+
569
+ $ result = '' ;
570
+ foreach ($ builder ->getGeneratedClasses () as $ class ) {
571
+ $ result .= $ class ->class . "\n\n" ;
572
+ }
573
+
574
+ $ expected = <<<'PHP'
575
+ class Root extends Swaggest\JsonSchema\Structure\ClassStructure
576
+ {
577
+ /** @var string */
578
+ public $stringDefault = 'list';
579
+
580
+ /** @var bool */
581
+ public $booleanDefault = false;
582
+
583
+ /** @var int */
584
+ public $integerDefault = 1;
585
+
586
+ /** @var array */
587
+ public $arrayDefault = array (
588
+ );
589
+
590
+ /** @var string */
591
+ public $noDefault;
592
+
593
+ /**
594
+ * @param Swaggest\JsonSchema\Constraint\Properties|static $properties
595
+ * @param Swaggest\JsonSchema\Schema $ownerSchema
596
+ */
597
+ public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $ownerSchema)
598
+ {
599
+ $properties->stringDefault = Swaggest\JsonSchema\Schema::string();
600
+ $properties->stringDefault->default = "list";
601
+ $properties->booleanDefault = Swaggest\JsonSchema\Schema::boolean();
602
+ $properties->booleanDefault->default = false;
603
+ $properties->integerDefault = Swaggest\JsonSchema\Schema::integer();
604
+ $properties->integerDefault->default = 1;
605
+ $properties->arrayDefault = Swaggest\JsonSchema\Schema::arr();
606
+ $properties->arrayDefault->default = array();
607
+ $properties->noDefault = Swaggest\JsonSchema\Schema::string();
608
+ }
609
+
610
+ /**
611
+ * @param string $stringDefault
612
+ * @return $this
613
+ * @codeCoverageIgnoreStart
614
+ */
615
+ public function setStringDefault($stringDefault)
616
+ {
617
+ $this->stringDefault = $stringDefault;
618
+ return $this;
619
+ }
620
+ /** @codeCoverageIgnoreEnd */
621
+
622
+ /**
623
+ * @param bool $booleanDefault
624
+ * @return $this
625
+ * @codeCoverageIgnoreStart
626
+ */
627
+ public function setBooleanDefault($booleanDefault)
628
+ {
629
+ $this->booleanDefault = $booleanDefault;
630
+ return $this;
631
+ }
632
+ /** @codeCoverageIgnoreEnd */
633
+
634
+ /**
635
+ * @param int $integerDefault
636
+ * @return $this
637
+ * @codeCoverageIgnoreStart
638
+ */
639
+ public function setIntegerDefault($integerDefault)
640
+ {
641
+ $this->integerDefault = $integerDefault;
642
+ return $this;
643
+ }
644
+ /** @codeCoverageIgnoreEnd */
645
+
646
+ /**
647
+ * @param array $arrayDefault
648
+ * @return $this
649
+ * @codeCoverageIgnoreStart
650
+ */
651
+ public function setArrayDefault($arrayDefault)
652
+ {
653
+ $this->arrayDefault = $arrayDefault;
654
+ return $this;
655
+ }
656
+ /** @codeCoverageIgnoreEnd */
657
+
658
+ /**
659
+ * @param string $noDefault
660
+ * @return $this
661
+ * @codeCoverageIgnoreStart
662
+ */
663
+ public function setNoDefault($noDefault)
664
+ {
665
+ $this->noDefault = $noDefault;
666
+ return $this;
667
+ }
668
+ /** @codeCoverageIgnoreEnd */
669
+ }
670
+
671
+
672
+ PHP;
673
+
674
+ $ this ->assertSame ($ expected , $ result );
675
+ }
676
+
677
+ public function testClassPropertyDefaultsDeactivated ()
678
+ {
679
+ $ schemaData = json_decode (<<<'JSON'
680
+ {
681
+ "properties": {
682
+ "stringDefault": {
683
+ "type": "string",
684
+ "default": "list"
685
+ },
686
+ "booleanDefault": {
687
+ "type": "boolean",
688
+ "default": false
689
+ },
690
+ "integerDefault": {
691
+ "type": "integer",
692
+ "default": 1
693
+ },
694
+ "arrayDefault": {
695
+ "type": "array",
696
+ "default": []
697
+ },
698
+ "noDefault": {
699
+ "type": "string"
700
+ }
701
+ }
702
+ }
703
+ JSON
704
+ );
705
+
706
+ $ schema = Schema::import ($ schemaData );
707
+ $ builder = new PhpBuilder ();
708
+ $ builder ->buildSetters = true ;
709
+ $ class = $ builder ->getClass ($ schema , 'Root ' );
710
+
711
+ $ result = '' ;
712
+ foreach ($ builder ->getGeneratedClasses () as $ class ) {
713
+ $ result .= $ class ->class . "\n\n" ;
714
+ }
715
+
716
+ $ expected = <<<'PHP'
717
+ /** @var string */
718
+ public $stringDefault;
719
+
720
+ /** @var bool */
721
+ public $booleanDefault;
722
+
723
+ /** @var int */
724
+ public $integerDefault;
725
+
726
+ /** @var array */
727
+ public $arrayDefault;
728
+
729
+ /** @var string */
730
+ public $noDefault;
731
+ PHP;
732
+
733
+ $ this ->assertContains ($ expected , $ result );
734
+ }
735
+
532
736
}
0 commit comments