diff --git a/Action/SmartCodeAction.php b/Action/SmartCodeAction.php index e410597..c98bddc 100644 --- a/Action/SmartCodeAction.php +++ b/Action/SmartCodeAction.php @@ -34,6 +34,7 @@ public function register(SubjectInterface $subject, SmartCodeInterface $smartCod } $subject->addSmartCode($smartCode); + $smartCode->incrementUsed(); $this->manager->persist($subject); $this->manager->flush(); @@ -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(); diff --git a/Tests/Action/SmartCodeActionTest.php b/Tests/Action/SmartCodeActionTest.php new file mode 100644 index 0000000..ae7defa --- /dev/null +++ b/Tests/Action/SmartCodeActionTest.php @@ -0,0 +1,59 @@ +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()); + } +} diff --git a/Tests/BaseTest.php b/Tests/BaseTest.php new file mode 100644 index 0000000..f3c7b02 --- /dev/null +++ b/Tests/BaseTest.php @@ -0,0 +1,49 @@ +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; + } +} diff --git a/Tests/Generator/SmartCodeGeneratorTest.php b/Tests/Generator/SmartCodeGeneratorTest.php index 89d0c02..c799d1d 100644 --- a/Tests/Generator/SmartCodeGeneratorTest.php +++ b/Tests/Generator/SmartCodeGeneratorTest.php @@ -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]); + } } diff --git a/Tests/KernelAwareTest.php b/Tests/KernelAwareTest.php deleted file mode 100644 index acb06f5..0000000 --- a/Tests/KernelAwareTest.php +++ /dev/null @@ -1,75 +0,0 @@ -kernel = new \AppKernel('dev', true); - $this->kernel->boot(); - - $this->container = $this->kernel->getContainer(); - $this->entityManager = $this->container->get('doctrine')->getManager(); - - $this->generateSchema(); - - parent::setUp(); - } - - /** - * @return null - */ - public function tearDown() - { - $this->kernel->shutdown(); - - parent::tearDown(); - } - - /** - * @return null - */ - protected function generateSchema() - { - $metadatas = $this->getMetadatas(); - - if (!empty($metadatas)) { - $tool = new \Doctrine\ORM\Tools\SchemaTool($this->entityManager); - $tool->dropSchema($metadatas); - $tool->createSchema($metadatas); - } - } - - /** - * @return array - */ - protected function getMetadatas() - { - return $this->entityManager->getMetadataFactory()->getAllMetadata(); - } -} diff --git a/composer.json b/composer.json index 7015063..cc5c3e1 100644 --- a/composer.json +++ b/composer.json @@ -30,5 +30,5 @@ "psr-0": { "Intracto\\SmartCodeBundle": "" } }, - "target-dir": "Intracto/SmartCodeBundle" + "target-dir": "Intracto/SmartCodeBundle" } \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..8d0ee3a --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,10 @@ + + + + + + ./Tests + + + + \ No newline at end of file