Skip to content

Add unit tests #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 22, 2017
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
],
"require": {
"php": "~7.0",
"ext-mbstring": "*",
"illuminate/support": "^5.5"
},
"require-dev": {
Expand Down
28 changes: 28 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ All notable changes to `larapulse/support` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## 2017-11-22

### Added
- `Larapulse\Support\Handlers\RegEx` to work with Regular Expressions
- Init `Larapulse\Support\Helpers\RegEx` to generate Regular Expressions
- Unit tests for `Larapulse\Support\Handlers\Str` class
- Unit tests for `Larapulse\Support\Handlers\Regex` class
- Unit tests for `Larapulse\Support\Helpers\Regex` class
- Unit tests for array and string functions

### Changed
- `Larapulse\Support\Handlers\Str::pop()` add `encoding` attribute
- `Larapulse\Support\Handlers\Str::shift()` add `encoding` attribute
- `Larapulse\Support\Handlers\Str::cutStart()` add `repeat` and `caseSensitive` attributes
- `Larapulse\Support\Handlers\Str::cutEnd()` add `repeat` and `caseSensitive` attributes

### Removed
- Array functions: `array_only`, `array_head`, `array_last`

## 2017-11-21

### Added
- Unit tests for `Larapulse\Support\Handlers\Arr` class

### Changed
- `array_flatten`, `array_flatten_assoc` function not flatten array, if wrong depth defined

## 2017-11-20

### Added
Expand All @@ -12,3 +39,4 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- `Larapulse\Support\Helpers\DataTypes` class
- Array functions: `array_flatten`, `array_flatten_assoc`, `array_depth`, `is_array_of_type`, `is_array_of_types`, `is_array_of_instance`, `array_is_assoc`, `array_only`, `array_head`, `array_last`
- String functions: `str_pop`, `str_shift`, `str_cut_start`, `str_cut_end`, `str_lower`, `str_upper`, `str_title`, `str_length`, `str_words`, `str_substr`, `str_ucfirst`, `str_starts_with`, `str_ends_with`
- Unit tests for `Larapulse\Support\Handlers\Arr` class
29 changes: 29 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Larapulse/Support Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
8 changes: 4 additions & 4 deletions src/Handlers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class Arr
*/
public static function flatten(array $array, $depth = INF) : array
{
$depth = is_int($depth) ? max($depth, 1) : INF;
$depth = is_int($depth) ? max($depth, 0) : INF;

return array_reduce($array, function ($result, $item) use ($depth) {
if (!is_array($item)) {
if (!is_array($item) || $depth === 0) {
return array_merge($result, [$item]);
} elseif ($depth === 1) {
return array_merge($result, array_values($item));
Expand All @@ -39,10 +39,10 @@ public static function flatten(array $array, $depth = INF) : array
public static function flattenAssoc(array $array, $depth = INF) : array
{
$result = [];
$depth = is_int($depth) ? max($depth, 1) : INF;
$depth = is_int($depth) ? max($depth, 0) : INF;

foreach ($array as $key => $value) {
if (is_array($value) && $depth > 1) {
if (is_array($value) && $depth >= 1) {
$result = self::flattenAssoc($value, $depth - 1) + $result;
} else {
$result[$key] = $value;
Expand Down
48 changes: 48 additions & 0 deletions src/Handlers/RegEx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Larapulse\Support\Handlers;

class RegEx
{
const REGEX_CHARS_REPLACEMENT = [
" " => '\s',
"\t" => '\t',
"\n" => '\n',
"\f" => '\f',
"\r" => '\r',
"\v" => '\v',
// "\h" => '\h',
// "\R" => '\R',
];

/**
* Validate if RegEx statement is valid
*
* @param string $regexStatement
*
* @return bool
*/
public static function isValid(string $regexStatement) : bool
{
return @preg_match($regexStatement, null) !== false;
}

/**
* Prepare string to be safety used in regex
*
* @param string $string
* @param string $quotes
*
* @return string
*/
public static function prepare(string $string, $quotes = '/') : string
{
$string = $quotes ? preg_quote($string, $quotes) : $string;

return str_replace(
array_keys(self::REGEX_CHARS_REPLACEMENT),
array_values(self::REGEX_CHARS_REPLACEMENT),
$string
);
}
}
64 changes: 52 additions & 12 deletions src/Handlers/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

namespace Larapulse\Support\Handlers;

use Larapulse\Support\Helpers\Regex as RegexHelper;

class Str
{
/**
* Pop the character off the end of string
*
* @param string $str
* @param string $encoding
*
* @return bool|string
*/
public static function pop(string &$str)
public static function pop(string &$str, string $encoding = null)
{
$last = substr($str, -1);
$str = substr($str, 0, -1);
$encoding = $encoding ?: mb_internal_encoding();

$last = mb_substr($str, -1, null, $encoding);
$str = mb_substr($str, 0, -1, $encoding);

return $last;
}
Expand All @@ -23,13 +28,16 @@ public static function pop(string &$str)
* Shift a character off the beginning of string
*
* @param string $str
* @param string $encoding
*
* @return bool|string
*/
public static function shift(string &$str)
public static function shift(string &$str, string $encoding = null)
{
$first = substr($str, 0, 1);
$str = substr($str, 1);
$encoding = $encoding ?: mb_internal_encoding();

$first = mb_substr($str, 0, 1, $encoding);
$str = mb_substr($str, 1, null, $encoding);

return $first;
}
Expand All @@ -39,24 +47,56 @@ public static function shift(string &$str)
*
* @param string $str
* @param string $subString
* @param bool $repeat
* @param bool $caseSensitive Not working with multi-byte characters
*
* @return string
*/
public static function cutStart(string $str, string $subString = ' ') : string
{
return preg_replace('/^'. preg_quote($subString, '/') . '/', '', $str);
public static function cutStart(
string $str,
string $subString = ' ',
bool $repeat = false,
bool $caseSensitive = true
) : string {
$prepared = RegEx::prepare($subString, '/');
$regex = sprintf(
'/^%s/%s',
($subString
? ($repeat ? RegexHelper::quantifyGroup($prepared, 0) : $prepared)
: ''
),
(!$caseSensitive ? 'i' : '')
);

return preg_replace($regex, '', $str);
}

/**
* Cut substring from the end of string
*
* @param string $str
* @param string $subString
* @param bool $repeat
* @param bool $caseSensitive Not working with multi-byte characters
*
* @return string
*/
public static function cutEnd(string $str, string $subString = ' ') : string
{
return preg_replace('/'. preg_quote($subString, '/') . '$/', '', $str);
public static function cutEnd(
string $str,
string $subString = ' ',
bool $repeat = false,
bool $caseSensitive = true
) : string {
$prepared = RegEx::prepare($subString, '/');
$regex = sprintf(
'/%s$/%s',
($subString
? ($repeat ? RegexHelper::quantifyGroup($prepared, 0) : $prepared)
: ''
),
(!$caseSensitive ? 'i' : '')
);

return preg_replace($regex, '', $str);
}
}
43 changes: 43 additions & 0 deletions src/Helpers/Regex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Larapulse\Support\Helpers;

class Regex
{
public static function quantifyGroup(string $str, $min = null, $max = INF, string $tag = null) : string
{
$tag = $tag ? "?<{$tag}>" : '';

return "({$tag}{$str})".self::fetchQuantifier($min, $max);
}

/**
* Build quantifier from parameters
*
* @param int|null $min
* @param float|int $max
* @param bool $lazyLoad
*
* @return string
*/
public static function fetchQuantifier($min = null, $max = INF, bool $lazyLoad = false) : string
{
$lazy = $lazyLoad ? '?' : '';

switch (true) {
case ($min === 0 && $max === 1):
return '?'.$lazy;
case ($min === 0 && $max === INF):
return '*'.$lazy;
case ($min === 1 && $max === INF):
return '+'.$lazy;
case (is_int($min) && $min >= 1 && $max === null):
return '{'.$min.'}'.$lazy;
case (is_int($min) && (is_int($max) || $max === INF)):
$max = is_int($max) ? $max : '';
return '{'.$min.','.$max.'}'.$lazy;
default:
return '';
}
}
}
45 changes: 3 additions & 42 deletions src/array_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ function array_flatten($array, $depth = INF) : array
* Flatten a multi-dimensional array into a single level with saving keys
*
* @param array $array
* @param int $depth
* @return array
*/
function array_flatten_assoc(array $array) : array
function array_flatten_assoc(array $array, $depth = INF) : array
{
return Arr::flattenAssoc($array);
return Arr::flattenAssoc($array, $depth);
}
}

Expand Down Expand Up @@ -102,43 +103,3 @@ function array_is_assoc(array $array) : bool
return IlluminateArr::isAssoc($array);
}
}

if (!function_exists('array_only')) {
/**
* Get a subset of the items from the given array
*
* @param array $array
* @param array|string $keys
* @return array
*/
function array_only($array, $keys) : array
{
return IlluminateArr::only($array, $keys);
}
}

if (!function_exists('array_head')) {
/**
* Get the first element of an array. Useful for method chaining
*
* @param array $array
* @return mixed
*/
function array_head($array)
{
return reset($array);
}
}

if (!function_exists('array_last')) {
/**
* Get the last element from an array
*
* @param array $array
* @return mixed
*/
function array_last($array)
{
return end($array);
}
}
Loading