Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
34 changes: 34 additions & 0 deletions Tuiter/src/Models/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tuiter\Models;

class Comment {
protected $content;
protected $idOwner;
protected $idPost;
protected $idComment;
protected $time;
public function __construct($time,$content,$idPost,$idOwner,$idComment){
$this->content=$content;
$this->idOwner=$idOwner;
$this->time=$time;
$this->idPost=$idPost;
$this->idComment=$idComment;
}
public function getContent(){
return $this->content;
}
public function getIdOwner(){
return $this->idOwner;
}
public function getIdPost(){
return $this->idPost;
}
public function getIdComment(){
return $this->idComment;
}
public function getTime(){
return $this->time;
}

}
34 changes: 34 additions & 0 deletions Tuiter/src/Models/CommentNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tuiter\Models;

class CommentNull extends Comment{
protected $content;
protected $idOwner;
protected $idPost;
protected $idComment;
protected $time;
public function __construct($time,$content,$idPost,$idOwner,$idComment){
$this->content="";
$this->idOwner=0;
$this->time=0;
$this->idPost=0;
$this->idComment=0;
}
public function getContent(){
return $this->content;
}
public function getIdOwner(){
return $this->idOwner;
}
public function getIdPost(){
return $this->idPost;
}
public function getIdComment(){
return $this->idComment;
}
public function getTime(){
return $this->time;
}

}
67 changes: 67 additions & 0 deletions Tuiter/src/Services/CommentService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Tuiter\Services;
use \Tuiter\Models\User;
use \Tuiter\Models\Post;
use \Tuiter\Models\Comment;
use \Tuiter\Models\CommentNull;
class CommentService {

private $collection;

public function __construct($collection){
$this->collection = $collection;
}
public function create(string $content, User $user, Post $post){
$comment=new Comment(time(),$content,$post->getPostId(),$user->getUserId(),md5(microtime()));
$result=$this->collection->insertOne(
array(
"content"=>$comment->getContent(),
"idOwner"=>$comment->getIdOwner(),
"idPost"=>$comment->getIdPost(),
"idComment"=>$comment->getIdComment(),
"time"=>$comment->getTime()
)
);
if($result->getInsertedCount()!=1){
return new CommentNull(time(),$content,$post->getPostId(),$user->getUserId(),md5(microtime()));
}
return $comment;
}
public function getAllCommentsFromPost(Post $post){
$comments=array();
$result=$this->collection->find(
array(
'idPost'=>$post->getPostId()
)
);
foreach($result as $info){
$newComment=new Comment(
$info["time"],
$info["content"],
$info["idPost"],
$info["idOwner"],
$info["idComment"]
);
$comments[]=$newComment;
}
return $comments;
}
public function getRandomComment(Post $post){
$comments=array();
$result=$this->collection->find(
array(
'idPost'=>$post->getPostId()
)
);
$newComment=new Comment(
$result["time"],
$result["content"],
$result["idPost"],
$result["idOwner"],
$result["idComment"]
);
return $newComment;
}

}
58 changes: 58 additions & 0 deletions Tuiter/tests/Services/CommentServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace TestTuiter\Services;
use Tuiter\Services\CommentService;
use \Tuiter\Services\UserService;
use \Tuiter\Services\FollowService;
use \Tuiter\Services\LoginService;
use \Tuiter\Services\PostService;
use \Tuiter\Models\User;
use \Tuiter\Models\Post;
final class CommentServiceTest extends \PHPUnit\Framework\TestCase {
protected function setUp(): void{
$connection = new \MongoDB\Client("mongodb://localhost");
$collectionComment = $connection->CommentServiceTest->CommentTest;
$collectionUserService = $connection->CommentServiceTest->UserTest;
$collectionPostService = $connection->CommentServiceTest->PostTest;
$collectionPostService->drop();
$collectionComment->drop();
$collectionUserService->drop();
$this->commentService=new CommentService($collectionComment);
$this->userService = new UserService($collectionUserService);
$this->postService= new PostService($collectionPostService);
}
public function testClassExists(){
$this->assertTrue(class_exists("\Tuiter\Services\CommentService"));
}
public function testComment(){
$eliel = new User("eliel", "Heber", "1234");
$edu = new User("edu", "Edward", "1234");
$this->userService->register("eliel", "Heber", "123456");
$this->userService->register("edu", "Edward", "123456");
$post=$this->postService->create('hello man',$edu);
$this->assertTrue($post instanceof Post);
$pudo=$this->commentService->create("boludito",$eliel,$post);
$this->assertEquals($pudo->getContent(),"boludito");
}
public function testGetCommentsFromPost(){
$eliel = new User("eliel", "Heber", "1234");
$edu = new User("edu", "Edward", "1234");
$this->userService->register("eliel", "Heber", "123456");
$this->userService->register("edu", "Edward", "123456");
$post=$this->postService->create('hello man',$edu);
$this->assertTrue($post instanceof Post);
$arrayDeComments=array();
$comment1=$this->commentService->create("boludito",$eliel,$post);
$arrayDeComments[]=$comment1;
$comment2=$this->commentService->create("pelotudito",$eliel,$post);
$arrayDeComments[]=$comment2;
$comment3=$this->commentService->create("marico",$edu,$post);
$arrayDeComments[]=$comment3;
$comment4=$this->commentService->create("mamahuevo",$eliel,$post);
$arrayDeComments[]=$comment4;
$comment5=$this->commentService->create("ojete",$eliel,$post);
$arrayDeComments[]=$comment5;
$this->assertEquals($this->commentService->getAllCommentsFromPost($post), $arrayDeComments);

}
}