This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2b13a2
commit 641cd56
Showing
15 changed files
with
642 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace KunicMarko\SonataImporterBundle\Tests\Admin; | ||
|
||
use KunicMarko\SonataImporterBundle\Admin\AdminWithImport; | ||
use KunicMarko\SonataImporterBundle\Admin\AdminImportExtension; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Sonata\AdminBundle\Admin\AbstractAdmin; | ||
use Sonata\AdminBundle\Admin\AdminInterface; | ||
use Sonata\AdminBundle\Route\RouteCollection; | ||
|
||
/** | ||
* @author Marko Kunic <kunicmarko20@gmail.com> | ||
*/ | ||
class AdminImportExtensionTest extends TestCase | ||
{ | ||
/** | ||
* @var AdminImportExtension | ||
*/ | ||
private $extension; | ||
|
||
public function setUp() | ||
{ | ||
$this->extension = new AdminImportExtension([ | ||
'action_button' => 'action_button_template', | ||
'dashboard_action' => 'dashboard_action_template', | ||
]); | ||
} | ||
|
||
public function testConfigureRoutes() | ||
{ | ||
$routeCollection = $this->prophesize(RouteCollection::class); | ||
$routeCollection->add(Argument::type('string'))->shouldBeCalled(); | ||
|
||
$this->extension->configureRoutes(new class('', '', '') extends AbstractAdmin implements AdminWithImport { | ||
|
||
}, $routeCollection->reveal()); | ||
} | ||
|
||
public function testConfigureRoutesNoCall() | ||
{ | ||
$routeCollection = $this->prophesize(RouteCollection::class); | ||
$routeCollection->add()->shouldNotBeCalled(); | ||
|
||
$this->extension->configureRoutes($this->getAdmin(), $routeCollection->reveal()); | ||
} | ||
|
||
private function getAdmin(): AdminInterface | ||
{ | ||
return new class('', '', '') extends AbstractAdmin { | ||
}; | ||
} | ||
|
||
public function testConfigureActionButtons(): void | ||
{ | ||
$result = $this->extension->configureActionButtons($this->getAdmin(), [], null, null); | ||
|
||
$this->assertArrayHasKey('import', $result); | ||
$this->assertArrayHasKey('template', $result['import']); | ||
$this->assertSame('action_button_template', $result['import']['template']); | ||
} | ||
|
||
public function testConfigureDashboardActions(): void | ||
{ | ||
$result = $this->extension->configureDashboardActions($this->getAdmin(), []); | ||
|
||
$this->assertArrayHasKey('import', $result); | ||
$this->assertArrayHasKey('template', $result['import']); | ||
$this->assertSame('dashboard_action_template', $result['import']['template']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
namespace KunicMarko\SonataImporterBundle\Tests\Controller; | ||
|
||
use KunicMarko\Importer\ImporterFactory; | ||
use KunicMarko\Importer\Reader\CsvReader; | ||
use KunicMarko\SonataImporterBundle\Tests\Fixtures\Admin; | ||
use KunicMarko\SonataImporterBundle\Tests\Fixtures\CRUDController; | ||
use KunicMarko\SonataImporterBundle\Tests\Fixtures\ImportConfiguration; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @author Marko Kunic <kunicmarko20@gmail.com> | ||
*/ | ||
class ImportCRUDControllerTest extends TestCase | ||
{ | ||
/** | ||
* @var CRUDController | ||
*/ | ||
private $controller; | ||
|
||
/** | ||
* @var FormInterface | ||
*/ | ||
private $form; | ||
|
||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var Admin | ||
*/ | ||
private $admin; | ||
|
||
/** | ||
* @var UploadedFile|MockObject | ||
*/ | ||
private $file; | ||
|
||
public function setUp() | ||
{ | ||
$this->form = $this->prophesize(FormInterface::class); | ||
$this->form->handleRequest(Argument::cetera())->shouldBeCalled(); | ||
|
||
$this->container = $this->prophesize(ContainerInterface::class); | ||
$this->container->getParameter(Argument::cetera())->willReturn(['form' => 'form.template']); | ||
|
||
$this->admin = new Admin(); | ||
|
||
$this->file = $this->getMockBuilder(UploadedFile::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->controller = new CRUDController( | ||
$this->form->reveal(), | ||
$this->container->reveal(), | ||
$this->admin, | ||
$this->file | ||
); | ||
|
||
$importerFactory = new ImporterFactory(); | ||
$importerFactory->addReader(new CsvReader()); | ||
$this->controller->setImporterFactory($importerFactory); | ||
} | ||
|
||
public function testNotSubmitted() | ||
{ | ||
$this->form->createView(Argument::cetera())->shouldBeCalled(); | ||
$this->form->isSubmitted()->willReturn(false); | ||
|
||
$this->controller->importAction(new Request()); | ||
} | ||
|
||
public function testNotValid() | ||
{ | ||
$this->form->createView(Argument::cetera())->shouldBeCalled(); | ||
$this->form->isSubmitted()->willReturn(true); | ||
$this->form->isValid()->willReturn(false); | ||
|
||
$this->controller->importAction(new Request()); | ||
} | ||
|
||
public function testNoImportConfiguration() | ||
{ | ||
$this->form->isSubmitted()->willReturn(true); | ||
$this->form->isValid()->willReturn(true); | ||
|
||
$this->file->expects($this->once()) | ||
->method('getClientOriginalExtension') | ||
->willReturn($type = 'csv'); | ||
|
||
$this->controller->importAction(new Request()); | ||
} | ||
|
||
public function testMissingImportConfigurationForType() | ||
{ | ||
$this->form->isSubmitted()->willReturn(true); | ||
$this->form->isValid()->willReturn(true); | ||
|
||
$this->file->expects($this->once()) | ||
->method('getClientOriginalExtension') | ||
->willReturn($type = 'csv'); | ||
|
||
|
||
$this->controller->setImportConfigurations([Admin::class => []]); | ||
$this->controller->importAction(new Request()); | ||
} | ||
|
||
public function testValid() | ||
{ | ||
$this->form->isSubmitted()->willReturn(true); | ||
$this->form->isValid()->willReturn(true); | ||
|
||
$this->file->expects($this->once()) | ||
->method('getClientOriginalExtension') | ||
->willReturn($type = 'csv'); | ||
|
||
$this->file->expects($this->once()) | ||
->method('getPathname') | ||
->willReturn(__DIR__ . '/../Fixtures/fake.csv'); | ||
|
||
$this->controller->flashType = 'success'; | ||
$this->admin->route = 'list'; | ||
|
||
$this->controller->setImportConfigurations([Admin::class => [$type => new ImportConfiguration()]]); | ||
$this->controller->importAction(new Request()); | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
tests/DependencyInjection/Compiler/AutoConfigureCompilerPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KunicMarko\SonataImporterBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use KunicMarko\SonataImporterBundle\Controller\ImportCRUDController; | ||
use KunicMarko\SonataImporterBundle\DependencyInjection\Compiler\AutoConfigureCompilerPass; | ||
use KunicMarko\SonataImporterBundle\Tests\Fixtures\Admin; | ||
use KunicMarko\SonataImporterBundle\Tests\Fixtures\Controller; | ||
use KunicMarko\SonataImporterBundle\Tests\Fixtures\ImportConfiguration; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
* @author Marko Kunic <kunicmarko20@gmail.com> | ||
*/ | ||
class AutoConfigureCompilerPassTest extends TestCase | ||
{ | ||
/** | ||
* @var AutoConfigureCompilerPass | ||
*/ | ||
private $compilerPass; | ||
|
||
/** | ||
* @var ContainerBuilder | ||
*/ | ||
private $containerBuilder; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->compilerPass = new AutoConfigureCompilerPass(); | ||
$this->containerBuilder = new ContainerBuilder(); | ||
} | ||
|
||
public function testProcess(): void | ||
{ | ||
$this->containerBuilder->setDefinition( | ||
'admin', | ||
($adminDefinition = new Definition( | ||
Admin::class, | ||
[ | ||
0, | ||
1, | ||
'SonataAdminBundle:CRUD' | ||
] | ||
) | ||
)->setTags([ | ||
'sonata.importer.admin' => [], | ||
'sonata.admin' => [], | ||
]) | ||
); | ||
|
||
$this->containerBuilder->setDefinition( | ||
'controller', | ||
($controllerDefinition = new Definition( | ||
Controller::class | ||
) | ||
)->setTags([ | ||
'sonata.importer.controller' => [], | ||
]) | ||
); | ||
|
||
$this->containerBuilder->setDefinition( | ||
'importConfiguration', | ||
(new Definition( | ||
ImportConfiguration::class | ||
) | ||
)->setTags([ | ||
'sonata.importer.configuration' => [], | ||
]) | ||
); | ||
|
||
$this->compilerPass->process($this->containerBuilder); | ||
|
||
$this->assertSame(ImportCRUDController::class, $adminDefinition->getArgument(2)); | ||
|
||
$methodCall = $controllerDefinition->getMethodCalls()[0]; | ||
$methodName = $methodCall[0]; | ||
$methodArguments = $methodCall[1][0]; | ||
|
||
$this->assertSame('setImportConfigurations', $methodName); | ||
$this->assertCount(1, $methodArguments); | ||
$this->assertSame(Admin::class, key($methodArguments)); | ||
$this->assertSame('csv', key($methodArguments[Admin::class])); | ||
$this->assertSame(ImportConfiguration::class, $methodArguments[Admin::class]['csv']->getClass()); | ||
} | ||
|
||
/** | ||
* @expectedException \KunicMarko\SonataImporterBundle\Exception\ImportConfigurationMissingInterface | ||
*/ | ||
public function testProcessImportConfigurationMissing(): void | ||
{ | ||
$this->containerBuilder->setDefinition( | ||
'importConfigurationMissing', | ||
(new Definition( | ||
self::class | ||
) | ||
)->setTags([ | ||
'sonata.importer.configuration' => [], | ||
]) | ||
); | ||
|
||
$this->compilerPass->process($this->containerBuilder); | ||
} | ||
|
||
/** | ||
* @expectedException \KunicMarko\SonataImporterBundle\Exception\ControllerMissingMethod | ||
*/ | ||
public function testProcessControllerMissingMethod(): void | ||
{ | ||
$this->containerBuilder->setDefinition( | ||
'import admin that isn\'t sonata admin', | ||
(new Definition( | ||
Admin::class, | ||
[ | ||
0, | ||
1, | ||
'SonataAdminBundle:CRUD' | ||
] | ||
) | ||
)->setTags([ | ||
'sonata.importer.admin' => [], | ||
]) | ||
); | ||
|
||
$this->containerBuilder->setDefinition( | ||
'controllerMissingMethod', | ||
(new Definition( | ||
self::class | ||
) | ||
)->setTags([ | ||
'sonata.importer.controller' => [], | ||
]) | ||
); | ||
|
||
$this->compilerPass->process($this->containerBuilder); | ||
} | ||
} |
Oops, something went wrong.