-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c076e9b
commit f67c28c
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
tests/python/tests/instance_cache/php/polymorphic_classes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
interface Simple { | ||
} | ||
|
||
interface Complex extends Simple { | ||
} | ||
|
||
/** @kphp-immutable-class */ | ||
class SimpleType implements Simple { | ||
public string $name; | ||
function __construct() { | ||
$this->name = "simple name"; | ||
} | ||
} | ||
|
||
/** @kphp-immutable-class */ | ||
class ComplexType implements Complex { | ||
public string $name; | ||
function __construct() { | ||
$this->name = "complex name"; | ||
} | ||
} | ||
|
||
/** @kphp-immutable-class */ | ||
class CompletelyComplexType extends ComplexType { | ||
public string $position; | ||
function __construct() { | ||
parent::__construct(); | ||
$this->position = "position"; | ||
} | ||
} | ||
|
||
function save_as_base(string $key, Simple $obj) { | ||
$flag = instance_cache_store($key, $obj); | ||
if ($flag) { | ||
fprintf(STDERR, "successfully store $key\n"); | ||
} | ||
} | ||
|
||
function test_polymorphic_store() { | ||
$simple = new SimpleType(); | ||
echo "simple name - " .$simple->name ."\n"; | ||
save_as_base("SimpleType", $simple); | ||
save_as_base("ComplexType", new ComplexType()); | ||
save_as_base("CompletelyComplexType", new CompletelyComplexType()); | ||
} | ||
|
||
function test_polymorphic_fetch_and_modify() { | ||
$simple = instance_cache_fetch(Simple::class, "SimpleType"); | ||
$complex = instance_cache_fetch(Simple::class, "ComplexType"); | ||
$completelyComplex = instance_cache_fetch(Simple::class, "CompletelyComplexType"); | ||
if ($simple instanceof SimpleType) { | ||
fprintf(STDERR, "successfully fetch SimpleType\n"); | ||
echo $simple->name; | ||
if (strcmp($simple->name, "simple name") !== 0) { | ||
critical_error("expected 'simple name' but got $simple->name\n"); | ||
} | ||
$copyName = $simple->$name; | ||
$copyName .= "used name"; | ||
} | ||
if ($complex instanceof ComplexType) { | ||
fprintf(STDERR, "successfully fetch ComplexType\n"); | ||
if (strcmp($complex->name, "complex name") !== 0) { | ||
critical_error("expected 'complex name' but got $complex->name\n"); | ||
} | ||
$copyName = $complex->$name; | ||
$copyName .= "used name"; | ||
} | ||
if ($completelyComplex instanceof CompletelyComplexType) { | ||
fprintf(STDERR, "successfully fetch CompletelyComplexType\n"); | ||
if (strcmp($completelyComplex->position, "position") !== 0) { | ||
critical_error("expected 'position' but got $completelyComplex->position\n"); | ||
} | ||
$copyPosition = $completelyComplex->$position; | ||
$copyPosition .= "used position"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from python.lib.testcase import KphpServerAutoTestCase | ||
|
||
|
||
class TestPolymorphic(KphpServerAutoTestCase): | ||
|
||
def test_store_fetch_modify(self): | ||
resp = self.kphp_server.http_post( | ||
uri='/polymorphic_store' | ||
) | ||
self.assertEqual(resp.status_code, 200) | ||
self.kphp_server.assert_log(["successfully store SimpleType", "successfully store ComplexType", "successfully store CompletelyComplexType"]) | ||
for i in range(10): | ||
resp = self.kphp_server.http_post( | ||
uri='/polymorphic_store_and_verify' | ||
) | ||
self.assertEqual(resp.status_code, 200) | ||
self.kphp_server.assert_log(["successfully fetch SimpleType", "successfully fetch ComplexType", "successfully fetch CompletelyComplexType"]) |