Skip to content

Commit

Permalink
Update unit tests for dispatching models (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Ducoudray authored and cdaguerre committed May 9, 2016
1 parent a08a12e commit 3376ac2
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 12 deletions.
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
{ "name": "Pierre Ducoudray", "email": "[email protected]" }
],
"require": {
"php": ">=5.3.3",
"ext-curl": "*",
"guzzle/guzzle": "~3.7"
"php": ">=5.3.3",
"ext-curl": "*",
"guzzle/guzzle": "~3.7"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"fabpot/php-cs-fixer": "^1.11",
"pagerfanta/pagerfanta": "^1.0",
"symfony/http-foundation": "^2.7",
"sylius/resource": "@dev",
"doctrine/orm": "^2.3"
"phpunit/phpunit": "~4.0",
"fabpot/php-cs-fixer": "^1.11",
"pagerfanta/pagerfanta": "^1.0",
"symfony/http-foundation": "^2.7",
"sylius/resource": "@dev",
"doctrine/orm": "^2.3",
"gedmo/doctrine-extensions": "^2.4"
},
"suggest": {
"symfony/http-foundation": "To use the callback handler",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Textmaster\Translator\Adapter;

use Doctrine\Common\Persistence\ManagerRegistry;
use Gedmo\DoctrineExtensions\Translatable\TranslatableListener;
use Gedmo\Translatable\TranslatableListener;

class GedmoTranslatableAdapter extends AbstractDoctrineAdapter
{
Expand All @@ -26,7 +26,8 @@ class GedmoTranslatableAdapter extends AbstractDoctrineAdapter
/**
* Constructor.
*
* @param ManagerRegistry $registry
* @param ManagerRegistry $registry
* @param TranslatableListener $listener
*/
public function __construct(ManagerRegistry $registry, TranslatableListener $listener)
{
Expand Down
12 changes: 12 additions & 0 deletions test/Textmaster/Functional/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class ManagerTest extends \PHPUnit_Framework_TestCase
{
const TEST_PROJECT_ID = '57065757f41f44001100000e';
const TEST_DOCUMENT_ID = '57065d0ef41f44000e00008d';

/**
* @var Client
Expand Down Expand Up @@ -99,4 +100,15 @@ public function shouldShowDocumentsProject()
$this->assertSame(1, $pager->count());
$this->assertTrue(in_array('Textmaster\Model\DocumentInterface', class_implements($document), true));
}

/**
* @test
*/
public function shouldGetDocument()
{
$manager = new Manager($this->client);
$document = $manager->getDocument(self::TEST_PROJECT_ID, self::TEST_DOCUMENT_ID);

$this->assertSame('document_test_1', $document->getTitle());
}
}
45 changes: 44 additions & 1 deletion test/Textmaster/Tests/Model/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class ProjectTest extends \PHPUnit_Framework_TestCase
{
protected $clientMock;
protected $projectApiMock;

public function setUp()
{
Expand Down Expand Up @@ -47,7 +48,7 @@ public function setUp()
);

$clientMock = $this->getMock('Textmaster\Client', array('api'));
$projectApiMock = $this->getMock('Textmaster\Api\Project', array('show', 'update'), array($clientMock));
$projectApiMock = $this->getMock('Textmaster\Api\Project', array('show', 'update', 'launch'), array($clientMock));
$documentApiMock = $this->getMock('Textmaster\Api\FilterableApiInterface', array('filter', 'getClient'));

$clientMock->method('api')
Expand All @@ -63,6 +64,7 @@ public function setUp()
->willReturn($documentApiMock);

$this->clientMock = $clientMock;
$this->projectApiMock = $projectApiMock;
}

/**
Expand All @@ -78,6 +80,7 @@ public function shouldCreateEmpty()
$category = 'C014';
$briefing = 'Lorem ipsum...';
$options = array('language_level' => 'premium');
$callback = array(ProjectInterface::STATUS_IN_PROGRESS => 'http://callback.url');

$project = new Project($this->clientMock);
$project
Expand All @@ -88,6 +91,7 @@ public function shouldCreateEmpty()
->setCategory($category)
->setBriefing($briefing)
->setOptions($options)
->setCallback($callback)
;

$this->assertNull($project->getId());
Expand All @@ -99,6 +103,7 @@ public function shouldCreateEmpty()
$this->assertSame($category, $project->getCategory());
$this->assertSame($briefing, $project->getBriefing());
$this->assertSame($options, $project->getOptions());
$this->assertSame($callback, $project->getCallback());
}

/**
Expand Down Expand Up @@ -195,6 +200,19 @@ public function shouldGetAllowedActivities()
$this->assertSame($allowedActivities, Project::getAllowedActivities());
}

/**
* @test
*/
public function shouldLaunch()
{
$this->projectApiMock->expects($this->once())
->method('launch')
->willReturn(array());

$project = new Project($this->clientMock, '123456');
$project->launch();
}

/**
* @test
* @expectedException \Textmaster\Exception\ObjectImmutableException
Expand Down Expand Up @@ -229,4 +247,29 @@ public function shouldNotCreateDocumentOnUnsaved()
$project = new Project($this->clientMock);
$project->createDocument();
}

/**
* @test
* @expectedException \Textmaster\Exception\InvalidArgumentException
*/
public function shouldNotSetWrongCallback()
{
$project = new Project($this->clientMock, '123456');
$project->setCallback(array('wrong_callback' => 'bad value'));
}

/**
* @test
* @expectedException \Textmaster\Exception\BadMethodCallException
*/
public function shouldNotLaunchImmutable()
{
$values = array(
'id' => 'ID-IMMUTABLE',
'status' => ProjectInterface::STATUS_IN_PROGRESS,
);

$project = new Project($this->clientMock, $values);
$project->launch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Textmaster\Tests\Translator\Adapter;

use Textmaster\Translator\Adapter\GedmoTranslatableAdapter;

class GedmoTranslatableAdapterTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function shouldCreateSameLocale()
{
$managerRegistryMock = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$listenerMock = $this->getMock('Gedmo\Translatable\TranslatableListener');

$translatableMock = $this->getMock('Gedmo\Translatable\Translatable', array('getName', 'getId'));
$documentMock = $this->getMock('Textmaster\Model\Document', array('getProject', 'save'), array(), '', false);
$projectMock = $this->getMock('Textmaster\Model\Project', array('getLanguageFrom'), array(), '', false);

$translatableMock->expects($this->once())
->method('getName')
->willReturn('name');
$translatableMock->expects($this->once())
->method('getId')
->willReturn(1);

$documentMock->expects($this->once())
->method('getProject')
->willReturn($projectMock);
$documentMock->expects($this->once())
->method('save')
->willReturn($documentMock);

$projectMock->expects($this->once())
->method('getLanguageFrom')
->willReturn('en');

$listenerMock->expects($this->once())
->method('getListenerLocale')
->willReturn('en');

$adapter = new GedmoTranslatableAdapter($managerRegistryMock, $listenerMock);
$adapter->create($translatableMock, array('name'), $documentMock);
}

/**
* @test
*/
public function shouldCreateDifferentLocale()
{
$managerRegistryMock = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$listenerMock = $this->getMock('Gedmo\Translatable\TranslatableListener');

$translatableMock = $this->getMock('Gedmo\Translatable\Translatable', array('setLocale', 'getName', 'getId'));
$documentMock = $this->getMock('Textmaster\Model\Document', array('getProject', 'save'), array(), '', false);
$projectMock = $this->getMock('Textmaster\Model\Project', array('getLanguageFrom'), array(), '', false);
$entityManagerMock = $this->getMock('Doctrine\Common\Persistence\ObjectManager');

$translatableMock->expects($this->once())
->method('getName')
->willReturn('name');
$translatableMock->expects($this->once())
->method('getId')
->willReturn(1);

$documentMock->expects($this->once())
->method('getProject')
->willReturn($projectMock);
$documentMock->expects($this->once())
->method('save')
->willReturn($documentMock);

$projectMock->expects($this->once())
->method('getLanguageFrom')
->willReturn('en');

$listenerMock->expects($this->once())
->method('getListenerLocale')
->willReturn('fr');

$managerRegistryMock->expects($this->once())
->method('getManagerForClass')
->willReturn($entityManagerMock);

$entityManagerMock->expects($this->once())
->method('refresh');

$adapter = new GedmoTranslatableAdapter($managerRegistryMock, $listenerMock);
$adapter->create($translatableMock, array('name'), $documentMock);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Textmaster\Tests\Translator\Provider;

use Textmaster\Exception\MappingNotFoundException;
use Textmaster\Tests\Mock\MockTranslatable;
use Textmaster\Translator\Provider\ChainedMappingProvider;

class ChainedMappingProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function shouldGetProperties()
{
$providerMock1 = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface');
$providerMock2 = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface');
$providers = array($providerMock1, $providerMock2);

$subjectMock = new MockTranslatable();

$providerMock1->expects($this->once())
->method('getProperties')
->will($this->throwException(new MappingNotFoundException('value')));

$providerMock2->expects($this->once())
->method('getProperties')
->willReturn(array('name'));

$provider = new ChainedMappingProvider($providers);
$properties = $provider->getProperties($subjectMock);

$this->assertSame(array('name'), $properties);
}

/**
* @test
* @expectedException \Textmaster\Exception\MappingNotFoundException
*/
public function shouldNotGetProperties()
{
$providerMock1 = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface');
$providerMock2 = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface');
$providers = array($providerMock1, $providerMock2);

$subjectMock = new MockTranslatable();

$providerMock1->expects($this->once())
->method('getProperties')
->will($this->throwException(new MappingNotFoundException('value')));

$providerMock2->expects($this->once())
->method('getProperties')
->will($this->throwException(new MappingNotFoundException('value')));

$provider = new ChainedMappingProvider($providers);
$provider->getProperties($subjectMock);
}
}
Loading

0 comments on commit 3376ac2

Please sign in to comment.