-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
125 lines (104 loc) · 2.39 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
include_once 'Shelter.php';
function testCat() {
echo "Testing Cat class... <br>";
$cat = new Cat('Murka', 2);
$age = $cat->getAge();
$name = $cat->getName();
echo "Test getAge: ";
if($age != 2) {
echo "getAge function returned ".$age.", expected 2. <br>";
return;
}
echo "OK<br>";
echo "Test getName: ";
if($name != 'Murka') {
echo "getName function returned ".$name.", expected Murka. <br>";
return;
}
echo "OK <br>";
}
function testDog() {
}
function testTurtle() {
}
function testShelter() {
echo "Testing Shelter class...<br>";
$shelter = new Shelter();
$sharik = new Dog('Sharik', 3);
$tuzik = new Dog('Tuzik', 5);
$murka = new Cat('Murka', 4);
$mike = new Turtle('Mike', 15);
$shelter->addPet($tuzik);
$shelter->addPet($sharik);
$shelter->addPet($murka);
$shelter->addPet($mike);
echo "Test petsByType: ";
$dogs = $shelter->petsByType(Dog::class);
if(count($dogs) != 2) {
echo "Function returned number of objects: ".count($dogs)." but expected 2 <br>";
return;
}
if($dogs[0] != $sharik) {
echo "Function must return objects sorted by its name";
return;
}
if($dogs[1] != $tuzik) {
echo "Function must return only Dog type objects";
return;
}
$cats = $shelter->petsByType(Cat::class);
if(count($cats) != 1) {
echo "Function returned number of objects: ".count($cats)." but expected 1 <br>";
return;
}
if($cats[0] != $murka) {
echo "Function must return only Cat type objects";
return;
}
echo "OK <br>";
echo "Test incompatible type: ";
try {
$shelter->addPet("Some object");
echo "ERROR, adding not pet <br>";
return;
}
catch(InvalidArgumentException $e) {
echo "OK <br>";
}
echo "Test giving pet by type: ";
try {
$dog = $shelter->givePetByType(Dog::class);
if(!($dog instanceof Dog)) {
echo "expected Dog, but found ".gettype($dog). "<br>";
return;
}
if($dog != $tuzik) {
echo "function must return the pet which most stay in the shelter <br>";
return;
}
} catch(Exception $e) {
echo "Unexpected error thrown <br>";
return;
}
echo "OK <br>";
echo "Test giving pet without type: ";
try {
$dog = $shelter->givePet();
if($dog != $sharik) {
echo "function must return the pet which most stay in the shelter <br>";
return;
}
}
catch(Exception $e) {
echo "Unexpected error thrown";
return;
}
echo "OK <br>";
}
//run tests
testCat();
testDog();
testTurtle();
testShelter();
echo "Test ended";