Skip to content

Commit

Permalink
Merge pull request #69 from magento-mpi/MAGETWO-33262
Browse files Browse the repository at this point in the history
MAGETWO-33262: Singletenant compiler doesn't resolve Repositories
  • Loading branch information
dkvashninbay committed Jan 30, 2015
2 parents 413a60a + f3142ee commit f52e5db
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ private function getPreferences()
private function getOptions()
{
return [
Task\OperationFactory::REPOSITORY_GENERATOR => [
'path' => BP . '/' . 'app/code',
'filePatterns' => ['di' => '/\/etc\/([a-zA-Z_]*\/di|di)\.xml$/']
],
Task\OperationFactory::AREA => [
BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation'
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Tools\Di\App\Task;

use Magento\Tools\Di\App\Task\Operation\RepositoryGenerator;
use Magento\Tools\Di\Code\Scanner;
use Magento\Tools\Di\Code\Reader\ClassesScanner;

class RepositoryGeneratorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Scanner\DirectoryScanner | \PHPUnit_Framework_MockObject_MockObject
*/
private $directoryScannerMock;

/**
* @var Scanner\RepositoryScanner | \PHPUnit_Framework_MockObject_MockObject
*/
private $repositoryScannerMock;

/**
* @var ClassesScanner | \PHPUnit_Framework_MockObject_MockObject
*/
private $classesScannerMock;

protected function setUp()
{
$this->directoryScannerMock = $this->getMockBuilder('Magento\Tools\Di\Code\Scanner\DirectoryScanner')
->setMethods([])
->disableOriginalConstructor()
->getMock();
$this->repositoryScannerMock = $this->getMockBuilder('Magento\Tools\Di\Code\Scanner\RepositoryScanner')
->setMethods([])
->disableOriginalConstructor()
->getMock();
$this->classesScannerMock = $this->getMockBuilder('Magento\Tools\Di\Code\Reader\ClassesScanner')
->setMethods([])
->disableOriginalConstructor()
->getMock();
}

/**
* @dataProvider wrongDataDataProvider
*/
public function testDoOperationEmptyData($wrongData)
{
$model = new RepositoryGenerator(
$this->directoryScannerMock,
$this->classesScannerMock,
$this->repositoryScannerMock,
$wrongData
);

$this->assertNull($model->doOperation());
}

/**
* @return array
*/
public function wrongDataDataProvider()
{
return [
[[]],
[['filePatterns' => ['php' => '*.php']]],
[['path' => 'path']]
];
}

public function testDoOperationEmptyRepositories()
{
$data = [
'path' => 'path/to/app',
'filePatterns' => ['di' => 'di.xml'],
];
$files = ['di' => []];
$model = new RepositoryGenerator(
$this->directoryScannerMock,
$this->classesScannerMock,
$this->repositoryScannerMock,
$data
);

$this->classesScannerMock->expects($this->once())
->method('getList')
->with($data['path']);
$this->directoryScannerMock->expects($this->once())
->method('scan')
->with(
$data['path'],
$data['filePatterns']
)->willReturn($files);
$this->repositoryScannerMock->expects($this->once())
->method('setUseAutoload')
->with(false);
$this->repositoryScannerMock->expects($this->once())
->method('collectEntities')
->with($files['di'])
->willReturn([]);

$this->assertEmpty($model->doOperation());
}
}
4 changes: 4 additions & 0 deletions dev/tools/Magento/Tools/Di/App/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function launch()
);

$operations = [
Task\OperationFactory::REPOSITORY_GENERATOR => [
'path' => BP . '/' . 'app/code',
'filePatterns' => ['di' => '/\/etc\/([a-zA-Z_]*\/di|di)\.xml$/']
],
Task\OperationFactory::AREA => [
BP . '/' . 'app/code', BP . '/' . 'lib/internal/Magento/Framework', BP . '/' . 'var/generation'
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Tools\Di\App\Task\Operation;

use Magento\Tools\Di\App\Task\OperationInterface;
use Magento\Tools\Di\Code\Scanner;
use Magento\Tools\Di\Code\Reader\ClassesScanner;

class RepositoryGenerator implements OperationInterface
{
/**
* @var Scanner\DirectoryScanner
*/
private $directoryScanner;

/**
* @var Scanner\RepositoryScanner
*/
private $repositoryScanner;

/**
* @var array
*/
private $data;

/**
* @var ClassesScanner
*/
private $classesScanner;

/**
* @param Scanner\DirectoryScanner $directoryScanner
* @param ClassesScanner $classesScanner
* @param Scanner\RepositoryScanner $repositoryScanner
* @param array $data
*/
public function __construct(
Scanner\DirectoryScanner $directoryScanner,
ClassesScanner $classesScanner,
Scanner\RepositoryScanner $repositoryScanner,
$data = []
) {
$this->directoryScanner = $directoryScanner;
$this->repositoryScanner = $repositoryScanner;
$this->data = $data;
$this->classesScanner = $classesScanner;
}

/**
* Processes operation task
*
* @return void
*/
public function doOperation()
{
if (array_diff(array_keys($this->data), ['filePatterns', 'path'])
!== array_diff(['filePatterns', 'path'], array_keys($this->data))) {
return;
}

$this->classesScanner->getList($this->data['path']);

$files = $this->directoryScanner->scan($this->data['path'], $this->data['filePatterns']);
$this->repositoryScanner->setUseAutoload(false);
$repositories = $this->repositoryScanner->collectEntities($files['di']);
foreach ($repositories as $entityName) {
class_exists($entityName);
}
}
}
7 changes: 7 additions & 0 deletions dev/tools/Magento/Tools/Di/App/Task/OperationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class OperationFactory
*/
const INTERCEPTION_CACHE = 'interception_cache';

/**
* Repository generator
*/
const REPOSITORY_GENERATOR = 'repository_generator';

/**
* Operations definitions
*
Expand All @@ -36,6 +41,7 @@ class OperationFactory
self::AREA => 'Magento\Tools\Di\App\Task\Operation\Area',
self::INTERCEPTION => 'Magento\Tools\Di\App\Task\Operation\Interception',
self::INTERCEPTION_CACHE => 'Magento\Tools\Di\App\Task\Operation\InterceptionCache',
self::REPOSITORY_GENERATOR => 'Magento\Tools\Di\App\Task\Operation\RepositoryGenerator'
];

/**
Expand All @@ -62,6 +68,7 @@ public function create($operationAlias, $arguments = null)
OperationException::UNAVAILABLE_OPERATION
);
}

return $this->objectManager->create($this->operationsDefinitions[$operationAlias], ['data' => $arguments]);
}
}
18 changes: 17 additions & 1 deletion dev/tools/Magento/Tools/Di/Code/Scanner/RepositoryScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
class RepositoryScanner implements ScannerInterface
{
/**
* @var bool
*/
private $useAutoload = true;

/**
* Get array of class names
*
Expand All @@ -32,7 +37,7 @@ public function collectEntities(array $files)
&& !is_null($replacementType)
&& (substr($forType->nodeValue, -19) == 'RepositoryInterface')
) {
if (!class_exists($replacementType->nodeValue)) {
if (!class_exists($replacementType->nodeValue, $this->useAutoload)) {
$persistor = str_replace('\\Repository', 'InterfacePersistor', $replacementType->nodeValue);
$factory = str_replace('\\Repository', 'InterfaceFactory', $replacementType->nodeValue);
$dataBuilder = str_replace('\\Repository', 'DataBuilder', $replacementType->nodeValue);
Expand All @@ -49,4 +54,15 @@ public function collectEntities(array $files)
}
return $repositoryClassNames;
}

/**
* Sets autoload flag
*
* @param boolean $useAutoload
* @return void
*/
public function setUseAutoload($useAutoload)
{
$this->useAutoload = $useAutoload;
}
}

0 comments on commit f52e5db

Please sign in to comment.