[PHP] Use parent constructor when inheriting

This commit is contained in:
Arne Jørgensen
2016-03-23 10:59:32 +01:00
parent 5d5a6e049c
commit 0196cdd558
3 changed files with 64 additions and 0 deletions

View File

@@ -114,6 +114,7 @@ class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayA
*/
public function __construct(array $data = null)
{
{{#parent}}parent::__construct($data);{{/parent}}
if ($data != null) {
{{#vars}}$this->{{name}} = $data["{{name}}"];{{#hasMore}}
{{/hasMore}}{{/vars}}

View File

@@ -1339,6 +1339,42 @@
"xml": {
"name": "Name"
}
},
"Dog" : {
"allOf" : [ {
"$ref" : "#/definitions/Animal"
}, {
"type" : "object",
"properties" : {
"breed" : {
"type" : "string"
}
}
} ]
},
"Cat" : {
"allOf" : [ {
"$ref" : "#/definitions/Animal"
}, {
"type" : "object",
"properties" : {
"declawed" : {
"type" : "boolean"
}
}
} ]
},
"Animal" : {
"type" : "object",
"discriminator": "className",
"required": [
"className"
],
"properties" : {
"className" : {
"type" : "string"
}
}
}
}
}

View File

@@ -375,6 +375,33 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
}
// test inheritance in the model
public function testInheritance()
{
$new_dog = new Swagger\Client\Model\Dog;
// the object should be an instance of the derived class
$this->assertInstanceOf('Swagger\Client\Model\Dog', $new_dog);
// the object should also be an instance of the parent class
$this->assertInstanceOf('Swagger\Client\Model\Animal', $new_dog);
}
// test inheritance constructor is working with data
// initialization
public function testInheritanceConstructorDataInitialization()
{
// initialize the object with data in the constructor
$data = array(
'class_name' => 'Dog',
'breed' => 'Great Dane'
);
$new_dog = new Swagger\Client\Model\Dog($data);
// the property on the derived class should be set
$this->assertSame('Great Dane', $new_dog->getBreed());
// the property on the parent class should be set
$this->assertSame('Dog', $new_dog->getClassName());
}
}
?>