Skip to content

Commit

Permalink
PHPUnit: Updated and fixed all unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-art committed May 12, 2015
1 parent a619552 commit 9a518c4
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 80 deletions.
2 changes: 2 additions & 0 deletions Action/SmartCodeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function register(SubjectInterface $subject, SmartCodeInterface $smartCod
}

$subject->addSmartCode($smartCode);
$smartCode->incrementUsed();

$this->manager->persist($subject);
$this->manager->flush();
Expand All @@ -50,6 +51,7 @@ public function register(SubjectInterface $subject, SmartCodeInterface $smartCod
public function unregister(SubjectInterface $subject, SmartCodeInterface $smartCode)
{
$subject->removeSmartCode($smartCode);
$smartCode->setUsed($smartCode->getUsed()-1);

$this->manager->persist($subject);
$this->manager->flush();
Expand Down
59 changes: 59 additions & 0 deletions Tests/Action/SmartCodeActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Intracto\SmartCodeBundle\Tests\Generator;

use Intracto\SmartCodeBundle\Action\SmartCodeAction;
use Intracto\SmartCodeBundle\Entity\SmartCode;
use Intracto\SmartCodeBundle\Generator\SmartCodeGenerator;
use Intracto\SmartCodeBundle\Generator\SmartCodeOptions;
use Intracto\SmartCodeBundle\Tests\BaseTest;

class SmartCodeActionTest extends BaseTest
{
private $action;
private $generator;
private $subject;
private $code;

/**
* @return null
*/
public function setUp()
{
parent::setUp();
$this->action = new SmartCodeAction($this->entityManager);
$this->generator = new SmartCodeGenerator($this->entityManager);
$this->subject = $this->getMock('Intracto\SmartCodeBundle\Entity\SubjectInterface');

$payload = $this->getMock('Intracto\SmartCodeBundle\Entity\PayloadInterface');
$options = new SmartCodeOptions();
$options->setAmount(1);
$codes = $this->generator->generate($payload, $options);
$this->code = $codes[0];
}

public function testGenerator()
{
$this->assertInstanceOf('Intracto\SmartCodeBundle\Action\SmartCodeActionInterface', $this->action);
}

public function testRegister()
{
$this->assertTrue($this->code->isValid());

$this->action->register($this->subject, $this->code);

$this->assertFalse($this->code->isValid());
}

public function testUnregister()
{
$this->action->register($this->subject, $this->code);

$this->assertFalse($this->code->isValid());

$this->action->unregister($this->subject, $this->code);

$this->assertTrue($this->code->isValid());
}
}
49 changes: 49 additions & 0 deletions Tests/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Intracto\SmartCodeBundle\Tests;

/**
* Test case class helpful with Entity tests requiring the database interaction.
* For regular entity tests it's better to extend standard \PHPUnit_Framework_TestCase instead.
*/
abstract class BaseTest extends \PHPUnit_Framework_TestCase
{
protected $entityManager;

/**
* @return null
*/
public function setUp()
{
$this->entityManager = $this->createLoadedMockedDoctrineRepository('AppBundle', 'SmartCodeBundle:SmartCode', 'findOneBy', null);
parent::setUp();
}

protected function createLoadedMockedDoctrineRepository($repository, $repositoryName,$repositoryMethod,$repositoryMethodReturnVal) {
$mockEM=$this->getMock('\Doctrine\ORM\EntityManager',
array('getRepository', 'getClassMetadata', 'persist', 'flush'), array(), '', false);
$mockSVRepo=$this->getMock($repository,array($repositoryMethod),array(),'',false);

$mockEM->expects($this->any())
->method('getClassMetadata')
->will($this->returnValue((object)array('name' => 'aClass')));
$mockEM->expects($this->any())
->method('persist')
->will($this->returnValue(null));
$mockEM->expects($this->any())
->method('flush')
->will($this->returnValue(null));

$mockSVRepo
->expects($this->any())
->method($repositoryMethod)
->will($this->returnValue($repositoryMethodReturnVal));

$mockEM->expects($this->any())
->method('getRepository')
->with($repositoryName)
->will($this->returnValue($mockSVRepo));

return $mockEM;
}
}
38 changes: 34 additions & 4 deletions Tests/Generator/SmartCodeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@

namespace Intracto\SmartCodeBundle\Tests\Generator;

use Intracto\SmartCodeBundle\Tests\KernelAwareTest;
use Intracto\SmartCodeBundle\Generator\SmartCodeGenerator;
use Intracto\SmartCodeBundle\Generator\SmartCodeOptions;
use Intracto\SmartCodeBundle\Tests\BaseTest;

class SmartCodeGeneratorTest extends KernelAwareTest
class SmartCodeGeneratorTest extends BaseTest
{
private $generator;

/**
* @return null
*/
public function setUp()
{
parent::setUp();
$this->generator = new SmartCodeGenerator($this->entityManager);
}

public function testGenerator()
{
$this->assertInstanceOf('Intracto\SmartCodeBundle\Generator\SmartCodeGeneratorInterface', $this->generator);
}

public function testGenerateUniqueCode()
{
$generator = $this->container->get('smartcode.generator.payload_smartcode');
$code = $generator->generateUniqueCode();
$code = $this->generator->generateUniqueCode();

$this->assertRegExp('/^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}/', $code);
}

public function testGenerate()
{
$payload = $this->getMock('Intracto\SmartCodeBundle\Entity\PayloadInterface');

$options = new SmartCodeOptions();
$options->setAmount(500);

$codes = $this->generator->generate($payload, $options);

$this->assertCount(500, $codes);
$this->assertInstanceOf('Intracto\SmartCodeBundle\Entity\SmartCodeInterface', $codes[0]);
}
}
75 changes: 0 additions & 75 deletions Tests/KernelAwareTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
"psr-0": { "Intracto\\SmartCodeBundle": "" }
},

"target-dir": "Intracto/SmartCodeBundle"
"target-dir": "Intracto/SmartCodeBundle"
}
10 changes: 10 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">

<testsuites>
<testsuite name="SmartCodeTestSuite">
<directory>./Tests</directory>
</testsuite>
</testsuites>

</phpunit>

0 comments on commit 9a518c4

Please sign in to comment.