public function testCat() { $cat = new Cat("Thelma"); $this->assertEquals("Thelma", $cat->getName()); $this->assertEquals("Thelma the Cat", (string) $cat); $cat->setName("Alice"); $this->assertEquals("Alice", $cat->getName()); $this->assertEquals("Alice the Cat", (string) $cat); }
} class Cat implements Animal { protected $name; public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getColour() { } } class Dog { } class Vet { public function treat(Animal $animal) { echo 'The vet has treated ' . $animal->getName(); } } $cat = new Cat(); $cat->setName('Felix'); //echo $cat->getName(); $vet = new Vet(); $vet->treat($cat);
public function testMultipleNamesAndAverageNameLength() { @($cat = new Cat()); $names = ['Chaos', 'Chocolate', 'Felix', 'Garfield', 'Nermal', 'Scratchy']; $totalLength = 0; foreach ($names as $name) { $cat->setName($name); $totalLength += strlen($name); } $allNames = $cat->getNames(); $this->assertEquals(implode(',', $names), $allNames); $this->assertEquals($totalLength / count($names), $cat->getAverageNameLength()); }
<?php include 'Cat.php'; $cat1 = new Cat(); $cat1->setColor("black"); $cat1->setName("Zoe"); $cat1->setBreed("Siamese "); $myCats[] = $cat1; $cat2 = new Cat(); $cat2->setColor("orange"); $cat2->setName("Garfield"); $cat2->setBreed("Tabby "); $myCats[] = $cat2; $cat3 = new Cat(); $cat3->setColor("tabby"); $cat3->setName("Fluffy"); $cat3->setBreed("catType3 "); $myCats[] = $cat3; $count = 0; catCount($myCats, $count); echo "the count for is " . $count . "<br>"; for ($i = 0; $i < 3; $i++) { displayCatInfo($myCats[$i]); } function catCount($cats, &$num) { for ($i = 0; $i < 2; $i++) { if ($cats[$i]->getColor() == "orange") { $num++; } }