Skip to content

Commit

Permalink
Fix Project class behaviour, also adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
falvarez committed Aug 31, 2016
1 parent 788fb6f commit 3426b9b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Application extends BaseApplication
{
const APP_NAME = 'Bitban Technologies PHP Code Quality Tools';
const APP_VERSION = '0.9.9';
const APP_VERSION = '0.9.10';

public function __construct()
{
Expand Down
44 changes: 30 additions & 14 deletions src/Infrastructure/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@

class Project
{
private $basepath = null;

/**
* Project constructor.
* @param string $basepath @optional
*/
public function __construct($basepath = null)
{
$this->basepath = $basepath === null ? GitHelper::getProjectBasepath() : $basepath;
}

public function getBasepath()
{
return GitHelper::getProjectBasepath();
return $this->basepath;
}

public function getBinPath()
Expand All @@ -24,31 +35,36 @@ public function getBinPath()
}

/**
* @param string $path
* Returns a list of PHP, JSON and Composer files (according to regexp detection) with full path for each file.
* Obviously, excluded paths are discarded
*
* @param string $path May be directory or file
* @param string[] $excludedPaths
* @return string[]
*/
public function listFiles($path, $excludedPaths = [])
{
if (is_file($path)) {
$defaultProjectBasepath = (new Project())->getBasepath();
$file = str_replace($defaultProjectBasepath . '/', '', $path);
return [$file];
}

$realpath = realpath($path);
$finder = new Finder();
$finder
->files()
->in($path)
->name(Constants::PHP_FILES_REGEXP)
->name(Constants::JSON_FILES_REGEXP)
->name(Constants::COMPOSER_FILES_REGEXP)
->exclude($excludedPaths);

if (is_file($realpath)) {
$finder
->in(dirname($realpath))
->name(basename($realpath));
} else {
$finder
->in($realpath)
->name(Constants::PHP_FILES_REGEXP)
->name(Constants::JSON_FILES_REGEXP)
->name(Constants::COMPOSER_FILES_REGEXP);
}

$filenames = [];
foreach ($finder as $file) {
$fileRealname = $file->getRealPath();
$filenames[] = str_replace($path . '/', '', $fileRealname);
$filenames[] = $file->getRealPath();
}
return $filenames;
}
Expand Down
87 changes: 87 additions & 0 deletions tests/ProjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* Copyright 2016 Bitban Technologies, S.L.
* Todos los derechos reservados.
*/

namespace Bitban\PhpCodeQualityTools\Tests;

use Bitban\PhpCodeQualityTools\Infrastructure\Project;

class ProjectTest extends \PHPUnit_Framework_TestCase
{
use TempFilesTrait;

/** @var Project */
private $project;

private function setUpTempFiles()
{
$this->createTempDirectories(['dir1', 'dir2', 'dir3/dir4']);
$this->writeTempFiles([
'file1.php' => '',
'file2.php' => '',
'file3.php' => '',
'file4.txt' => ''
], 'dir1');
$this->writeTempFiles([
'file5.php' => '',
'file6.php' => '',
'file7.txt' => ''
], 'dir2');
$this->writeTempFiles([
'file8.php' => '',
'file9.php' => ''
], 'dir3/dir4');

$this->project = new Project($this->tmpdir);
}

public function testInvalidFile()
{
$this->setUpTempFiles();
$this->setExpectedException('InvalidArgumentException');
$files = $this->project->listFiles($this->tmpdir . '/invalidFile.php');
}

public function testInvalidPath()
{
$this->setUpTempFiles();
$this->setExpectedException('InvalidArgumentException');
$files = $this->project->listFiles($this->tmpdir . '/foo');
}

public function testSingleFile()
{
$this->setUpTempFiles();
$files = $this->project->listFiles($this->tmpdir . '/dir1/file3.php');
$numFiles = count($files);
$this->assertEquals(1, $numFiles, "1 file should be returned, but $numFiles were");
$this->assertEquals(realpath($this->tmpdir . '/dir1/file3.php'), $files[0], 'dir1/file3.php should be returned, but it is not');
}

public function testListFiles()
{
$this->setUpTempFiles();
$files = $this->project->listFiles($this->tmpdir . '/dir1');
$numFiles = count($files);
$this->assertEquals(3, $numFiles, "3 files should be returned, but $numFiles were");
$this->assertContains(realpath($this->tmpdir . '/dir1/file1.php'), $files, 'dir1/file1.php should be returned but it is not');
$this->assertContains(realpath($this->tmpdir . '/dir1/file2.php'), $files, 'dir1/file2.php should be returned but it is not');
$this->assertContains(realpath($this->tmpdir . '/dir1/file3.php'), $files, 'dir1/file3.php should be returned but it is not');
$this->assertNotContains(realpath($this->tmpdir . '/dir1/file4.txt'), $files, 'dir1/file4.txt should not be returned but it is');
}

public function testListFilesWithExclusions()
{
$this->setUpTempFiles();
$files = $this->project->listFiles($this->tmpdir, ['dir1', 'dir3/dir4']);
$numFiles = count($files);
$this->assertEquals(2, $numFiles, "2 files should be returned, but $numFiles were");
$this->assertContains(realpath($this->tmpdir . '/dir2/file5.php'), $files, 'dir2/file5.php should be returned but it is not');
$this->assertContains(realpath($this->tmpdir . '/dir2/file6.php'), $files, 'dir2/file6.php should be returned but it is not');
$this->assertNotContains(realpath($this->tmpdir . '/dir1/file1.php'), $files, 'dir1/file1.php should not be returned but it is');
$this->assertNotContains(realpath($this->tmpdir . '/dir2/file7.txt'), $files, 'dir2/file7.txt should not be returned but it is');
}
}
14 changes: 11 additions & 3 deletions tests/TempFilesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ public function setUp()
exec("mkdir -p $this->tmpdir");
}

private function writeTempFiles($files)
private function createTempDirectories($directories)
{
foreach ($directories as $directory) {
exec("mkdir -p $this->tmpdir/$directory");
}
}

private function writeTempFiles($files, $directory = '')
{
foreach ($files as $filename => $filecontent) {
file_put_contents($this->tmpdir . '/' . $filename, $filecontent);
$relativeDirectory = ($directory != '') ? $directory . '/' : '';
file_put_contents($this->tmpdir . '/' . $relativeDirectory . $filename, $filecontent);
}
return array_keys($files);
}
Expand All @@ -34,4 +42,4 @@ public function tearDown()
{
exec("rm -rf $this->tmpdir");
}
}
}

0 comments on commit 3426b9b

Please sign in to comment.