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 4613dc1
Showing
10 changed files
with
398 additions
and
2 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
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\ImportAdminExtension; | ||
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 ImportAdminExtensionTest extends TestCase | ||
{ | ||
/** | ||
* @var ImportAdminExtension | ||
*/ | ||
private $extension; | ||
|
||
public function setUp() | ||
{ | ||
$this->extension = new ImportAdminExtension([ | ||
'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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KunicMarko\SonataImporterBundle\Tests\DataFixtures; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class Admin extends TestCase | ||
{ | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KunicMarko\SonataImporterBundle\Tests\DataFixtures; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class Controller extends TestCase | ||
{ | ||
public function setImportConfigurations(): void | ||
{ | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KunicMarko\SonataImporterBundle\Tests\DataFixtures; | ||
|
||
use KunicMarko\SonataImporterBundle\SonataImportConfiguration; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ImportConfiguration extends TestCase implements SonataImportConfiguration | ||
{ | ||
public function map(array $item, array $additionalData) | ||
{ | ||
} | ||
|
||
public function save(array $items, array $additionalData): void | ||
{ | ||
} | ||
|
||
public static function adminClass(): string | ||
{ | ||
return Admin::class; | ||
} | ||
|
||
public static function format(): string | ||
{ | ||
return 'csv'; | ||
} | ||
} |
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\DataFixtures\Admin; | ||
use KunicMarko\SonataImporterBundle\Tests\DataFixtures\Controller; | ||
use KunicMarko\SonataImporterBundle\Tests\DataFixtures\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); | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KunicMarko\SonataImporterBundle\Tests\DependencyInjection; | ||
|
||
use KunicMarko\SonataImporterBundle\DependencyInjection\SonataImporterExtension; | ||
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; | ||
use KunicMarko\SonataImporterBundle\Controller\ImportCRUDController; | ||
|
||
/** | ||
* @author Marko Kunic <kunicmarko20@gmail.com> | ||
*/ | ||
final class SonataImporterExtensionTest extends AbstractExtensionTestCase | ||
{ | ||
public function testLoadsFormServiceDefinition(): void | ||
{ | ||
$this->container->setParameter('kernel.project_dir', $param = 'test'); | ||
|
||
$this->load(); | ||
|
||
$this->assertContainerBuilderHasService( | ||
ImportCRUDController::class | ||
); | ||
} | ||
|
||
protected function getContainerExtensions(): array | ||
{ | ||
return [new SonataImporterExtension()]; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
namespace KunicMarko\SonataImporterBundle\Tests\Form; | ||
|
||
use KunicMarko\SonataImporterBundle\Form\AdminImport as AdminImportForm; | ||
use KunicMarko\SonataImporterBundle\DTO\AdminImport as AdminImportDTO; | ||
use Symfony\Component\Form\Test\TypeTestCase; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
|
||
/** | ||
* @author Marko Kunic <kunicmarko20@gmail.com> | ||
*/ | ||
class AdminImportTest extends TypeTestCase | ||
{ | ||
/** | ||
* @var UploadedFile | ||
*/ | ||
private $uploadedFile; | ||
|
||
private $file; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->uploadedFile = new UploadedFile( | ||
$this->file = tempnam(sys_get_temp_dir(), 'test'), | ||
'imp.csv' | ||
); | ||
} | ||
|
||
public function testSubmitValidData(): void | ||
{ | ||
$form = $this->factory->create( | ||
AdminImportForm::class, | ||
new AdminImportDTO() | ||
); | ||
|
||
$form->submit([ | ||
'file' => $this->uploadedFile | ||
]); | ||
|
||
$this->assertTrue($form->isSynchronized()); | ||
$view = $form->createView(); | ||
|
||
$this->assertArrayHasKey('file', $view->children); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
unlink($this->file); | ||
} | ||
} |
Oops, something went wrong.