Skip to content

Commit

Permalink
Merge pull request #120 from Soulou/Record_Testing
Browse files Browse the repository at this point in the history
User model testing
  • Loading branch information
Soulou committed Jun 3, 2012
2 parents bf78f39 + 1ce7930 commit ac60674
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Unsapa/IPWBundle/Entity/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function upload()
return;
}
$this->file->move($this->getDocumentUploadRootDir(), $this->document);
// Functionnal test on uploadAciton is necessary to test this last instruction
unset($this->file);
}

Expand Down Expand Up @@ -242,7 +243,7 @@ public function getDocumentAbsolutePath()
**/
public function getDocumentWebPath()
{
return $this->getUploadDir().'/'.$this->getDocument();
return $this->getDocumentUploadDir().'/'.$this->getDocument();
}

/**
Expand Down
195 changes: 195 additions & 0 deletions src/Unsapa/IPWBundle/Tests/Entity/RecordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php
/**
* Testing the Record Entity
* @package Unsapa\IPWBundle\Tests\Entity
*/
namespace Unsapa\IPWBundle\Tests\Entity;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\Exception\FileException;

use Unsapa\IPWBundle\Tests\UnsapaTest;

use Unsapa\IPWBundle\Entity\Exam;
use Unsapa\IPWBundle\Entity\Promo;
use Unsapa\IPWBundle\Entity\User;
use Unsapa\IPWBundle\Entity\Record;

/**
* Tests the entity Recprd
* @see Unsapa\IPWBundle\Entity\Record
*/
class RecordTest extends UnsapaTest
{
/**
* @var Record $record for generic tests
*/
private $record;
/**
* @var User student
*/
private $student;
/**
* @var Exam $exam
*/
private $exam;
/**
* @var Promo $promo
*/
private $promo;
/**
* @var User $td
*/
private $td;

/**
* Setup
*/
public function setUp()
{
parent::setUp();
$this->td = $this->createRespTD();
$this->promo = $this->createPromo();
$this->exam = $this->createExam($this->promo, $this->td);
$this->student = $this->createStudent();
$this->record = $this->createRecord(array(
'student' => $this->student,
'exam' => $this->exam
));
}

/**
* Test that we can't save a record without Student
* @expectedException InvalidArgumentException
* @expectedExceptionMessage This value should not be null.
*/
public function testEmptyStudent()
{
$record = $this->createRecord(array(
'exam' => $this->exam
));
}

/**
* Test that we can't save a record with an empty Exam
* @expectedException InvalidArgumentException
* @expectedExceptionMessage This value should not be null.
*/
public function testEmptyNamePromo()
{
$record = $this->createRecord(array(
'student' => $this->student,
));
}

/**
* Test Record toString
*/
public function testToString()
{
$this->assertEquals('{"exam":'.$this->exam->getId().
',"student":'.$this->student->getId().',"mark":null,"document":false}',
$this->record->__toString());
}

/**
* Test Getter and Setter
*/
public function testName()
{
$this->assertEquals($this->student, $this->record->getStudent());
$this->assertEquals($this->exam, $this->record->getExam());
$this->assertEquals(NULL, $this->record->getDocument());
$this->assertEquals(NULL, $this->record->getMark());

$exam = $this->createExam($this->promo, $this->td, NULL, "Test2");
$this->record->setExam($exam);
$this->assertEquals($exam, $this->record->getExam());

$st = $this->createStudent(array('username'=>"Username2", 'email'=>'[email protected]'));
$this->record->setStudent($st);
$this->assertEquals($st, $this->record->getStudent());

$this->record->setMark(10);
$this->assertEquals(10, $this->record->getMark());

$this->record->setDocument("./a.pdf");
$this->assertEquals("./a.pdf", $this->record->getDocument());
}

/**
* Test two Record with the same couple student/exam
* @expectedException PDOException
*/
public function testDoubleRecord()
{
$record = $this->createRecord(array(
'student' => $this->student,
'exam' => $this->exam
));
$record = $this->createRecord(array(
'student' => $this->student,
'exam' => $this->exam
));
}

/**
* Test file attribute manipulation
*/
public function testFileAttr()
{
$this->record->setDocument(NULL);
$this->assertEquals(NULL, $this->record->getDocument());

$this->record->preUpload();
$this->assertEquals(NULL, $this->record->getDocument());

$file = new UploadedFile(__DIR__."/../../../../../data/sampleDocument.pdf", "sampleDocument.pdf");
$this->record->setFile($file);
$this->assertEquals($file, $this->record->getFile());

$this->record->preUpload();
$this->assertRegExp("/[a-z0-9]{40}\.pdf/", $this->record->getDocument());

$sha = $this->record->getDocument();

$this->assertEquals("uploads/records/$sha", $this->record->getDocumentWebPath());
$this->assertRegExp("+.*/web/uploads/records/$sha+", $this->record->getDocumentAbsolutePath());
$this->assertEquals("$sha", $this->record->getDocumentName());


}

/**
* Test the upload function
* @expectedException Symfony\Component\HttpFoundation\File\Exception\FileException
*/
public function testUpload()
{
$file = new UploadedFile(__DIR__."/../../../../../data/sampleDocument.pdf", "sampleDocument.pdf");
$this->record->setFile($file);
$this->record->upload();
}

/**
* Test the upload function when there is no document
*/
public function testEmptyUpload()
{
$this->record->setFile(NULL);

$this->assertEquals(NULL, $this->record->getDocumentName());
$this->record->upload();
}

/**
* Test when a record is deleted
*/
public function testRemoveUpload()
{
$file = new UploadedFile(__DIR__."/../../../../../data/sampleDocument.pdf", "sampleDocument.pdf");
$this->record->setFile($file);

$this->record->removeUpload();
}
}
6 changes: 3 additions & 3 deletions src/Unsapa/IPWBundle/Tests/UnsapaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ protected function createStudent($params = array('username' => "StudentTest", 'e
* @param User $resp
* @return Exam We want a new instance in each test.
*/
protected function createExam($promo, $resp, $date = NULL)
protected function createExam($promo, $resp, $date = NULL, $title = "ExamTest")
{
if($date == NULL)
$date = new \Datetime('now');
$date = (new \Datetime('now'))->add((new \DateInterval("P1D")));
$exam = new Exam(array(
'title' => "ExamTest",
'title' => $title,
'promo' => $promo,
'exam_date' => $date,
'exam_desc' => "ExamDesc",
Expand Down

0 comments on commit ac60674

Please sign in to comment.