diff --git a/Tests/Visitor/VisitorTest.php b/Tests/Visitor/VisitorTest.php new file mode 100644 index 0000000..dfca346 --- /dev/null +++ b/Tests/Visitor/VisitorTest.php @@ -0,0 +1,51 @@ +visitor = new Visitor\RolePrintVisitor(); + } + + public function getRole() + { + return array( + array(new Visitor\User("Dominik"), 'Role: User Dominik'), + array(new Visitor\Group("Administrators"), 'Role: Group: Administrators') + ); + } + + /** + * @dataProvider getRole + */ + public function testVisitSomeRole(Visitor\Role $role, $expect) + { + $this->expectOutputString($expect); + $role->accept($this->visitor); + } + + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Mock + */ + public function testUnknownObject() + { + $mock = $this->getMockForAbstractClass('DesignPatterns\Visitor\Role'); + $mock->accept($this->visitor); + } + +} \ No newline at end of file diff --git a/Visitor/Group.php b/Visitor/Group.php new file mode 100644 index 0000000..972e95c --- /dev/null +++ b/Visitor/Group.php @@ -0,0 +1,32 @@ +name = (string) $name; + } + + /** + * @return string + */ + public function getName() + { + return "Group: " . $this->name; + } + +} diff --git a/Visitor/Role.php b/Visitor/Role.php new file mode 100644 index 0000000..baaefe9 --- /dev/null +++ b/Visitor/Role.php @@ -0,0 +1,38 @@ +getName(); + } + + public function visitUser(User $role) + { + echo "Role: " . $role->getName(); + } + +} diff --git a/Visitor/RoleVisitor.php b/Visitor/RoleVisitor.php new file mode 100644 index 0000000..0f40667 --- /dev/null +++ b/Visitor/RoleVisitor.php @@ -0,0 +1,26 @@ +name = (string) $name; + } + + /** + * @return string + */ + public function getName() + { + return "User " . $this->name; + } + +} + diff --git a/Visitor/Visitor.php b/Visitor/Visitor.php deleted file mode 100644 index 57ebe90..0000000 --- a/Visitor/Visitor.php +++ /dev/null @@ -1,97 +0,0 @@ -getName(); - } -} - -class User implements Role { - - /** - * @var string - */ - protected $name; - - /** - * @param string $name - */ - public function __construct($name) { - $this->name = (string) $name; - } - - /** - * @param RoleVisitor $visitor - * @return void - */ - public function accept(RoleVisitor $visitor) { - $visitor->visit($this); - } - - /** - * @return string - */ - public function getName() { - return "User " . $this->name; - } -} - -class Group implements Role { - - /** - * @var string - */ - protected $name; - - /** - * @param string $name - */ - public function __construct($name) { - $this->name = (string) $name; - } - - /** - * @param RoleVisitor $visitor - * @return void - */ - public function accept(RoleVisitor $visitor) { - $visitor->visit($this); - } - - /** - * @return string - */ - public function getName() { - return "Group: " . $this->name; - } -} - -$user = new User("Dominik"); -$group = new Group("Administrators"); - -$printer = new RolePrintVisitor(); -$printer->visit($user); -$printer->visit($group); -