<u>Clase User: </u>
<?php
namespace Repte;
class User
{
protected $name;
protected $lastname;
public function setName($name,$lastname)
{
$this->name = $name;
$this->lastname = $lastname;
}
public function getFullName()
{
return $this->name . ' ' .$this->lastname;
}
}
<u>Clase Author: </u>
<?php
namespace Repte;
class Author extends User
{
protected $posts = [];
public function setPost(Post $post)
{
$this->posts[] = $post;
}
public function getPosts()
{
return $this->posts;
}
public function countPosts()
{
return count($this->posts);
}
}
<u>Clase Category: </u>
<?php
namespace Repte;
class Category
{
protected $name;
public function setName(string $name)
{
$this->name = $name;
}
public function getCategoryName()
{
return $this->name;
}
}
<u>Clase Post :</u>
<?php
namespace Repte;
class Post
{
protected $title;
protected $content;
protected $category;
protected $author;
public function setTitle(string $title)
{
$this->title = $title;
}
public function setContent(string $content)
{
$this->content = $content;
}
public function setCategory(string $category)
{
$this->category = $category;
}
public function setAuthor($author)
{
$this->author = $author;
}
public function getPostTitle()
{
return $this->title;
}
public function getAuthorName()
{
return $this->author;
}
public function getCategory()
{
return $this->category;
}
public function getContent()
{
return $this->content;
}
}
Test :
<?php
use PHPUnit\Framework\TestCase;
use Repte\Author;
use Repte\Post;
use Repte\Category;
class PostTest extends TestCase
{
public function test_post()
{
// Creamos un autor
$authors[0] = new Author;
$authors[0]->setName("Xavier","Mateu");
// Creamos una categoría
$categories[0] = new Category;
$categories[0]->setName("PHP");
// Creamos un post
//$posts = new Post("Título", "Contenido", $categories);
$posts[0] = new Post;
$posts[0]->setTitle("Lo más sobre PHP");
$posts[0]->setContent("bla bla bla sobre PHP");
$posts[0]->setCategory($categories[0]->getCategoryName());
$posts[0]->setAuthor($authors[0]);
// Agregar post al autor
$authors[0]->setPost($posts[0]);
// Aserción o afirmación para comprobar que tenemos 1 post agregado
$this->assertEquals(1, $authors[0]->countPosts());
// Aserción o afirmación para comprobar que los post de un autor son una instancia de la clase Post
$this->assertInstanceOf(Post::class, $authors[0]->getPosts()[0]);
}
}
index :
<?php
include __DIR__ ."/vendor/autoload.php";
use Repte\Category;
use Repte\Author;
use Repte\Post;
//creamos 3 autores
$authors[0] = new Author;
$authors[0]->setName("Xavier","Mateu");
$authors[1] = new Author;
$authors[1]->setName("Manel","Martinez");
$authors[2] = new Author;
$authors[2]->setName("Carles","Andreu");
//creamos las 3 categorias
$categories[0] = new Category;
$categories[0]->setName("PHP");
$categories[1] = new Category;
$categories[1]->setName("VueJS");
$categories[2] = new Category;
$categories[2]->setName("JS");
//creamos los posts y los asociamos a los autores
$posts[0] = new Post;
$posts[0]->setTitle("Lo más sobre PHP");
$posts[0]->setContent("bla bla bla sobre PHP");
$posts[0]->setCategory($categories[0]->getCategoryName());
$posts[0]->setAuthor($authors[0]);
$posts[3] = new Post;
$posts[3]->setTitle("Algo más sobre PHP");
$posts[3]->setContent("na de na sobre PHP");
$posts[3]->setCategory($categories[0]->getCategoryName());
$posts[3]->setAuthor($authors[0]);
$posts[1] = new Post;
$posts[1]->setTitle("Lo más sobre Vue");
$posts[1]->setContent("bla bla bla sobre Vue");
$posts[1]->setCategory($categories[1]->getCategoryName());
$posts[1]->setAuthor($authors[1]);
$posts[2] = new Post;
$posts[2]->setTitle("Lo más sobre Javascript");
$posts[2]->setContent("bla bla bla sobre Javascript");
$posts[2]->setCategory($categories[2]->getCategoryName());
$posts[2]->setAuthor($authors[2]);
//asignamos los post a los autores
$authors[0]->setPost($posts[0]);
$authors[0]->setPost($posts[3]);
$authors[1]->setPost($posts[1]);
$authors[2]->setPost($posts[2]);
//Listamos todos los posts de cada autor con su título, categoria y contenido
foreach ($authors as $author)
{
echo "Autor: {$author->getFullName()}<br>";
foreach ($author->getPosts() as $post)
{
echo "Título articulo: {$post->getPostTitle()}<br>";
echo "Categoria {$post->getCategory()}<br>";
echo "Contenido: {$post->getContent()}<br>";
echo "<br><br><br>";
}
}