Skip to content

Commit

Permalink
improved plugin using differrent interface for required file and dir …
Browse files Browse the repository at this point in the history
…lists ref yawik/standard#4
  • Loading branch information
kilip committed Nov 19, 2018
1 parent f3d9172 commit 6e0f379
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 36 deletions.
21 changes: 11 additions & 10 deletions src/PermissionsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,27 @@ public function onConfigureEvent(ConfigureEvent $event)
$directories = [];

foreach ($modules as $module) {
if ($module instanceof PermissionsFixerModuleInterface) {
$modDirLists = $module->getDirectoryPermissionLists($options);
$modFileLists = $module->getFilePermissionLists($options);

if (!is_null($modDirLists) && !is_array($modDirLists)) {
if ($module instanceof RequireFilePermissionInterface) {
$modFileLists = $module->getRequiredFileLists($options);
if (!is_null($modFileLists) && !is_array($modFileLists)) {
$this->logError(sprintf(
'<comment>%s::getDirectoryPermissionList()</comment> should return an array.',
'<comment>%s::getRequiredFileList()</comment> should return an array.',
get_class($module)
));
} else {
$directories = array_merge($directories, $modDirLists);
$files = array_merge($files, $modFileLists);
}
}

if (!is_null($modFileLists) && !is_array($modFileLists)) {
if ($module instanceof RequireDirectoryPermissionInterface) {
$modDirLists = $module->getRequiredDirectoryLists($options);
if (!is_null($modDirLists) && !is_array($modDirLists)) {
$this->logError(sprintf(
'<comment>%s::getFilePermissionList()</comment> should return an array.',
'<comment>%s::getRequiredDirectoryList()</comment> should return an array.',
get_class($module)
));
} else {
$files = array_merge($files, $modFileLists);
$directories = array_merge($directories, $modDirLists);
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/RequireDirectoryPermissionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Yawik project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Yawik\Composer;

use Core\Options\ModuleOptions as CoreOptions;

interface RequireDirectoryPermissionInterface
{
/**
* Lists of directories that permissions need to be fixed
*
* @param CoreOptions $options A CoreOptions object to use
* @return array A list of files
*/
public function getRequiredDirectoryLists(CoreOptions $options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@
* @author Anthonius Munthi <[email protected]>
* @since 0.32.0
*/
interface PermissionsFixerModuleInterface
interface RequireFilePermissionInterface
{
/**
* Lists of files that permissions need to be fixed
*
* @param CoreOptions $options A CoreOptions object to use
* @return array A list of files
*/
public function getFilePermissionLists(CoreOptions $options);

/**
* Lists of directories that permissions need to be fixed
*
* @param CoreOptions $options A CoreOptions object to use
* @return array A list of files
*/
public function getDirectoryPermissionLists(CoreOptions $options);
public function getRequiredFileLists(CoreOptions $options);
}
27 changes: 17 additions & 10 deletions test/PermissionsFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use Yawik\Composer\Event\ConfigureEvent;
use Yawik\Composer\PermissionsFixer;
use PHPUnit\Framework\TestCase;
use Yawik\Composer\PermissionsFixerModuleInterface;
use Yawik\Composer\RequireDirectoryPermissionInterface;
use Yawik\Composer\RequireFilePermissionInterface;
use Core\Options\ModuleOptions as CoreOptions;

/**
Expand Down Expand Up @@ -51,9 +52,9 @@ public function setUp()

public function testOnConfigureEvent()
{
$module1 = $this->createMock(PermissionsFixerModuleInterface::class);
$module2 = $this->createMock(PermissionsFixerModuleInterface::class);
$modError = $this->createMock(PermissionsFixerModuleInterface::class);
$module1 = $this->createMock(RequireFileAndDirPermissionModule::class);
$module2 = $this->createMock(RequireFileAndDirPermissionModule::class);
$modError = $this->createMock(RequireFileAndDirPermissionModule::class);
$modules = [$module1,$module2];
$plugin = $this->getMockBuilder(PermissionsFixer::class)
->setMethods(['touch','chmod','mkdir','logError'])
Expand All @@ -63,25 +64,25 @@ public function testOnConfigureEvent()
foreach ($modules as $index => $module) {
$index = $index+1;
$module->expects($this->once())
->method('getDirectoryPermissionLists')
->method('getRequiredDirectoryLists')
->willReturn([
'public/static/module'.$index
])
;
$module->expects($this->once())
->method('getFilePermissionLists')
->method('getRequiredFileLists')
->willReturn([
'public/module'.$index.'.log'
])
;
}

$modError->expects($this->once())
->method('getDirectoryPermissionLists')
->method('getRequiredDirectoryLists')
->willReturn('foo')
;
$modError->expects($this->once())
->method('getFilePermissionLists')
->method('getRequiredFileLists')
->willReturn('bar')
;
$modules[] = $modError;
Expand Down Expand Up @@ -122,8 +123,8 @@ public function testOnConfigureEvent()
$plugin->expects($this->exactly(2))
->method('logError')
->withConsecutive(
[$this->stringContains('getDirectoryPermissionList()')],
[$this->stringContains('getFilePermissionList()')]
[$this->stringContains('should return an array.')],
[$this->stringContains('should return an array.')]
)
;

Expand Down Expand Up @@ -179,3 +180,9 @@ public function getTestFilesystemAction()
];
}
}

abstract class RequireFileAndDirPermissionModule implements
RequireDirectoryPermissionInterface,
RequireFilePermissionInterface
{
}
4 changes: 2 additions & 2 deletions test/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Prophecy\Argument;
use Yawik\Composer\Event\ActivateEvent;
use Yawik\Composer\Event\ConfigureEvent;
use Yawik\Composer\PermissionsFixerModuleInterface;
use Yawik\Composer\RequireFilePermissionInterface;
use Yawik\Composer\Plugin;
use Zend\EventManager\EventManager;
use Zend\ModuleManager\ModuleManager;
Expand Down Expand Up @@ -276,7 +276,7 @@ public function testOnPostAutoloadDump()
$app = $this->prophesize(Application::class);
$manager = $this->prophesize(ModuleManager::class);
$options = $this->prophesize(ModuleOptions::class);
$mod1 = $this->prophesize(PermissionsFixerModuleInterface::class)->reveal();
$mod1 = $this->prophesize(RequireFilePermissionInterface::class)->reveal();
$modules = [$mod1];
$events = $this->createMock(EventManager::class);

Expand Down
9 changes: 5 additions & 4 deletions test/sandbox/src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@

namespace Foo;

use Yawik\Composer\PermissionsFixerModuleInterface;
use Yawik\Composer\RequireDirectoryPermissionInterface;
use Yawik\Composer\RequireFilePermissionInterface;
use Core\Options\ModuleOptions as CoreOptions;

class Module implements PermissionsFixerModuleInterface
class Module implements RequireFilePermissionInterface, RequireDirectoryPermissionInterface
{
public function getDirectoryPermissionLists(CoreOptions $options)
public function getRequiredDirectoryLists(CoreOptions $options)
{
return [
'public/static/Organizations/images'
];
}

public function getFilePermissionLists(CoreOptions $options)
public function getRequiredFileLists(CoreOptions $options)
{
return [];
}
Expand Down

0 comments on commit 6e0f379

Please sign in to comment.