Skip to content

Commit 9a518c4

Browse files
committed
PHPUnit: Updated and fixed all unit tests
1 parent a619552 commit 9a518c4

File tree

7 files changed

+155
-80
lines changed

7 files changed

+155
-80
lines changed

Action/SmartCodeAction.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function register(SubjectInterface $subject, SmartCodeInterface $smartCod
3434
}
3535

3636
$subject->addSmartCode($smartCode);
37+
$smartCode->incrementUsed();
3738

3839
$this->manager->persist($subject);
3940
$this->manager->flush();
@@ -50,6 +51,7 @@ public function register(SubjectInterface $subject, SmartCodeInterface $smartCod
5051
public function unregister(SubjectInterface $subject, SmartCodeInterface $smartCode)
5152
{
5253
$subject->removeSmartCode($smartCode);
54+
$smartCode->setUsed($smartCode->getUsed()-1);
5355

5456
$this->manager->persist($subject);
5557
$this->manager->flush();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Intracto\SmartCodeBundle\Tests\Generator;
4+
5+
use Intracto\SmartCodeBundle\Action\SmartCodeAction;
6+
use Intracto\SmartCodeBundle\Entity\SmartCode;
7+
use Intracto\SmartCodeBundle\Generator\SmartCodeGenerator;
8+
use Intracto\SmartCodeBundle\Generator\SmartCodeOptions;
9+
use Intracto\SmartCodeBundle\Tests\BaseTest;
10+
11+
class SmartCodeActionTest extends BaseTest
12+
{
13+
private $action;
14+
private $generator;
15+
private $subject;
16+
private $code;
17+
18+
/**
19+
* @return null
20+
*/
21+
public function setUp()
22+
{
23+
parent::setUp();
24+
$this->action = new SmartCodeAction($this->entityManager);
25+
$this->generator = new SmartCodeGenerator($this->entityManager);
26+
$this->subject = $this->getMock('Intracto\SmartCodeBundle\Entity\SubjectInterface');
27+
28+
$payload = $this->getMock('Intracto\SmartCodeBundle\Entity\PayloadInterface');
29+
$options = new SmartCodeOptions();
30+
$options->setAmount(1);
31+
$codes = $this->generator->generate($payload, $options);
32+
$this->code = $codes[0];
33+
}
34+
35+
public function testGenerator()
36+
{
37+
$this->assertInstanceOf('Intracto\SmartCodeBundle\Action\SmartCodeActionInterface', $this->action);
38+
}
39+
40+
public function testRegister()
41+
{
42+
$this->assertTrue($this->code->isValid());
43+
44+
$this->action->register($this->subject, $this->code);
45+
46+
$this->assertFalse($this->code->isValid());
47+
}
48+
49+
public function testUnregister()
50+
{
51+
$this->action->register($this->subject, $this->code);
52+
53+
$this->assertFalse($this->code->isValid());
54+
55+
$this->action->unregister($this->subject, $this->code);
56+
57+
$this->assertTrue($this->code->isValid());
58+
}
59+
}

Tests/BaseTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Intracto\SmartCodeBundle\Tests;
4+
5+
/**
6+
* Test case class helpful with Entity tests requiring the database interaction.
7+
* For regular entity tests it's better to extend standard \PHPUnit_Framework_TestCase instead.
8+
*/
9+
abstract class BaseTest extends \PHPUnit_Framework_TestCase
10+
{
11+
protected $entityManager;
12+
13+
/**
14+
* @return null
15+
*/
16+
public function setUp()
17+
{
18+
$this->entityManager = $this->createLoadedMockedDoctrineRepository('AppBundle', 'SmartCodeBundle:SmartCode', 'findOneBy', null);
19+
parent::setUp();
20+
}
21+
22+
protected function createLoadedMockedDoctrineRepository($repository, $repositoryName,$repositoryMethod,$repositoryMethodReturnVal) {
23+
$mockEM=$this->getMock('\Doctrine\ORM\EntityManager',
24+
array('getRepository', 'getClassMetadata', 'persist', 'flush'), array(), '', false);
25+
$mockSVRepo=$this->getMock($repository,array($repositoryMethod),array(),'',false);
26+
27+
$mockEM->expects($this->any())
28+
->method('getClassMetadata')
29+
->will($this->returnValue((object)array('name' => 'aClass')));
30+
$mockEM->expects($this->any())
31+
->method('persist')
32+
->will($this->returnValue(null));
33+
$mockEM->expects($this->any())
34+
->method('flush')
35+
->will($this->returnValue(null));
36+
37+
$mockSVRepo
38+
->expects($this->any())
39+
->method($repositoryMethod)
40+
->will($this->returnValue($repositoryMethodReturnVal));
41+
42+
$mockEM->expects($this->any())
43+
->method('getRepository')
44+
->with($repositoryName)
45+
->will($this->returnValue($mockSVRepo));
46+
47+
return $mockEM;
48+
}
49+
}

Tests/Generator/SmartCodeGeneratorTest.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,45 @@
22

33
namespace Intracto\SmartCodeBundle\Tests\Generator;
44

5-
use Intracto\SmartCodeBundle\Tests\KernelAwareTest;
5+
use Intracto\SmartCodeBundle\Generator\SmartCodeGenerator;
6+
use Intracto\SmartCodeBundle\Generator\SmartCodeOptions;
7+
use Intracto\SmartCodeBundle\Tests\BaseTest;
68

7-
class SmartCodeGeneratorTest extends KernelAwareTest
9+
class SmartCodeGeneratorTest extends BaseTest
810
{
11+
private $generator;
12+
13+
/**
14+
* @return null
15+
*/
16+
public function setUp()
17+
{
18+
parent::setUp();
19+
$this->generator = new SmartCodeGenerator($this->entityManager);
20+
}
21+
22+
public function testGenerator()
23+
{
24+
$this->assertInstanceOf('Intracto\SmartCodeBundle\Generator\SmartCodeGeneratorInterface', $this->generator);
25+
}
26+
927
public function testGenerateUniqueCode()
1028
{
11-
$generator = $this->container->get('smartcode.generator.payload_smartcode');
12-
$code = $generator->generateUniqueCode();
29+
$code = $this->generator->generateUniqueCode();
1330

1431
$this->assertRegExp('/^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}/', $code);
1532
}
33+
34+
public function testGenerate()
35+
{
36+
$payload = $this->getMock('Intracto\SmartCodeBundle\Entity\PayloadInterface');
37+
38+
$options = new SmartCodeOptions();
39+
$options->setAmount(500);
40+
41+
$codes = $this->generator->generate($payload, $options);
42+
43+
$this->assertCount(500, $codes);
44+
$this->assertInstanceOf('Intracto\SmartCodeBundle\Entity\SmartCodeInterface', $codes[0]);
45+
}
1646
}

Tests/KernelAwareTest.php

Lines changed: 0 additions & 75 deletions
This file was deleted.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
"psr-0": { "Intracto\\SmartCodeBundle": "" }
3131
},
3232

33-
"target-dir": "Intracto/SmartCodeBundle"
33+
"target-dir": "Intracto/SmartCodeBundle"
3434
}

phpunit.xml.dist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<phpunit bootstrap="./vendor/autoload.php">
3+
4+
<testsuites>
5+
<testsuite name="SmartCodeTestSuite">
6+
<directory>./Tests</directory>
7+
</testsuite>
8+
</testsuites>
9+
10+
</phpunit>

0 commit comments

Comments
 (0)