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
36 changes: 36 additions & 0 deletions Aurolieen/Palindrome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Aurolieen;

/**
* Assessment 2. Palindrome
*
* Class Palindrome
* @package Aurolieen
*/
class Palindrome
{
/**
* Checks whether or not the given word is a palindrome.
* One-letter or zero-letter strings are not considered palindromes by this algorithm.
*
* @param string $word The word to check.
* @return bool True if the word is a palindrome, false if otherwise.
*/
public function isPalindrome($word)
{
if (!is_string($word)) return false;
$length = strlen($word);
if ($length <= 1) return false;
$characters = str_split($word);
$halfLength = (int)($length / 2);
for ($i = 0; $i < $halfLength; $i++)
{
if (strcasecmp($characters[$i], $characters[$length - $i - 1]) !== 0) return false;
}
return true;
}
}

$palindrome = new Palindrome;
echo $palindrome->isPalindrome('Deleveled');
86 changes: 86 additions & 0 deletions Aurolieen/tests/PalindromeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Aurolieen\Tests;

use Aurolieen\Palindrome;
use PHPUnit\Framework\TestCase;

/**
* Test suite for the Palindrome assessment class.
*
* Class PalindromeTest
* @package Aurolieen\Tests
*/
class PalindromeTest extends TestCase
{
protected $palindrome;

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

/**
* Tests that a word with an even amount of letters that is a palindrome results in true.
*/
public function testEvenPalindrome()
{
$this->assertTrue($this->palindrome->isPalindrome('tattarrattat'), 'tattarrattat is a palindrome');
$this->assertTrue($this->palindrome->isPalindrome('doggod'), 'doggod is a palindrome');
}

/**
* Tests that a word with an odd amount of letters that is a palindrome results in true.
*/
public function testOddPalindrome()
{
$this->assertTrue($this->palindrome->isPalindrome('deleveled'), 'deleveled is a palindrome');
$this->assertTrue($this->palindrome->isPalindrome('racecar'), 'racecar is a palindrome');
}

/**
* Tests that a word that isn't a palindrome, results in false.
*/
public function testNonPalindrome()
{
$this->assertFalse($this->palindrome->isPalindrome('abc'), 'abc is not a palindrome');
$this->assertFalse($this->palindrome->isPalindrome('delevueled'), 'delevueled is not a palindrome');
}

/**
* Tests that differences in casing doesn't reflect the outcome of the result.
*/
public function testCasingInvariance()
{
$this->assertEquals($this->palindrome->isPalindrome('DELEVeled'),
$this->palindrome->isPalindrome('delevELED'));
}

/**
* Tests that several input values that are invalid return false as output.
*
* @param mixed $input Invalid input.
* @dataProvider invalidInputProvider
*/
public function testInvalidInput($input)
{
$this->assertFalse($this->palindrome->isPalindrome($input));
}

/**
* Provides invalid input data.
*
* @return array Array of arrays containing invalid input parameters.
*/
public function invalidInputProvider()
{
return [
[null],
[''],
['a']
];
}
}