Skip to content

Commit

Permalink
add runtime tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astrophysik committed Nov 15, 2023
1 parent c076e9b commit f67c28c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/python/tests/instance_cache/php/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once('polymorphic_classes.php');

function main() {
switch ($_SERVER["PHP_SELF"]) {
case "/store": {
Expand All @@ -14,6 +16,14 @@ function main() {
test_delete();
return;
}
case "/polymorphic_store": {
test_polymorphic_store();
return;
}
case "/polymorphic_store_and_verify": {
test_polymorphic_fetch_and_modify();
return;
}
}

critical_error("unknown test " . $_SERVER["PHP_SELF"]);
Expand Down
79 changes: 79 additions & 0 deletions tests/python/tests/instance_cache/php/polymorphic_classes.php
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";
}

}
17 changes: 17 additions & 0 deletions tests/python/tests/instance_cache/test_polymorphic.py
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"])

0 comments on commit f67c28c

Please sign in to comment.