diff --git a/composer.json b/composer.json index 8171c72..a2972ea 100755 --- a/composer.json +++ b/composer.json @@ -10,17 +10,18 @@ { "name": "Pierre Ducoudray", "email": "pducoudray@worldia.com" } ], "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", diff --git a/lib/Textmaster/Translator/Adapter/GedmoTranslatableAdapter.php b/lib/Textmaster/Translator/Adapter/GedmoTranslatableAdapter.php index bd58c2c..94fa3a4 100644 --- a/lib/Textmaster/Translator/Adapter/GedmoTranslatableAdapter.php +++ b/lib/Textmaster/Translator/Adapter/GedmoTranslatableAdapter.php @@ -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 { @@ -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) { diff --git a/test/Textmaster/Functional/ManagerTest.php b/test/Textmaster/Functional/ManagerTest.php index 9af716e..d1c48b5 100644 --- a/test/Textmaster/Functional/ManagerTest.php +++ b/test/Textmaster/Functional/ManagerTest.php @@ -18,6 +18,7 @@ class ManagerTest extends \PHPUnit_Framework_TestCase { const TEST_PROJECT_ID = '57065757f41f44001100000e'; + const TEST_DOCUMENT_ID = '57065d0ef41f44000e00008d'; /** * @var Client @@ -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()); + } } diff --git a/test/Textmaster/Tests/Model/ProjectTest.php b/test/Textmaster/Tests/Model/ProjectTest.php index c31fb6c..80ae66b 100644 --- a/test/Textmaster/Tests/Model/ProjectTest.php +++ b/test/Textmaster/Tests/Model/ProjectTest.php @@ -18,6 +18,7 @@ class ProjectTest extends \PHPUnit_Framework_TestCase { protected $clientMock; + protected $projectApiMock; public function setUp() { @@ -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') @@ -63,6 +64,7 @@ public function setUp() ->willReturn($documentApiMock); $this->clientMock = $clientMock; + $this->projectApiMock = $projectApiMock; } /** @@ -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 @@ -88,6 +91,7 @@ public function shouldCreateEmpty() ->setCategory($category) ->setBriefing($briefing) ->setOptions($options) + ->setCallback($callback) ; $this->assertNull($project->getId()); @@ -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()); } /** @@ -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 @@ -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(); + } } diff --git a/test/Textmaster/Tests/Translator/Adapter/GedmoTranslatableAdapterTest.php b/test/Textmaster/Tests/Translator/Adapter/GedmoTranslatableAdapterTest.php new file mode 100644 index 0000000..ab88f56 --- /dev/null +++ b/test/Textmaster/Tests/Translator/Adapter/GedmoTranslatableAdapterTest.php @@ -0,0 +1,92 @@ +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); + } +} diff --git a/test/Textmaster/Tests/Translator/Provider/ChainedMappingProviderTest.php b/test/Textmaster/Tests/Translator/Provider/ChainedMappingProviderTest.php new file mode 100644 index 0000000..b32ddd9 --- /dev/null +++ b/test/Textmaster/Tests/Translator/Provider/ChainedMappingProviderTest.php @@ -0,0 +1,59 @@ +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); + } +} diff --git a/test/Textmaster/Tests/Translator/TranslatorTest.php b/test/Textmaster/Tests/Translator/TranslatorTest.php index 6856bcd..829438c 100644 --- a/test/Textmaster/Tests/Translator/TranslatorTest.php +++ b/test/Textmaster/Tests/Translator/TranslatorTest.php @@ -2,6 +2,7 @@ namespace Textmaster\Tests\Translator; +use Textmaster\Exception\UnexpectedTypeException; use Textmaster\Translator\Translator; class TranslatorTest extends \PHPUnit_Framework_TestCase @@ -30,6 +31,35 @@ public function shouldCreate() $translator->create($subjectMock, $documentMock); } + /** + * @test + */ + public function shouldCreateFromFactory() + { + $adapterMock = $this->getMock('Textmaster\Translator\Adapter\AdapterInterface'); + $mappingProviderMock = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface'); + $documentFactoryMock = $this->getMock('Textmaster\Translator\Factory\DocumentFactoryInterface'); + $adapters = array($adapterMock); + + $subjectMock = $this->getMock('Subject'); + $documentMock = $this->getMock('Textmaster\Model\DocumentInterface'); + + $documentFactoryMock->expects($this->once()) + ->method('createDocument') + ->willReturn($documentMock); + + $mappingProviderMock->expects($this->once()) + ->method('getProperties') + ->willReturn(array()); + + $adapterMock->expects($this->once()) + ->method('supports') + ->willReturn(true); + + $translator = new Translator($adapters, $mappingProviderMock, $documentFactoryMock); + $translator->create($subjectMock); + } + /** * @test */ @@ -49,6 +79,55 @@ public function shouldComplete() $translator->complete($documentMock); } + /** + * @test + */ + public function shouldCompleteWhenTwoAdapters() + { + $adapterMock1 = $this->getMock('Textmaster\Translator\Adapter\AdapterInterface'); + $adapterMock2 = $this->getMock('Textmaster\Translator\Adapter\AdapterInterface'); + $mappingProviderMock = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface'); + $adapters = array($adapterMock1, $adapterMock2); + + $documentMock = $this->getMock('Textmaster\Model\DocumentInterface'); + + $adapterMock1->expects($this->once()) + ->method('complete') + ->will($this->throwException(new UnexpectedTypeException('value', 'expectedType'))); + + $adapterMock2->expects($this->once()) + ->method('complete') + ->willReturn(true); + + $translator = new Translator($adapters, $mappingProviderMock); + $translator->complete($documentMock); + } + + /** + * @test + */ + public function shouldGetSubjectFromDocument() + { + $adapterMock1 = $this->getMock('Textmaster\Translator\Adapter\AdapterInterface'); + $adapterMock2 = $this->getMock('Textmaster\Translator\Adapter\AdapterInterface'); + $mappingProviderMock = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface'); + $adapters = array($adapterMock1, $adapterMock2); + + $documentMock = $this->getMock('Textmaster\Model\DocumentInterface'); + $subjectMock = $this->getMock('Subject'); + + $adapterMock1->expects($this->once()) + ->method('getSubjectFromDocument') + ->willReturn(null); + + $adapterMock2->expects($this->once()) + ->method('getSubjectFromDocument') + ->willReturn($subjectMock); + + $translator = new Translator($adapters, $mappingProviderMock); + $translator->getSubjectFromDocument($documentMock); + } + /** * @test * @expectedException \Textmaster\Exception\InvalidArgumentException @@ -88,4 +167,45 @@ public function shouldNotComplete() $translator = new Translator($adapters, $mappingProviderMock); $translator->complete($documentMock); } + + /** + * @test + * @expectedException \Textmaster\Exception\InvalidArgumentException + */ + public function shouldNotCreateFromFactory() + { + $adapterMock = $this->getMock('Textmaster\Translator\Adapter\AdapterInterface'); + $mappingProviderMock = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface'); + $adapters = array($adapterMock); + + $subjectMock = $this->getMock('Subject'); + + $translator = new Translator($adapters, $mappingProviderMock); + $translator->create($subjectMock); + } + + /** + * @test + * @expectedException \Textmaster\Exception\InvalidArgumentException + */ + public function shouldNotGetSubjectFromDocument() + { + $adapterMock1 = $this->getMock('Textmaster\Translator\Adapter\AdapterInterface'); + $adapterMock2 = $this->getMock('Textmaster\Translator\Adapter\AdapterInterface'); + $mappingProviderMock = $this->getMock('Textmaster\Translator\Provider\MappingProviderInterface'); + $adapters = array($adapterMock1, $adapterMock2); + + $documentMock = $this->getMock('Textmaster\Model\DocumentInterface'); + + $adapterMock1->expects($this->once()) + ->method('getSubjectFromDocument') + ->willReturn(null); + + $adapterMock2->expects($this->once()) + ->method('getSubjectFromDocument') + ->willReturn(null); + + $translator = new Translator($adapters, $mappingProviderMock); + $translator->getSubjectFromDocument($documentMock); + } }