Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Aurolieen/FileOwners.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Aurolieen;

/**
* Assessment 1. File Owners
*
* Class FileOwners
* @package Aurolieen
*/
class FileOwners
{
/**
* Groups the provided files by their owners.
*
* @param array $files Map containing file keys and owner values.
* @return array The input files grouped by their owners.
*/
public function groupByOwners($files)
{
if (empty($files) || !is_array($files)) return [];
$grouped = [];
foreach ($files as $file => $owner)
{
if (!is_string($file) || !is_string($owner)) continue;
if (!key_exists($owner, $grouped)) $grouped[$owner] = [];
$grouped[$owner][] = $file;
}
return $grouped;
}
}

$files = [
"Input.txt" => "Randy",
"Code.py" => "Stan",
"Output.txt" => "Randy"
];
$fileOwners = new FileOwners;
var_dump($fileOwners->groupByOwners($files));
102 changes: 102 additions & 0 deletions Aurolieen/tests/FileOwnersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Aurolieen\Tests;

use Aurolieen\FileOwners;
use PHPUnit\Framework\TestCase;

/**
* Test suite for the FilerOwners assessment class.
*
* Class FileOwnersTest
* @package Aurolieen\Tests
*/
class FileOwnersTest extends TestCase
{
protected $fileOwners;

/**
* Instantiates a FileOwners object on which the tests are executed.
*/
public function setUp(): void
{
$this->fileOwners = new FileOwners();
}

/**
* Tests that the output is an array of files grouped by their owners using a random set of file/author combinations
* as input.
*
* @param array $input Well formed array of random files and owners.
* @dataProvider randomInputProvider
*/
public function testRandomInput($input)
{
$output = $this->fileOwners->groupByOwners($input);
foreach ($input as $file => $owner)
{
$this->assertArrayHasKey($owner, $output);
$this->assertContains($file, $output[$owner]);
}
}

/**
* Tests that the output is empty when the input is empty.
*/
public function testEmptyInput()
{
$this->assertEquals([], $this->fileOwners->groupByOwners([]));
}

/**
* Tests that any input elements that aren't strings are skipped.
*
* @param mixed $expected The expected output.
* @param mixed $input Invalid input.
* @dataProvider invalidInputProvider
*/
public function testInvalidInput($expected, $input)
{
$this->assertEquals($expected, $this->fileOwners->groupByOwners($input));
}

/**
* Provides invalid input data.
*
* @return array Array of arrays containing invalid input / expected output combinations.
*/
public function invalidInputProvider()
{
return [
[[], [1, 2, 3]],
[
["Randy" => ["file.txt", ""]],
["file.txt" => "Randy", "file2.txt" => null, "file3.txt" => 2, null => "Randy"]
],
[[], 3]
];
}

/**
* Provides valid random input data.
*/
public function randomInputProvider()
{
$randomInputArrays = [];
for ($i = 0; $i < 5; $i++)
{
$randomInputArray = [];
$randJLength = rand(4, 10);
for ($j = 0; $j < $randJLength; $j++)
{
$file = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, rand(3,10));
$owner = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, rand(3,10));
$randomInputArray[$file] = $owner;
}
// Create a duplicate owner.
$randomInputArray[substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, rand(3,10))] = reset($randomInputArray);
$randomInputArrays[] = [$randomInputArray];
}
return $randomInputArrays;
}
}