Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed Dec 15, 2016
1 parent 491547b commit 0acd01c
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 22 deletions.
24 changes: 24 additions & 0 deletions src/Mapper/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\AbstractPart;

abstract class AbstractMapper
{
/**
Expand All @@ -28,4 +30,26 @@ public function __construct(array $options = null)
* @return array $parts - the mapped parts
*/
abstract public function map(array $parts);

/**
* checks if there are still unmapped parts left before the given position
*
* @param array $parts
* @param $index
* @return bool
*/
protected function hasUnmappedPartsBefore(array $parts, $index)
{
foreach ($parts as $k => $part) {
if ($k === $index) {
break;
}

if (!($part instanceof AbstractPart)) {
return true;
}
}

return false;
}
}
22 changes: 0 additions & 22 deletions src/Mapper/LastnameMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,4 @@ public function map(array $parts) {

return array_reverse($parts);
}

/**
* checks if there are still unmapped parts left before the given position
*
* @param array $parts
* @param $index
* @return bool
*/
protected function hasUnmappedPartsBefore(array $parts, $index)
{
foreach ($parts as $k => $part) {
if ($k === $index) {
break;
}

if (!($part instanceof AbstractPart)) {
return true;
}
}

return false;
}
}
46 changes: 46 additions & 0 deletions tests/Mapper/MiddlenameMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\Salutation;
use TheIconic\NameParser\Part\Firstname;
use TheIconic\NameParser\Part\Middlename;
use TheIconic\NameParser\Part\Lastname;

class MiddlenameMapperTest extends AbstractMapperTest
{
/**
* @return array
*/
public function provider()
{
return [
[
'input' => [
new Firstname('Peter'),
'Fry',
new Lastname('Pan'),
],
'expectation' => [
new Firstname('Peter'),
new Middlename('Fry'),
new Lastname('Pan'),
],
],
[
'input' => [
new Firstname('John'),
'Albert',
'Tiberius',
new Lastname('Brown'),
],
'expectation' => [
new Firstname('John'),
new Middlename('Albert'),
new Middlename('Tiberius'),
new Lastname('Brown'),
],
],
];
}
}
50 changes: 50 additions & 0 deletions tests/Mapper/NicknameMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\Salutation;
use TheIconic\NameParser\Part\Nickname;

class NicknameMapperTest extends AbstractMapperTest
{
/**
* @return array
*/
public function provider()
{
return [
[
'input' => [
'James',
'(Jim)',
'T.',
'Kirk',
],
'expectation' => [
'James',
new Nickname('Jim'),
'T.',
'Kirk'
],
],
[
'input' => [
new Salutation('Mr'),
'Andre',
'(The',
'Giant)',
'Rene',
'Roussimoff',
],
'expectation' => [
new Salutation('Mr'),
'Andre',
new Nickname('The'),
new Nickname('Giant'),
'Rene',
'Roussimoff',
],
],
];
}
}
53 changes: 53 additions & 0 deletions tests/Mapper/SalutationMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\Salutation;
use TheIconic\NameParser\Part\Firstname;
use TheIconic\NameParser\Part\Lastname;

class SalutationMapperTest extends AbstractMapperTest
{
/**
* @return array
*/
public function provider()
{
return [
[
'input' => [
'Mr.',
'Pan',
],
'expectation' => [
new Salutation('Mr.'),
'Pan',
],
],
[
'input' => [
'Mr',
'Peter',
'Pan',
],
'expectation' => [
new Salutation('Mr'),
'Peter',
'Pan',
],
],
[
'input' => [
'Mr',
new Firstname('James'),
'Miss',
],
'expectation' => [
new Salutation('Mr'),
new Firstname('James'),
'Miss',
],
],
];
}
}
69 changes: 69 additions & 0 deletions tests/Mapper/SuffixMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\Part\Lastname;
use TheIconic\NameParser\Part\Firstname;
use TheIconic\NameParser\Part\Suffix;

class SuffixMapperTest extends AbstractMapperTest
{
/**
* @return array
*/
public function provider()
{
return [
[
'input' => [
'Mr.',
'James',
'Blueberg',
'PhD',
],
'expectation' => [
'Mr.',
'James',
'Blueberg',
new Suffix('PhD'),
],
],
[
'input' => [
'Prince',
'Alfred',
'III',
],
'expectation' => [
'Prince',
'Alfred',
new Suffix('III'),
],
],
[
'input' => [
new Firstname('Paul'),
new Lastname('Smith'),
'Senior',
],
'expectation' => [
new Firstname('Paul'),
new Lastname('Smith'),
new Suffix('Senior'),
],
],
[
'input' => [
'Senior',
new Firstname('James'),
'Norrington',
],
'expectation' => [
'Senior',
new Firstname('James'),
'Norrington',
],
],
];
}
}

0 comments on commit 0acd01c

Please sign in to comment.