mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 17:41:50 +01:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8bcf006e02 | ||
|
|
edc51aeee1 | ||
|
|
6d4e422165 | ||
|
|
6f441bb9ea | ||
|
|
73030d703b | ||
|
|
36ddb326b9 | ||
|
|
452a4ec458 | ||
|
|
325fe6b141 | ||
|
|
a1c26b3812 | ||
|
|
67d93036cf | ||
|
|
9368616dfe | ||
|
|
5ab68d3667 | ||
|
|
4613a63f02 |
@@ -1,10 +1,11 @@
|
||||
FROM php:5.6-cli
|
||||
FROM php:5.5-cli
|
||||
|
||||
#
|
||||
# Tools & libraries
|
||||
#
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
vim \
|
||||
git \
|
||||
zip \
|
||||
unzip \
|
||||
@@ -14,7 +15,7 @@ RUN apt-get update \
|
||||
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
||||
|
||||
#
|
||||
# PHP Extensions
|
||||
# PHP extensions
|
||||
#
|
||||
RUN docker-php-ext-install \
|
||||
zip \
|
||||
@@ -23,10 +24,12 @@ RUN docker-php-ext-install \
|
||||
|
||||
#
|
||||
# PHP configuration:
|
||||
# - default configuration
|
||||
# - timezone
|
||||
#
|
||||
COPY php.ini /usr/local/etc/php/php.ini
|
||||
ARG TIMEZONE
|
||||
RUN echo "date.timezone = $TIMEZONE" >> /usr/local/etc/php/php.ini
|
||||
RUN echo "\n""date.timezone = $TIMEZONE""\n" >> /usr/local/etc/php/php.ini
|
||||
|
||||
#
|
||||
# Xdebug
|
||||
@@ -52,3 +55,10 @@ RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
|
||||
&& php -r "unlink('composer-setup.php');" \
|
||||
&& composer global require --no-plugins --no-scripts hirak/prestissimo \
|
||||
&& rm -rf /root/.composer/cache/*
|
||||
|
||||
#
|
||||
# Bash
|
||||
#
|
||||
RUN sed -i 's/^# export/export/g' /root/.bashrc \
|
||||
&& sed -i 's/^# alias/alias/g' /root/.bashrc \
|
||||
&& echo "\n"'export PATH=/project/vendor/bin:$PATH'"\n" >> /root/.bashrc
|
||||
|
||||
3
.docker/config/php.ini
Normal file
3
.docker/config/php.ini
Normal file
@@ -0,0 +1,3 @@
|
||||
display_errors = On
|
||||
display_startup_errors = On
|
||||
error_reporting = E_ALL
|
||||
@@ -1,7 +1,6 @@
|
||||
[xdebug]
|
||||
zend_extension='xdebug.so'
|
||||
zend_extension=xdebug.so
|
||||
|
||||
xdebug.remote_enable=1
|
||||
xdebug.remote_connect_back=1
|
||||
xdebug.idekey='PHPSTORM'
|
||||
xdebug.remote_port=9001
|
||||
xdebug.remote_host=10.254.254.254
|
||||
|
||||
@@ -28,7 +28,13 @@ assets.installWithSymlink = true
|
||||
|
||||
# Clear cache with the "warmup" option
|
||||
#
|
||||
cache.clearWithWarmup = true
|
||||
# The cache:clear command should always be called with the --no-warmup option. Warmup should be done via the cache:warmup command.
|
||||
# https://github.com/symfony/symfony/blob/master/UPGRADE-3.3.md#frameworkbundle
|
||||
#
|
||||
# Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
# 2017-06-06
|
||||
#
|
||||
cache.clearWithWarmup = false
|
||||
|
||||
# --------------------------------------------------------------------------------
|
||||
# Composer
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "meritoo/common-library",
|
||||
"description": "Useful classes, methods, extensions etc.",
|
||||
"license": "MIT",
|
||||
"version": "0.0.15",
|
||||
"version": "0.0.18",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Meritoo.pl",
|
||||
@@ -11,18 +11,18 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.6.0",
|
||||
"php": ">=5.5.9",
|
||||
"doctrine/orm": "^2.5",
|
||||
"gedmo/doctrine-extensions": "^2.4",
|
||||
"symfony/http-foundation": "^3.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^2.6",
|
||||
"friendsofphp/php-cs-fixer": "^2.2",
|
||||
"pdepend/pdepend": "^2.5",
|
||||
"phploc/phploc": "^4.0",
|
||||
"phploc/phploc": "^2.1",
|
||||
"phpmd/phpmd": "^2.6",
|
||||
"phpunit/phpunit": "^5.7",
|
||||
"sebastian/phpcpd": "^3.0",
|
||||
"phpunit/phpunit": "^4.8",
|
||||
"sebastian/phpcpd": "^2.0",
|
||||
"squizlabs/php_codesniffer": "^2.9"
|
||||
},
|
||||
"autoload": {
|
||||
@@ -34,5 +34,8 @@
|
||||
"psr-4": {
|
||||
"Meritoo\\Common\\Test\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,8 @@
|
||||
|
||||
namespace Meritoo\Common\Test\Base;
|
||||
|
||||
use DateTime;
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\Common\Utilities\Miscellaneous;
|
||||
use Meritoo\Common\Traits\Test\Base\BaseTestCaseTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
* Base test case with common methods and data providers
|
||||
@@ -25,214 +19,5 @@ use ReflectionMethod;
|
||||
*/
|
||||
abstract class BaseTestCase extends TestCase
|
||||
{
|
||||
/**
|
||||
* Path of directory with data used by test cases
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $testsDataDirPath = '.data/tests';
|
||||
|
||||
/**
|
||||
* Provides an empty value
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideEmptyValue()
|
||||
{
|
||||
yield[''];
|
||||
yield[' '];
|
||||
yield[null];
|
||||
yield[0];
|
||||
yield[false];
|
||||
yield[[]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides boolean value
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideBooleanValue()
|
||||
{
|
||||
yield[false];
|
||||
yield[true];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides instance of DateTime class
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideDateTimeInstance()
|
||||
{
|
||||
yield[new DateTime()];
|
||||
yield[new DateTime('yesterday')];
|
||||
yield[new DateTime('now')];
|
||||
yield[new DateTime('tomorrow')];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides relative / compound format of DateTime
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideDateTimeRelativeFormat()
|
||||
{
|
||||
yield['now'];
|
||||
yield['yesterday'];
|
||||
yield['tomorrow'];
|
||||
yield['back of 10'];
|
||||
yield['front of 10'];
|
||||
yield['last day of February'];
|
||||
yield['first day of next month'];
|
||||
yield['last day of previous month'];
|
||||
yield['last day of next month'];
|
||||
yield['Y-m-d'];
|
||||
yield['Y-m-d 10:00'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides path of not existing file, e.g. "lorem/ipsum.jpg"
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideNotExistingFilePath()
|
||||
{
|
||||
yield['lets-test.doc'];
|
||||
yield['lorem/ipsum.jpg'];
|
||||
yield['surprise/me/one/more/time.txt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns path of file used by tests.
|
||||
* It should be placed in /.data/tests directory of this project.
|
||||
*
|
||||
* @param string $fileName Name of file
|
||||
* @param string $directoryPath (optional) Path of directory containing the file
|
||||
* @return string
|
||||
*/
|
||||
public function getFilePathToTests($fileName, $directoryPath = '')
|
||||
{
|
||||
$rootPath = Miscellaneous::getProjectRootPath();
|
||||
|
||||
$paths = [
|
||||
$rootPath,
|
||||
self::$testsDataDirPath,
|
||||
$directoryPath,
|
||||
$fileName,
|
||||
];
|
||||
|
||||
return Miscellaneous::concatenatePaths($paths);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies visibility and arguments of method
|
||||
*
|
||||
* @param string $classNamespace Namespace of class that contains method to verify
|
||||
* @param string|ReflectionMethod $method Name of method or just the method to verify
|
||||
* @param string $visibilityType Expected visibility of verified method. One of
|
||||
* OopVisibilityType class constants.
|
||||
* @param int $argumentsCount (optional) Expected count/amount of arguments of the
|
||||
* verified method
|
||||
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments
|
||||
* of the verified method
|
||||
* @throws UnknownOopVisibilityTypeException
|
||||
*
|
||||
* Attention. 2nd argument, the $method, may be:
|
||||
* - string - name of the method
|
||||
* - instance of ReflectionMethod - just the method (provided by ReflectionClass::getMethod() method)
|
||||
*/
|
||||
protected static function assertMethodVisibilityAndArguments(
|
||||
$classNamespace,
|
||||
$method,
|
||||
$visibilityType,
|
||||
$argumentsCount = 0,
|
||||
$requiredArgumentsCount = 0
|
||||
) {
|
||||
/*
|
||||
* Type of visibility is correct?
|
||||
*/
|
||||
if (!(new OopVisibilityType())->isCorrectType($visibilityType)) {
|
||||
throw new UnknownOopVisibilityTypeException($visibilityType);
|
||||
}
|
||||
|
||||
$reflection = new ReflectionClass($classNamespace);
|
||||
|
||||
/*
|
||||
* Name of method provided only?
|
||||
* Let's find instance of the method (based on reflection)
|
||||
*/
|
||||
if (!$method instanceof ReflectionMethod) {
|
||||
$method = $reflection->getMethod($method);
|
||||
}
|
||||
|
||||
switch ($visibilityType) {
|
||||
case OopVisibilityType::IS_PUBLIC:
|
||||
static::assertTrue($method->isPublic());
|
||||
break;
|
||||
|
||||
case OopVisibilityType::IS_PROTECTED:
|
||||
static::assertTrue($method->isProtected());
|
||||
break;
|
||||
|
||||
case OopVisibilityType::IS_PRIVATE:
|
||||
static::assertTrue($method->isPrivate());
|
||||
break;
|
||||
}
|
||||
|
||||
static::assertEquals($argumentsCount, $method->getNumberOfParameters());
|
||||
static::assertEquals($requiredArgumentsCount, $method->getNumberOfRequiredParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies visibility and arguments of class constructor
|
||||
*
|
||||
* @param string $classNamespace Namespace of class that contains constructor to verify
|
||||
* @param string $visibilityType Expected visibility of verified method. One of OopVisibilityType class
|
||||
* constants.
|
||||
* @param int $argumentsCount (optional) Expected count/amount of arguments of the verified method
|
||||
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments of the verified
|
||||
* method
|
||||
* @throws UnknownOopVisibilityTypeException
|
||||
*/
|
||||
protected static function assertConstructorVisibilityAndArguments(
|
||||
$classNamespace,
|
||||
$visibilityType,
|
||||
$argumentsCount = 0,
|
||||
$requiredArgumentsCount = 0
|
||||
) {
|
||||
/*
|
||||
* Let's grab the constructor
|
||||
*/
|
||||
$reflection = new ReflectionClass($classNamespace);
|
||||
$method = $reflection->getConstructor();
|
||||
|
||||
return static::assertMethodVisibilityAndArguments($classNamespace, $method, $visibilityType, $argumentsCount, $requiredArgumentsCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that class with given namespace has no constructor
|
||||
*
|
||||
* @param string $classNamespace Namespace of class that contains constructor to verify
|
||||
*/
|
||||
protected static function assertHasNoConstructor($classNamespace)
|
||||
{
|
||||
/*
|
||||
* Let's grab the constructor
|
||||
*/
|
||||
$reflection = new ReflectionClass($classNamespace);
|
||||
$constructor = $reflection->getConstructor();
|
||||
|
||||
static::assertNull($constructor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets path of directory with data used by test cases
|
||||
*
|
||||
* @param string $testsDataDirPath Path of directory with data used by test cases
|
||||
*/
|
||||
protected static function setTestsDataDirPath($testsDataDirPath)
|
||||
{
|
||||
static::$testsDataDirPath = $testsDataDirPath;
|
||||
}
|
||||
use BaseTestCaseTrait;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\Base;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
use Meritoo\Common\Traits\Test\Base\BaseTypeTestCaseTrait;
|
||||
|
||||
/**
|
||||
* Base test case for the type of something
|
||||
@@ -13,48 +18,5 @@ use Meritoo\Common\Type\Base\BaseType;
|
||||
*/
|
||||
abstract class BaseTypeTestCase extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* Verifies availability of all types
|
||||
*/
|
||||
public function testAvailabilityOfAllTypes()
|
||||
{
|
||||
$available = $this->getTestedTypeInstance()->getAll();
|
||||
$all = $this->getAllExpectedTypes();
|
||||
|
||||
static::assertEquals($all, $available);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies whether given type is correct or not
|
||||
*
|
||||
* @param string $type Type to verify
|
||||
* @param bool $expected Information if given type is correct or not
|
||||
*
|
||||
* @dataProvider provideTypeToVerify
|
||||
*/
|
||||
public function testIfGivenTypeIsCorrect($type, $expected)
|
||||
{
|
||||
static::assertEquals($expected, $this->getTestedTypeInstance()->isCorrectType($type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides type to verify and information if it's correct
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
abstract public function provideTypeToVerify();
|
||||
|
||||
/**
|
||||
* Returns instance of the tested type
|
||||
*
|
||||
* @return BaseType
|
||||
*/
|
||||
abstract protected function getTestedTypeInstance();
|
||||
|
||||
/**
|
||||
* Returns all expected types of the tested type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function getAllExpectedTypes();
|
||||
use BaseTypeTestCaseTrait;
|
||||
}
|
||||
|
||||
238
src/Traits/Test/Base/BaseTestCaseTrait.php
Normal file
238
src/Traits/Test/Base/BaseTestCaseTrait.php
Normal file
@@ -0,0 +1,238 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Traits\Test\Base;
|
||||
|
||||
use DateTime;
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\Common\Utilities\Miscellaneous;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
* BaseTestCaseTrait
|
||||
* Created on 2017-11-02
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
trait BaseTestCaseTrait
|
||||
{
|
||||
/**
|
||||
* Path of directory with data used by test cases
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $testsDataDirPath = '.data/tests';
|
||||
|
||||
/**
|
||||
* Provides an empty value
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideEmptyValue()
|
||||
{
|
||||
yield[''];
|
||||
yield[' '];
|
||||
yield[null];
|
||||
yield[0];
|
||||
yield[false];
|
||||
yield[[]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides boolean value
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideBooleanValue()
|
||||
{
|
||||
yield[false];
|
||||
yield[true];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides instance of DateTime class
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideDateTimeInstance()
|
||||
{
|
||||
yield[new DateTime()];
|
||||
yield[new DateTime('yesterday')];
|
||||
yield[new DateTime('now')];
|
||||
yield[new DateTime('tomorrow')];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides relative / compound format of DateTime
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideDateTimeRelativeFormat()
|
||||
{
|
||||
yield['now'];
|
||||
yield['yesterday'];
|
||||
yield['tomorrow'];
|
||||
yield['back of 10'];
|
||||
yield['front of 10'];
|
||||
yield['last day of February'];
|
||||
yield['first day of next month'];
|
||||
yield['last day of previous month'];
|
||||
yield['last day of next month'];
|
||||
yield['Y-m-d'];
|
||||
yield['Y-m-d 10:00'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides path of not existing file, e.g. "lorem/ipsum.jpg"
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideNotExistingFilePath()
|
||||
{
|
||||
yield['lets-test.doc'];
|
||||
yield['lorem/ipsum.jpg'];
|
||||
yield['surprise/me/one/more/time.txt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns path of file used by tests.
|
||||
* It should be placed in /.data/tests directory of this project.
|
||||
*
|
||||
* @param string $fileName Name of file
|
||||
* @param string $directoryPath (optional) Path of directory containing the file
|
||||
* @return string
|
||||
*/
|
||||
public function getFilePathForTesting($fileName, $directoryPath = '')
|
||||
{
|
||||
$rootPath = Miscellaneous::getProjectRootPath();
|
||||
|
||||
$paths = [
|
||||
$rootPath,
|
||||
self::$testsDataDirPath,
|
||||
$directoryPath,
|
||||
$fileName,
|
||||
];
|
||||
|
||||
return Miscellaneous::concatenatePaths($paths);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies visibility and arguments of method
|
||||
*
|
||||
* @param string $classNamespace Namespace of class that contains method to verify
|
||||
* @param string|ReflectionMethod $method Name of method or just the method to verify
|
||||
* @param string $visibilityType Expected visibility of verified method. One of
|
||||
* OopVisibilityType class constants.
|
||||
* @param int $argumentsCount (optional) Expected count/amount of arguments of the
|
||||
* verified method
|
||||
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments
|
||||
* of the verified method
|
||||
* @throws UnknownOopVisibilityTypeException
|
||||
*
|
||||
* Attention. 2nd argument, the $method, may be:
|
||||
* - string - name of the method
|
||||
* - instance of ReflectionMethod - just the method (provided by ReflectionClass::getMethod() method)
|
||||
*/
|
||||
protected static function assertMethodVisibilityAndArguments(
|
||||
$classNamespace,
|
||||
$method,
|
||||
$visibilityType,
|
||||
$argumentsCount = 0,
|
||||
$requiredArgumentsCount = 0
|
||||
) {
|
||||
/*
|
||||
* Type of visibility is correct?
|
||||
*/
|
||||
if (!(new OopVisibilityType())->isCorrectType($visibilityType)) {
|
||||
throw new UnknownOopVisibilityTypeException($visibilityType);
|
||||
}
|
||||
|
||||
$reflection = new ReflectionClass($classNamespace);
|
||||
|
||||
/*
|
||||
* Name of method provided only?
|
||||
* Let's find instance of the method (based on reflection)
|
||||
*/
|
||||
if (!$method instanceof ReflectionMethod) {
|
||||
$method = $reflection->getMethod($method);
|
||||
}
|
||||
|
||||
switch ($visibilityType) {
|
||||
case OopVisibilityType::IS_PUBLIC:
|
||||
static::assertTrue($method->isPublic());
|
||||
break;
|
||||
|
||||
case OopVisibilityType::IS_PROTECTED:
|
||||
static::assertTrue($method->isProtected());
|
||||
break;
|
||||
|
||||
case OopVisibilityType::IS_PRIVATE:
|
||||
static::assertTrue($method->isPrivate());
|
||||
break;
|
||||
}
|
||||
|
||||
static::assertEquals($argumentsCount, $method->getNumberOfParameters());
|
||||
static::assertEquals($requiredArgumentsCount, $method->getNumberOfRequiredParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies visibility and arguments of class constructor
|
||||
*
|
||||
* @param string $classNamespace Namespace of class that contains constructor to verify
|
||||
* @param string $visibilityType Expected visibility of verified method. One of OopVisibilityType class
|
||||
* constants.
|
||||
* @param int $argumentsCount (optional) Expected count/amount of arguments of the verified method
|
||||
* @param int $requiredArgumentsCount (optional) Expected count/amount of required arguments of the verified
|
||||
* method
|
||||
* @throws UnknownOopVisibilityTypeException
|
||||
*/
|
||||
protected static function assertConstructorVisibilityAndArguments(
|
||||
$classNamespace,
|
||||
$visibilityType,
|
||||
$argumentsCount = 0,
|
||||
$requiredArgumentsCount = 0
|
||||
) {
|
||||
/*
|
||||
* Let's grab the constructor
|
||||
*/
|
||||
$reflection = new ReflectionClass($classNamespace);
|
||||
$method = $reflection->getConstructor();
|
||||
|
||||
static::assertMethodVisibilityAndArguments($classNamespace, $method, $visibilityType, $argumentsCount, $requiredArgumentsCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that class with given namespace has no constructor
|
||||
*
|
||||
* @param string $classNamespace Namespace of class that contains constructor to verify
|
||||
*/
|
||||
protected static function assertHasNoConstructor($classNamespace)
|
||||
{
|
||||
/*
|
||||
* Let's grab the constructor
|
||||
*/
|
||||
$reflection = new ReflectionClass($classNamespace);
|
||||
$constructor = $reflection->getConstructor();
|
||||
|
||||
static::assertNull($constructor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets path of directory with data used by test cases
|
||||
*
|
||||
* @param string $testsDataDirPath Path of directory with data used by test cases
|
||||
*/
|
||||
protected static function setTestsDataDirPath($testsDataDirPath)
|
||||
{
|
||||
static::$testsDataDirPath = $testsDataDirPath;
|
||||
}
|
||||
}
|
||||
66
src/Traits/Test/Base/BaseTypeTestCaseTrait.php
Normal file
66
src/Traits/Test/Base/BaseTypeTestCaseTrait.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Traits\Test\Base;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
|
||||
/**
|
||||
* Trait for the base test case for the type of something
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
trait BaseTypeTestCaseTrait
|
||||
{
|
||||
/**
|
||||
* Verifies availability of all types
|
||||
*/
|
||||
public function testAvailabilityOfAllTypes()
|
||||
{
|
||||
$available = $this->getTestedTypeInstance()->getAll();
|
||||
$all = $this->getAllExpectedTypes();
|
||||
|
||||
static::assertEquals($all, $available);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies whether given type is correct or not
|
||||
*
|
||||
* @param string $type Type to verify
|
||||
* @param bool $expected Information if given type is correct or not
|
||||
*
|
||||
* @dataProvider provideTypeToVerify
|
||||
*/
|
||||
public function testIfGivenTypeIsCorrect($type, $expected)
|
||||
{
|
||||
static::assertEquals($expected, $this->getTestedTypeInstance()->isCorrectType($type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides type to verify and information if it's correct
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
abstract public function provideTypeToVerify();
|
||||
|
||||
/**
|
||||
* Returns instance of the tested type
|
||||
*
|
||||
* @return BaseType
|
||||
*/
|
||||
abstract protected function getTestedTypeInstance();
|
||||
|
||||
/**
|
||||
* Returns all expected types of the tested type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function getAllExpectedTypes();
|
||||
}
|
||||
@@ -6,9 +6,11 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Utilities;
|
||||
namespace Meritoo\Common\Type;
|
||||
|
||||
use DateTime;
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
use Meritoo\Common\Utilities\Date;
|
||||
|
||||
/**
|
||||
* A date's period.
|
||||
@@ -17,7 +19,7 @@ use DateTime;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class DatePeriod
|
||||
class DatePeriod extends BaseType
|
||||
{
|
||||
/**
|
||||
* The period constant: last month
|
||||
@@ -108,17 +110,6 @@ class DatePeriod
|
||||
$this->endDate = $endDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if given period is correct
|
||||
*
|
||||
* @param int $period The period to verify
|
||||
* @return bool
|
||||
*/
|
||||
public static function isCorrectPeriod($period)
|
||||
{
|
||||
return in_array($period, Reflection::getConstants(__CLASS__));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns formatted one of the period's date: start date or end date
|
||||
*
|
||||
@@ -12,6 +12,7 @@ use DateInterval;
|
||||
use DateTime;
|
||||
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
|
||||
use Meritoo\Common\Type\DatePartType;
|
||||
use Meritoo\Common\Type\DatePeriod;
|
||||
|
||||
/**
|
||||
* Useful date methods
|
||||
@@ -66,97 +67,105 @@ class Date
|
||||
* The dates are returned in an array with indexes 'start' and 'end'.
|
||||
*
|
||||
* @param int $period The period, type of period. One of DatePeriod class constants, e.g. DatePeriod::LAST_WEEK.
|
||||
* @return DatePeriod
|
||||
* @return null|DatePeriod
|
||||
*/
|
||||
public static function getDatesForPeriod($period)
|
||||
{
|
||||
$datePeriod = null;
|
||||
|
||||
if (DatePeriod::isCorrectPeriod($period)) {
|
||||
$dateStart = null;
|
||||
$dateEnd = null;
|
||||
|
||||
switch ($period) {
|
||||
case DatePeriod::LAST_WEEK:
|
||||
$thisWeekStart = new DateTime('this week');
|
||||
|
||||
$dateStart = clone $thisWeekStart;
|
||||
$dateEnd = clone $thisWeekStart;
|
||||
|
||||
$dateStart->sub(new DateInterval('P7D'));
|
||||
$dateEnd->sub(new DateInterval('P1D'));
|
||||
|
||||
break;
|
||||
case DatePeriod::THIS_WEEK:
|
||||
$dateStart = new DateTime('this week');
|
||||
|
||||
$dateEnd = clone $dateStart;
|
||||
$dateEnd->add(new DateInterval('P6D'));
|
||||
|
||||
break;
|
||||
case DatePeriod::NEXT_WEEK:
|
||||
$dateStart = new DateTime('this week');
|
||||
$dateStart->add(new DateInterval('P7D'));
|
||||
|
||||
$dateEnd = clone $dateStart;
|
||||
$dateEnd->add(new DateInterval('P6D'));
|
||||
|
||||
break;
|
||||
case DatePeriod::LAST_MONTH:
|
||||
$dateStart = new DateTime('first day of last month');
|
||||
$dateEnd = new DateTime('last day of last month');
|
||||
|
||||
break;
|
||||
case DatePeriod::THIS_MONTH:
|
||||
$lastMonth = self::getDatesForPeriod(DatePeriod::LAST_MONTH);
|
||||
$nextMonth = self::getDatesForPeriod(DatePeriod::NEXT_MONTH);
|
||||
|
||||
$dateStart = $lastMonth->getEndDate();
|
||||
$dateStart->add(new DateInterval('P1D'));
|
||||
|
||||
$dateEnd = $nextMonth->getStartDate();
|
||||
$dateEnd->sub(new DateInterval('P1D'));
|
||||
|
||||
break;
|
||||
case DatePeriod::NEXT_MONTH:
|
||||
$dateStart = new DateTime('first day of next month');
|
||||
$dateEnd = new DateTime('last day of next month');
|
||||
|
||||
break;
|
||||
case DatePeriod::LAST_YEAR:
|
||||
case DatePeriod::THIS_YEAR:
|
||||
case DatePeriod::NEXT_YEAR:
|
||||
$dateStart = new DateTime();
|
||||
$dateEnd = new DateTime();
|
||||
|
||||
if (DatePeriod::LAST_YEAR == $period || DatePeriod::NEXT_YEAR == $period) {
|
||||
$yearDifference = 1;
|
||||
|
||||
if (DatePeriod::LAST_YEAR == $period) {
|
||||
$yearDifference *= -1;
|
||||
}
|
||||
|
||||
$modifyString = sprintf('%s year', $yearDifference);
|
||||
$dateStart->modify($modifyString);
|
||||
$dateEnd->modify($modifyString);
|
||||
}
|
||||
|
||||
$year = $dateStart->format('Y');
|
||||
$dateStart->setDate($year, 1, 1);
|
||||
$dateEnd->setDate($year, 12, 31);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (null !== $dateStart && null !== $dateEnd) {
|
||||
$dateStart->setTime(0, 0, 0);
|
||||
$dateEnd->setTime(23, 59, 59);
|
||||
|
||||
$datePeriod = new DatePeriod($dateStart, $dateEnd);
|
||||
}
|
||||
/*
|
||||
* Type of period is incorrect?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (!(new DatePeriod())->isCorrectType($period)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $datePeriod;
|
||||
$dateStart = null;
|
||||
$dateEnd = null;
|
||||
|
||||
switch ($period) {
|
||||
case DatePeriod::LAST_WEEK:
|
||||
$thisWeekStart = new DateTime('this week');
|
||||
|
||||
$dateStart = clone $thisWeekStart;
|
||||
$dateEnd = clone $thisWeekStart;
|
||||
|
||||
$dateStart->sub(new DateInterval('P7D'));
|
||||
$dateEnd->sub(new DateInterval('P1D'));
|
||||
|
||||
break;
|
||||
case DatePeriod::THIS_WEEK:
|
||||
$dateStart = new DateTime('this week');
|
||||
|
||||
$dateEnd = clone $dateStart;
|
||||
$dateEnd->add(new DateInterval('P6D'));
|
||||
|
||||
break;
|
||||
case DatePeriod::NEXT_WEEK:
|
||||
$dateStart = new DateTime('this week');
|
||||
$dateStart->add(new DateInterval('P7D'));
|
||||
|
||||
$dateEnd = clone $dateStart;
|
||||
$dateEnd->add(new DateInterval('P6D'));
|
||||
|
||||
break;
|
||||
case DatePeriod::LAST_MONTH:
|
||||
$dateStart = new DateTime('first day of last month');
|
||||
$dateEnd = new DateTime('last day of last month');
|
||||
|
||||
break;
|
||||
case DatePeriod::THIS_MONTH:
|
||||
$lastMonth = self::getDatesForPeriod(DatePeriod::LAST_MONTH);
|
||||
$nextMonth = self::getDatesForPeriod(DatePeriod::NEXT_MONTH);
|
||||
|
||||
$dateStart = $lastMonth->getEndDate();
|
||||
$dateStart->add(new DateInterval('P1D'));
|
||||
|
||||
$dateEnd = $nextMonth->getStartDate();
|
||||
$dateEnd->sub(new DateInterval('P1D'));
|
||||
|
||||
break;
|
||||
case DatePeriod::NEXT_MONTH:
|
||||
$dateStart = new DateTime('first day of next month');
|
||||
$dateEnd = new DateTime('last day of next month');
|
||||
|
||||
break;
|
||||
case DatePeriod::LAST_YEAR:
|
||||
case DatePeriod::THIS_YEAR:
|
||||
case DatePeriod::NEXT_YEAR:
|
||||
$dateStart = new DateTime();
|
||||
$dateEnd = new DateTime();
|
||||
|
||||
if (DatePeriod::LAST_YEAR == $period || DatePeriod::NEXT_YEAR == $period) {
|
||||
$yearDifference = 1;
|
||||
|
||||
if (DatePeriod::LAST_YEAR == $period) {
|
||||
$yearDifference *= -1;
|
||||
}
|
||||
|
||||
$modifyString = sprintf('%s year', $yearDifference);
|
||||
$dateStart->modify($modifyString);
|
||||
$dateEnd->modify($modifyString);
|
||||
}
|
||||
|
||||
$year = $dateStart->format('Y');
|
||||
$dateStart->setDate($year, 1, 1);
|
||||
$dateEnd->setDate($year, 12, 31);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Start or end date is unknown?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (null === $dateStart || null === $dateEnd) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$dateStart->setTime(0, 0, 0);
|
||||
$dateEnd->setTime(23, 59, 59);
|
||||
|
||||
return new DatePeriod($dateStart, $dateEnd);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,6 +33,10 @@ class QueryBuilderUtility
|
||||
{
|
||||
$aliases = $queryBuilder->getRootAliases();
|
||||
|
||||
/*
|
||||
* No aliases?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (empty($aliases)) {
|
||||
return null;
|
||||
}
|
||||
@@ -42,7 +46,8 @@ class QueryBuilderUtility
|
||||
|
||||
/**
|
||||
* Returns alias of given property joined in given query builder
|
||||
* If the join does not exist, null is returned.
|
||||
*
|
||||
* If there are no joins or the join does not exist, null is returned.
|
||||
* It's also information if given property is already joined in given query builder.
|
||||
*
|
||||
* @param QueryBuilder $queryBuilder The query builder to verify
|
||||
@@ -53,6 +58,10 @@ class QueryBuilderUtility
|
||||
{
|
||||
$joins = $queryBuilder->getDQLPart('join');
|
||||
|
||||
/*
|
||||
* No joins?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (empty($joins)) {
|
||||
return null;
|
||||
}
|
||||
@@ -99,33 +108,43 @@ class QueryBuilderUtility
|
||||
*/
|
||||
public static function setCriteria(QueryBuilder $queryBuilder, array $criteria = [], $alias = '')
|
||||
{
|
||||
if (!empty($criteria)) {
|
||||
if (empty($alias)) {
|
||||
$alias = self::getRootAlias($queryBuilder);
|
||||
}
|
||||
/*
|
||||
* No criteria used in WHERE clause?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (empty($criteria)) {
|
||||
return $queryBuilder;
|
||||
}
|
||||
|
||||
foreach ($criteria as $column => $value) {
|
||||
$compareOperator = '=';
|
||||
/*
|
||||
* No alias provided?
|
||||
* Let's use root alias
|
||||
*/
|
||||
if (empty($alias)) {
|
||||
$alias = self::getRootAlias($queryBuilder);
|
||||
}
|
||||
|
||||
if (is_array($value) && !empty($value)) {
|
||||
if (2 == count($value)) {
|
||||
$compareOperator = $value[1];
|
||||
}
|
||||
foreach ($criteria as $column => $value) {
|
||||
$compareOperator = '=';
|
||||
|
||||
$value = $value[0];
|
||||
if (is_array($value) && !empty($value)) {
|
||||
if (2 == count($value)) {
|
||||
$compareOperator = $value[1];
|
||||
}
|
||||
|
||||
$predicate = sprintf('%s.%s %s :%s', $alias, $column, $compareOperator, $column);
|
||||
|
||||
if (null === $value) {
|
||||
$predicate = $queryBuilder->expr()->isNull(sprintf('%s.%s', $alias, $column));
|
||||
unset($criteria[$column]);
|
||||
} else {
|
||||
$queryBuilder->setParameter($column, $value);
|
||||
}
|
||||
|
||||
$queryBuilder = $queryBuilder->andWhere($predicate);
|
||||
$value = $value[0];
|
||||
}
|
||||
|
||||
$predicate = sprintf('%s.%s %s :%s', $alias, $column, $compareOperator, $column);
|
||||
|
||||
if (null === $value) {
|
||||
$predicate = $queryBuilder->expr()->isNull(sprintf('%s.%s', $alias, $column));
|
||||
unset($criteria[$column]);
|
||||
} else {
|
||||
$queryBuilder->setParameter($column, $value);
|
||||
}
|
||||
|
||||
$queryBuilder = $queryBuilder->andWhere($predicate);
|
||||
}
|
||||
|
||||
return $queryBuilder;
|
||||
@@ -143,7 +162,7 @@ class QueryBuilderUtility
|
||||
public static function deleteEntities(EntityManager $entityManager, $entities, $flushDeleted = true)
|
||||
{
|
||||
/*
|
||||
* No entities found?
|
||||
* No entities provided?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (empty($entities)) {
|
||||
@@ -169,24 +188,30 @@ class QueryBuilderUtility
|
||||
* Attention. Existing parameters will be overridden.
|
||||
*
|
||||
* @param QueryBuilder $queryBuilder The query builder
|
||||
* @param array|ArrayCollection $parameters Parameters to add. Collection of instances of
|
||||
* Doctrine\ORM\Query\Parameter class or an array with key-value pairs.
|
||||
* @param array|ArrayCollection $parameters Parameters to add. Collection of Doctrine\ORM\Query\Parameter
|
||||
* instances or an array with key-value pairs.
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
public static function addParameters(QueryBuilder $queryBuilder, $parameters)
|
||||
{
|
||||
if (!empty($parameters)) {
|
||||
foreach ($parameters as $key => $parameter) {
|
||||
$name = $key;
|
||||
$value = $parameter;
|
||||
/*
|
||||
* No parameters?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (empty($parameters)) {
|
||||
return $queryBuilder;
|
||||
}
|
||||
|
||||
if ($parameter instanceof Parameter) {
|
||||
$name = $parameter->getName();
|
||||
$value = $parameter->getValue();
|
||||
}
|
||||
foreach ($parameters as $key => $parameter) {
|
||||
$name = $key;
|
||||
$value = $parameter;
|
||||
|
||||
$queryBuilder->setParameter($name, $value);
|
||||
if ($parameter instanceof Parameter) {
|
||||
$name = $parameter->getName();
|
||||
$value = $parameter->getValue();
|
||||
}
|
||||
|
||||
$queryBuilder->setParameter($name, $value);
|
||||
}
|
||||
|
||||
return $queryBuilder;
|
||||
|
||||
@@ -74,8 +74,8 @@ class Reflection
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns maximum constant from all constants of given class / object.
|
||||
* Values of constants should be integers.
|
||||
* Returns maximum integer value of constant of given class / object.
|
||||
* Constants whose values are integers are considered only.
|
||||
*
|
||||
* @param object|string $class The object or name of object's class
|
||||
* @return int|null
|
||||
|
||||
@@ -33,7 +33,7 @@ class UnknownTypeExceptionTest extends BaseTestCase
|
||||
|
||||
public function testTheException()
|
||||
{
|
||||
$this->expectException(UnknownTestTypeException::class);
|
||||
$this->setExpectedException(UnknownTestTypeException::class);
|
||||
self::assertEmpty((new TestService())->getTranslatedType('test_3'));
|
||||
}
|
||||
}
|
||||
|
||||
176
tests/Test/Base/BaseTestCaseTest.php
Normal file
176
tests/Test/Base/BaseTestCaseTest.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\Test\Base;
|
||||
|
||||
use DateTime;
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\Common\Utilities\GeneratorUtility;
|
||||
|
||||
/**
|
||||
* Test case of the base test case with common methods and data providers
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class BaseTestCaseTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(BaseTestCase::class, OopVisibilityType::IS_PUBLIC, 3);
|
||||
}
|
||||
|
||||
public function testProvideEmptyValue()
|
||||
{
|
||||
$elements = [
|
||||
[''],
|
||||
[' '],
|
||||
[null],
|
||||
[0],
|
||||
[false],
|
||||
[[]],
|
||||
];
|
||||
|
||||
$generator = (new SimpleTestCase())->provideEmptyValue();
|
||||
self::assertEquals($elements, GeneratorUtility::getGeneratorElements($generator));
|
||||
}
|
||||
|
||||
public function testProvideBooleanValue()
|
||||
{
|
||||
$elements = [
|
||||
[false],
|
||||
[true],
|
||||
];
|
||||
|
||||
$generator = (new SimpleTestCase())->provideBooleanValue();
|
||||
self::assertEquals($elements, GeneratorUtility::getGeneratorElements($generator));
|
||||
}
|
||||
|
||||
public function testProvideDateTimeInstance()
|
||||
{
|
||||
$dateFormat = 'Y-m-d H:i';
|
||||
|
||||
$expectedElements = [
|
||||
[new DateTime()],
|
||||
[new DateTime('yesterday')],
|
||||
[new DateTime('now')],
|
||||
[new DateTime('tomorrow')],
|
||||
];
|
||||
|
||||
$generator = (new SimpleTestCase())->provideDateTimeInstance();
|
||||
$generatedElements = GeneratorUtility::getGeneratorElements($generator);
|
||||
|
||||
/* @var DateTime $instance1 */
|
||||
$instance1 = $generatedElements[0][0];
|
||||
|
||||
/* @var DateTime $instance2 */
|
||||
$instance2 = $generatedElements[1][0];
|
||||
|
||||
/* @var DateTime $instance3 */
|
||||
$instance3 = $generatedElements[2][0];
|
||||
|
||||
/* @var DateTime $instance4 */
|
||||
$instance4 = $generatedElements[3][0];
|
||||
|
||||
self::assertCount(count($expectedElements), $generatedElements);
|
||||
self::assertEquals($instance1->format($dateFormat), (new DateTime())->format($dateFormat));
|
||||
self::assertEquals($instance2->format($dateFormat), (new DateTime('yesterday'))->format($dateFormat));
|
||||
self::assertEquals($instance3->format($dateFormat), (new DateTime('now'))->format($dateFormat));
|
||||
self::assertEquals($instance4->format($dateFormat), (new DateTime('tomorrow'))->format($dateFormat));
|
||||
}
|
||||
|
||||
public function testProvideDateTimeRelativeFormat()
|
||||
{
|
||||
$elements = [
|
||||
['now'],
|
||||
['yesterday'],
|
||||
['tomorrow'],
|
||||
['back of 10'],
|
||||
['front of 10'],
|
||||
['last day of February'],
|
||||
['first day of next month'],
|
||||
['last day of previous month'],
|
||||
['last day of next month'],
|
||||
['Y-m-d'],
|
||||
['Y-m-d 10:00'],
|
||||
];
|
||||
|
||||
$generator = (new SimpleTestCase())->provideDateTimeRelativeFormat();
|
||||
self::assertEquals($elements, GeneratorUtility::getGeneratorElements($generator));
|
||||
}
|
||||
|
||||
public function testProvideNotExistingFilePath()
|
||||
{
|
||||
$elements = [
|
||||
['lets-test.doc'],
|
||||
['lorem/ipsum.jpg'],
|
||||
['surprise/me/one/more/time.txt'],
|
||||
];
|
||||
|
||||
$generator = (new SimpleTestCase())->provideNotExistingFilePath();
|
||||
self::assertEquals($elements, GeneratorUtility::getGeneratorElements($generator));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $fileName Name of file
|
||||
* @param string $directoryPath Path of directory containing the file
|
||||
*
|
||||
* @dataProvider provideFileNameAndDirectoryPath
|
||||
*/
|
||||
public function testGetFilePathForTesting($fileName, $directoryPath)
|
||||
{
|
||||
$path = (new SimpleTestCase())->getFilePathForTesting($fileName, $directoryPath);
|
||||
|
||||
if (!empty($directoryPath)) {
|
||||
$directoryPath .= '/';
|
||||
}
|
||||
|
||||
$expectedContains = sprintf('/.data/tests/%s%s', $directoryPath, $fileName);
|
||||
static::assertContains($expectedContains, $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides name of file and path of directory containing the file
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideFileNameAndDirectoryPath()
|
||||
{
|
||||
yield[
|
||||
'abc.jpg',
|
||||
'',
|
||||
];
|
||||
|
||||
yield[
|
||||
'abc.def.jpg',
|
||||
'',
|
||||
];
|
||||
|
||||
yield[
|
||||
'abc.jpg',
|
||||
'def',
|
||||
];
|
||||
|
||||
yield[
|
||||
'abc.def.jpg',
|
||||
'def',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple test case
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class SimpleTestCase extends BaseTestCase
|
||||
{
|
||||
}
|
||||
@@ -9,8 +9,8 @@
|
||||
namespace Meritoo\Common\Test\Type\Base;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test case of the base / abstract type of something
|
||||
@@ -18,8 +18,13 @@ use PHPUnit\Framework\TestCase;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class BaseTypeTest extends TestCase
|
||||
class BaseTypeTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(BaseType::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BaseType $type Type of something
|
||||
* @param array $expectedTypes Expected concrete types of given instance of type
|
||||
|
||||
@@ -6,13 +6,13 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
namespace Meritoo\Common\Test\Type;
|
||||
|
||||
use DateTime;
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Test\Base\BaseTypeTestCase;
|
||||
use Meritoo\Common\Type\DatePeriod;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\Common\Utilities\DatePeriod;
|
||||
|
||||
/**
|
||||
* Test case of date's period
|
||||
@@ -20,7 +20,7 @@ use Meritoo\Common\Utilities\DatePeriod;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class DatePeriodTest extends BaseTestCase
|
||||
class DatePeriodTest extends BaseTypeTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
@@ -58,33 +58,6 @@ class DatePeriodTest extends BaseTestCase
|
||||
self::assertEquals($endDate, $period->getEndDate());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $period Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
*/
|
||||
public function testIsCorrectPeriodEmptyPeriod($period)
|
||||
{
|
||||
self::assertFalse(DatePeriod::isCorrectPeriod($period));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $period Incorrect period to verify
|
||||
* @dataProvider provideIncorrectPeriod
|
||||
*/
|
||||
public function testIsCorrectPeriodIncorrectPeriod($period)
|
||||
{
|
||||
self::assertFalse(DatePeriod::isCorrectPeriod($period));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $period The period to verify
|
||||
* @dataProvider providePeriod
|
||||
*/
|
||||
public function testIsCorrectPeriod($period)
|
||||
{
|
||||
self::assertTrue(DatePeriod::isCorrectPeriod($period));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DatePeriod $period The date period to verify
|
||||
* @param string $format Format used to format the date
|
||||
@@ -142,36 +115,6 @@ class DatePeriodTest extends BaseTestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides incorrect period
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideIncorrectPeriod()
|
||||
{
|
||||
yield[-1];
|
||||
yield[0];
|
||||
yield[10];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides period to verify
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function providePeriod()
|
||||
{
|
||||
yield[DatePeriod::LAST_WEEK];
|
||||
yield[DatePeriod::THIS_WEEK];
|
||||
yield[DatePeriod::NEXT_WEEK];
|
||||
yield[DatePeriod::LAST_MONTH];
|
||||
yield[DatePeriod::THIS_MONTH];
|
||||
yield[DatePeriod::NEXT_MONTH];
|
||||
yield[DatePeriod::LAST_YEAR];
|
||||
yield[DatePeriod::THIS_YEAR];
|
||||
yield[DatePeriod::NEXT_YEAR];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides period and incorrect format of date to verify
|
||||
*
|
||||
@@ -270,4 +213,68 @@ class DatePeriodTest extends BaseTestCase
|
||||
'2002-02-02 00:00',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all expected types of the tested type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllExpectedTypes()
|
||||
{
|
||||
return [
|
||||
'LAST_MONTH' => DatePeriod::LAST_MONTH,
|
||||
'LAST_WEEK' => DatePeriod::LAST_WEEK,
|
||||
'LAST_YEAR' => DatePeriod::LAST_YEAR,
|
||||
'NEXT_MONTH' => DatePeriod::NEXT_MONTH,
|
||||
'NEXT_WEEK' => DatePeriod::NEXT_WEEK,
|
||||
'NEXT_YEAR' => DatePeriod::NEXT_YEAR,
|
||||
'THIS_MONTH' => DatePeriod::THIS_MONTH,
|
||||
'THIS_WEEK' => DatePeriod::THIS_WEEK,
|
||||
'THIS_YEAR' => DatePeriod::THIS_YEAR,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getTestedTypeInstance()
|
||||
{
|
||||
return new DatePeriod();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function provideTypeToVerify()
|
||||
{
|
||||
yield[
|
||||
'',
|
||||
false,
|
||||
];
|
||||
|
||||
yield[
|
||||
-1,
|
||||
false,
|
||||
];
|
||||
|
||||
yield[
|
||||
true,
|
||||
false,
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePeriod::LAST_MONTH,
|
||||
true,
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePeriod::NEXT_WEEK,
|
||||
true,
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePeriod::THIS_YEAR,
|
||||
true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -88,6 +88,6 @@ class ComposerTest extends BaseTestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->composerJsonPath = $this->getFilePathToTests(Composer::FILE_NAME_MAIN);
|
||||
$this->composerJsonPath = $this->getFilePathForTesting(Composer::FILE_NAME_MAIN);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ use DateTime;
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\DatePeriod;
|
||||
use Meritoo\Common\Utilities\Date;
|
||||
|
||||
/**
|
||||
@@ -223,7 +224,7 @@ class DateTest extends BaseTestCase
|
||||
*/
|
||||
public function testGetDayOfWeekIncorrectValues($year, $month, $day)
|
||||
{
|
||||
$this->expectException(UnknownDatePartTypeException::class);
|
||||
$this->setExpectedException(UnknownDatePartTypeException::class);
|
||||
self::assertEmpty(Date::getDayOfWeek($year, $month, $day));
|
||||
}
|
||||
|
||||
@@ -506,6 +507,35 @@ class DateTest extends BaseTestCase
|
||||
self::assertTrue($randomDate >= $intervalMinDate && $randomDate <= $intervalMaxDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $period Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
*/
|
||||
public function testGetDatesForPeriodUsingEmptyPeriod($period)
|
||||
{
|
||||
self::assertNull(Date::getDatesForPeriod($period));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $period Incorrect period to verify
|
||||
* @dataProvider provideIncorrectPeriod
|
||||
*/
|
||||
public function testGetDatesForPeriodUsingIncorrectPeriod($period)
|
||||
{
|
||||
self::assertNull(Date::getDatesForPeriod($period));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $period The period, type of period. One of DatePeriod class constants, e.g. DatePeriod::LAST_WEEK.
|
||||
* @param DatePeriod $expected Expected start and end date for given period
|
||||
*
|
||||
* @dataProvider provideCorrectPeriod
|
||||
*/
|
||||
public function testGetDatesForPeriod($period, DatePeriod $expected)
|
||||
{
|
||||
self::assertEquals($expected, Date::getDatesForPeriod($period));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides incorrect invalidCount of DateTime
|
||||
*
|
||||
@@ -758,4 +788,112 @@ class DateTest extends BaseTestCase
|
||||
50,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides incorrect period
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideIncorrectPeriod()
|
||||
{
|
||||
yield[-1];
|
||||
yield[0];
|
||||
yield[10];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides correct period
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideCorrectPeriod()
|
||||
{
|
||||
yield[
|
||||
DatePeriod::LAST_WEEK,
|
||||
new DatePeriod(
|
||||
(new DateTime('this week'))->sub(new DateInterval('P7D'))->setTime(0, 0, 0),
|
||||
(new DateTime('this week'))->sub(new DateInterval('P1D'))->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePeriod::THIS_WEEK,
|
||||
new DatePeriod(
|
||||
(new DateTime('this week'))->setTime(0, 0, 0),
|
||||
(new DateTime('this week'))->add(new DateInterval('P6D'))->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePeriod::NEXT_WEEK,
|
||||
new DatePeriod(
|
||||
(new DateTime('this week'))->add(new DateInterval('P7D'))->setTime(0, 0, 0),
|
||||
(new DateTime('this week'))->add(new DateInterval('P7D'))->add(new DateInterval('P6D'))->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePeriod::LAST_MONTH,
|
||||
new DatePeriod(
|
||||
(new DateTime('first day of last month'))->setTime(0, 0, 0),
|
||||
(new DateTime('last day of last month'))->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePeriod::THIS_MONTH,
|
||||
new DatePeriod(
|
||||
Date::getDatesForPeriod(DatePeriod::LAST_MONTH)
|
||||
->getEndDate()
|
||||
->add(new DateInterval('P1D'))
|
||||
->setTime(0, 0, 0),
|
||||
Date::getDatesForPeriod(DatePeriod::NEXT_MONTH)
|
||||
->getStartDate()
|
||||
->sub(new DateInterval('P1D'))
|
||||
->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePeriod::NEXT_MONTH,
|
||||
new DatePeriod(
|
||||
(new DateTime('first day of next month'))->setTime(0, 0, 0),
|
||||
(new DateTime('last day of next month'))->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
|
||||
$lastYearStart = (new DateTime())->modify('-1 year');
|
||||
$lastYearEnd = (new DateTime())->modify('-1 year');
|
||||
$year = $lastYearStart->format('Y');
|
||||
|
||||
yield[
|
||||
DatePeriod::LAST_YEAR,
|
||||
new DatePeriod(
|
||||
$lastYearStart->setDate($year, 1, 1)->setTime(0, 0, 0),
|
||||
$lastYearEnd->setDate($year, 12, 31)->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
|
||||
$year = (new DateTime())->format('Y');
|
||||
|
||||
yield[
|
||||
DatePeriod::THIS_YEAR,
|
||||
new DatePeriod(
|
||||
(new DateTime())->setDate($year, 1, 1)->setTime(0, 0, 0),
|
||||
(new DateTime())->setDate($year, 12, 31)->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
|
||||
$nextYearStart = (new DateTime())->modify('1 year');
|
||||
$nextYearEnd = (new DateTime())->modify('1 year');
|
||||
$year = $nextYearStart->format('Y');
|
||||
|
||||
yield[
|
||||
DatePeriod::NEXT_YEAR,
|
||||
new DatePeriod(
|
||||
$nextYearStart->setDate($year, 1, 1)->setTime(0, 0, 0),
|
||||
$nextYearEnd->setDate($year, 12, 31)->setTime(23, 59, 59)
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -447,12 +447,12 @@ class MimeTypesTest extends BaseTestCase
|
||||
public function provideFilePathToGetMimeTypeOfRealFile()
|
||||
{
|
||||
yield[
|
||||
$this->getFilePathToTests('minion.jpg'),
|
||||
$this->getFilePathForTesting('minion.jpg'),
|
||||
'image/jpeg',
|
||||
];
|
||||
|
||||
yield[
|
||||
$this->getFilePathToTests('lorem-ipsum.txt'),
|
||||
$this->getFilePathForTesting('lorem-ipsum.txt'),
|
||||
'text/plain',
|
||||
];
|
||||
}
|
||||
@@ -465,12 +465,12 @@ class MimeTypesTest extends BaseTestCase
|
||||
public function provideExistingFilePathToCheckIsImagePath()
|
||||
{
|
||||
yield[
|
||||
$this->getFilePathToTests('minion.jpg'),
|
||||
$this->getFilePathForTesting('minion.jpg'),
|
||||
true,
|
||||
];
|
||||
|
||||
yield[
|
||||
$this->getFilePathToTests('lorem-ipsum.txt'),
|
||||
$this->getFilePathForTesting('lorem-ipsum.txt'),
|
||||
false,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -663,7 +663,8 @@ class MiscellaneousTest extends BaseTestCase
|
||||
|
||||
public function testGetInvertedColorWithIncorrectLength()
|
||||
{
|
||||
$this->expectException(IncorrectColorHexLengthException::class);
|
||||
$this->setExpectedException(IncorrectColorHexLengthException::class);
|
||||
|
||||
Miscellaneous::getInvertedColor(null);
|
||||
Miscellaneous::getInvertedColor('');
|
||||
Miscellaneous::getInvertedColor(1);
|
||||
@@ -676,7 +677,8 @@ class MiscellaneousTest extends BaseTestCase
|
||||
|
||||
public function testGetInvertedColorWithInvalidValue()
|
||||
{
|
||||
$this->expectException(InvalidColorHexValueException::class);
|
||||
$this->setExpectedException(InvalidColorHexValueException::class);
|
||||
|
||||
Miscellaneous::getInvertedColor('0011zz');
|
||||
Miscellaneous::getInvertedColor('001#zz');
|
||||
Miscellaneous::getInvertedColor('001!zz');
|
||||
|
||||
341
tests/Utilities/QueryBuilderUtilityTest.php
Normal file
341
tests/Utilities/QueryBuilderUtilityTest.php
Normal file
@@ -0,0 +1,341 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Query\Expr;
|
||||
use Doctrine\ORM\Query\Parameter;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Utilities\QueryBuilderUtility;
|
||||
|
||||
/**
|
||||
* Test case of the useful methods for query builder (the Doctrine's QueryBuilder class)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class QueryBuilderUtilityTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(QueryBuilderUtility::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryBuilder $queryBuilder The query builder to retrieve root alias
|
||||
* @param null|string $rootAlias Expected root alias of given query builder
|
||||
*
|
||||
* @dataProvider provideQueryBuilderAndRootAlias
|
||||
*/
|
||||
public function testGetRootAlias(QueryBuilder $queryBuilder, $rootAlias)
|
||||
{
|
||||
static::assertSame($rootAlias, QueryBuilderUtility::getRootAlias($queryBuilder));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryBuilder $queryBuilder The query builder to verify
|
||||
* @param string $propertyName Name of property that maybe is joined
|
||||
* @param null|string $propertyAlias Expected alias of given property joined in given query builder
|
||||
*
|
||||
* @dataProvider provideQueryBuilderAndPropertyAlias
|
||||
*/
|
||||
public function testGetJoinedPropertyAlias(QueryBuilder $queryBuilder, $propertyName, $propertyAlias)
|
||||
{
|
||||
static::assertSame($propertyAlias, QueryBuilderUtility::getJoinedPropertyAlias($queryBuilder, $propertyName));
|
||||
}
|
||||
|
||||
public function testSetCriteriaWithoutCriteria()
|
||||
{
|
||||
$entityManager = $this->getMock(EntityManagerInterface::class);
|
||||
$queryBuilder = new QueryBuilder($entityManager);
|
||||
$newQueryBuilder = QueryBuilderUtility::setCriteria($queryBuilder);
|
||||
|
||||
static::assertSame($queryBuilder, $newQueryBuilder);
|
||||
static::assertCount(0, $newQueryBuilder->getParameters());
|
||||
static::assertNull($newQueryBuilder->getDQLPart('where'));
|
||||
}
|
||||
|
||||
public function testSetCriteriaWithoutAlias()
|
||||
{
|
||||
$criteria = [
|
||||
'lorem' => 11,
|
||||
'ipsum' => 22,
|
||||
];
|
||||
|
||||
$entityManager = $this->getMock(EntityManagerInterface::class);
|
||||
$queryBuilder = new QueryBuilder($entityManager);
|
||||
$newQueryBuilder = QueryBuilderUtility::setCriteria($queryBuilder, $criteria);
|
||||
|
||||
static::assertSame($queryBuilder, $newQueryBuilder);
|
||||
static::assertCount(count($criteria), $newQueryBuilder->getParameters());
|
||||
static::assertNotNull($newQueryBuilder->getDQLPart('where'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryBuilder $queryBuilder The query builder
|
||||
* @param array $criteria The criteria used in WHERE clause
|
||||
*
|
||||
* @dataProvider provideQueryBuilderAndCriteria
|
||||
*/
|
||||
public function testSetCriteria(QueryBuilder $queryBuilder, array $criteria)
|
||||
{
|
||||
$newQueryBuilder = QueryBuilderUtility::setCriteria($queryBuilder, $criteria);
|
||||
$criteriaCount = count($criteria);
|
||||
$nullsCount = 0;
|
||||
|
||||
/*
|
||||
* I have to verify count/amount of NULLs and decrease $criteriaCount, because for null parameter is not added
|
||||
*/
|
||||
array_walk($criteria, function ($value) use (&$nullsCount) {
|
||||
if (null === $value) {
|
||||
++$nullsCount;
|
||||
}
|
||||
});
|
||||
|
||||
static::assertSame($queryBuilder, $newQueryBuilder);
|
||||
static::assertCount($criteriaCount - $nullsCount, $newQueryBuilder->getParameters());
|
||||
static::assertNotNull($newQueryBuilder->getDQLPart('where'));
|
||||
}
|
||||
|
||||
public function testDeleteEntitiesWithoutFlush()
|
||||
{
|
||||
$methods = [
|
||||
'remove',
|
||||
'flush',
|
||||
];
|
||||
|
||||
$entityManager = $this->getMock(EntityManager::class, $methods, [], '', false);
|
||||
$entities1 = [];
|
||||
|
||||
$entities2 = [
|
||||
new \stdClass(),
|
||||
];
|
||||
|
||||
static::assertFalse(QueryBuilderUtility::deleteEntities($entityManager, $entities1, false));
|
||||
static::assertTrue(QueryBuilderUtility::deleteEntities($entityManager, $entities2, false));
|
||||
}
|
||||
|
||||
public function testDeleteEntities()
|
||||
{
|
||||
$methods = [
|
||||
'remove',
|
||||
'flush',
|
||||
];
|
||||
|
||||
$entityManager = $this->getMock(EntityManager::class, $methods, [], '', false);
|
||||
$entities1 = [];
|
||||
|
||||
$entities2 = [
|
||||
new \stdClass(),
|
||||
];
|
||||
|
||||
static::assertFalse(QueryBuilderUtility::deleteEntities($entityManager, $entities1));
|
||||
static::assertTrue(QueryBuilderUtility::deleteEntities($entityManager, $entities2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryBuilder $queryBuilder The query builder
|
||||
* @param array|ArrayCollection $parameters Parameters to add. Collection of Doctrine\ORM\Query\Parameter
|
||||
* instances or an array with key-value pairs.
|
||||
*
|
||||
* @dataProvider provideQueryBuilderAndParameters
|
||||
*/
|
||||
public function testAddParameters(QueryBuilder $queryBuilder, $parameters)
|
||||
{
|
||||
$newQueryBuilder = QueryBuilderUtility::addParameters($queryBuilder, $parameters);
|
||||
|
||||
static::assertSame($queryBuilder, $newQueryBuilder);
|
||||
static::assertCount(count($parameters), $newQueryBuilder->getParameters());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides query builder to retrieve root alias and expected root alias
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideQueryBuilderAndRootAlias()
|
||||
{
|
||||
$entityManager = $this->getMock(EntityManagerInterface::class);
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
null,
|
||||
];
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))->from('lorem_ipsum', 'lm'),
|
||||
'lm',
|
||||
];
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))
|
||||
->from('lorem', 'l')
|
||||
->leftJoin('l.ipsum', 'i'),
|
||||
'l',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides query builder, name of property and expected alias of given property
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideQueryBuilderAndPropertyAlias()
|
||||
{
|
||||
$entityManager = $this->getMock(EntityManagerInterface::class);
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
'',
|
||||
null,
|
||||
];
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
'lorem',
|
||||
null,
|
||||
];
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))->from('lorem_ipsum', 'lm'),
|
||||
'lm',
|
||||
null,
|
||||
];
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))
|
||||
->from('lorem', 'l')
|
||||
->leftJoin('l.ipsum', 'i'),
|
||||
'ipsum',
|
||||
'i',
|
||||
];
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))
|
||||
->from('lorem', 'l')
|
||||
->leftJoin('l.ipsum', 'i')
|
||||
->innerJoin('i.dolor', 'd'),
|
||||
'ipsum1',
|
||||
null,
|
||||
];
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))
|
||||
->from('lorem', 'l')
|
||||
->leftJoin('l.ipsum', 'i')
|
||||
->innerJoin('i.dolor', 'd'),
|
||||
'ipsum',
|
||||
'i',
|
||||
];
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))
|
||||
->from('lorem', 'l')
|
||||
->leftJoin('l.ipsum', 'i')
|
||||
->innerJoin('i.dolor', 'd'),
|
||||
'dolor',
|
||||
'd',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides query builder and criteria used in WHERE clause
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideQueryBuilderAndCriteria()
|
||||
{
|
||||
$entityManager = $this->getMock(EntityManager::class, ['getExpressionBuilder'], [], '', false);
|
||||
|
||||
$entityManager
|
||||
->expects(static::any())
|
||||
->method('getExpressionBuilder')
|
||||
->willReturn(new Expr());
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))->from('lorem_ipsum', 'lm'),
|
||||
[
|
||||
'lorem' => 11,
|
||||
'ipsum' => 22,
|
||||
'dolor' => null,
|
||||
],
|
||||
];
|
||||
|
||||
yield[
|
||||
(new QueryBuilder($entityManager))->from('lorem_ipsum', 'lm'),
|
||||
[
|
||||
'lorem' => [
|
||||
11,
|
||||
'>=',
|
||||
],
|
||||
'ipsum' => [
|
||||
22,
|
||||
'<',
|
||||
],
|
||||
'dolor' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides query builder and parameters to add to given query builder
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideQueryBuilderAndParameters()
|
||||
{
|
||||
$entityManager = $this->getMock(EntityManagerInterface::class);
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
[],
|
||||
];
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
new ArrayCollection(),
|
||||
];
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
[
|
||||
'lorem' => 11,
|
||||
'ipsum' => 22,
|
||||
],
|
||||
];
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
new ArrayCollection([
|
||||
'lorem' => 11,
|
||||
'ipsum' => 22,
|
||||
]),
|
||||
];
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
[
|
||||
new Parameter('lorem', 11),
|
||||
new Parameter('ipsum', 22),
|
||||
],
|
||||
];
|
||||
|
||||
yield[
|
||||
new QueryBuilder($entityManager),
|
||||
new ArrayCollection([
|
||||
new Parameter('lorem', 11),
|
||||
new Parameter('ipsum', 22),
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ namespace Meritoo\Common\Test\Utilities\Reflection;
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class B extends A
|
||||
class B extends A implements I
|
||||
{
|
||||
protected $name = 'Lorem Ipsum';
|
||||
|
||||
|
||||
27
tests/Utilities/Reflection/H.php
Normal file
27
tests/Utilities/Reflection/H.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\Utilities\Reflection;
|
||||
|
||||
/**
|
||||
* The H class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class H
|
||||
{
|
||||
const DOLOR = 'sit';
|
||||
|
||||
const LOREM = 'ipsum';
|
||||
|
||||
const MAX_USERS = 5;
|
||||
|
||||
const MIN_USERS = 2;
|
||||
}
|
||||
20
tests/Utilities/Reflection/I.php
Normal file
20
tests/Utilities/Reflection/I.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\Utilities\Reflection;
|
||||
|
||||
/**
|
||||
* The H interface.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
interface I
|
||||
{
|
||||
}
|
||||
@@ -22,6 +22,8 @@ use Meritoo\Common\Test\Utilities\Reflection\D;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\E;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\F;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\G;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\H;
|
||||
use Meritoo\Common\Test\Utilities\Reflection\I;
|
||||
use Meritoo\Common\Utilities\Reflection;
|
||||
use ReflectionProperty;
|
||||
|
||||
@@ -122,7 +124,7 @@ class ReflectionTest extends BaseTestCase
|
||||
*/
|
||||
public function testGetChildClassesInvalidClass($invalidClass)
|
||||
{
|
||||
$this->expectException(CannotResolveClassNameException::class);
|
||||
$this->setExpectedException(CannotResolveClassNameException::class);
|
||||
|
||||
self::assertNull(Reflection::getChildClasses($invalidClass));
|
||||
self::assertNull(Reflection::getChildClasses(123));
|
||||
@@ -130,7 +132,7 @@ class ReflectionTest extends BaseTestCase
|
||||
|
||||
public function testGetChildClassesNotExistingClass()
|
||||
{
|
||||
$this->expectException(CannotResolveClassNameException::class);
|
||||
$this->setExpectedException(CannotResolveClassNameException::class);
|
||||
self::assertEquals('', Reflection::getChildClasses('xyz'));
|
||||
}
|
||||
|
||||
@@ -161,13 +163,13 @@ class ReflectionTest extends BaseTestCase
|
||||
|
||||
public function testGetOneChildClassWithMissingChildClasses()
|
||||
{
|
||||
$this->expectException(MissingChildClassesException::class);
|
||||
$this->setExpectedException(MissingChildClassesException::class);
|
||||
self::assertEquals('LoremIpsum', Reflection::getOneChildClass(C::class));
|
||||
}
|
||||
|
||||
public function testGetOneChildClassWithTooManyChildClasses()
|
||||
{
|
||||
$this->expectException(TooManyChildClassesException::class);
|
||||
$this->setExpectedException(TooManyChildClassesException::class);
|
||||
|
||||
self::assertEquals(B::class, Reflection::getOneChildClass(A::class));
|
||||
self::assertEquals(C::class, Reflection::getOneChildClass(A::class));
|
||||
@@ -195,7 +197,7 @@ class ReflectionTest extends BaseTestCase
|
||||
*/
|
||||
public function testUsesTraitInvalidClass($class, $trait)
|
||||
{
|
||||
$this->expectException(CannotResolveClassNameException::class);
|
||||
$this->setExpectedException(CannotResolveClassNameException::class);
|
||||
self::assertNull(Reflection::usesTrait($class, $trait));
|
||||
}
|
||||
|
||||
@@ -205,7 +207,7 @@ class ReflectionTest extends BaseTestCase
|
||||
*/
|
||||
public function testUsesTraitInvalidTrait($trait)
|
||||
{
|
||||
$this->expectException(CannotResolveClassNameException::class);
|
||||
$this->setExpectedException(CannotResolveClassNameException::class);
|
||||
self::assertNull(Reflection::usesTrait(DateTime::class, $trait));
|
||||
}
|
||||
|
||||
@@ -379,6 +381,99 @@ class ReflectionTest extends BaseTestCase
|
||||
self::assertEquals($expected, Reflection::getPropertyValues($collection, 'gInstance.firstName', true));
|
||||
}
|
||||
|
||||
public function testGetMaxNumberConstantUsingClassWithoutConstants()
|
||||
{
|
||||
static::assertNull(Reflection::getMaxNumberConstant(A::class));
|
||||
}
|
||||
|
||||
public function testGetMaxNumberConstant()
|
||||
{
|
||||
static::assertEquals(5, Reflection::getMaxNumberConstant(H::class));
|
||||
}
|
||||
|
||||
public function testHasMethodUsingClassWithoutMethod()
|
||||
{
|
||||
static::assertFalse(Reflection::hasMethod(A::class, 'getUser'));
|
||||
}
|
||||
|
||||
public function testHasMethod()
|
||||
{
|
||||
static::assertTrue(Reflection::hasMethod(A::class, 'getCount'));
|
||||
}
|
||||
|
||||
public function testHasPropertyUsingClassWithoutProperty()
|
||||
{
|
||||
static::assertFalse(Reflection::hasProperty(A::class, 'users'));
|
||||
}
|
||||
|
||||
public function testHasProperty()
|
||||
{
|
||||
static::assertTrue(Reflection::hasProperty(A::class, 'count'));
|
||||
}
|
||||
|
||||
public function testHasConstantUsingClassWithoutConstant()
|
||||
{
|
||||
static::assertFalse(Reflection::hasConstant(H::class, 'users'));
|
||||
}
|
||||
|
||||
public function testHasConstant()
|
||||
{
|
||||
static::assertTrue(Reflection::hasConstant(H::class, 'LOREM'));
|
||||
}
|
||||
|
||||
public function testGetConstantValueUsingClassWithoutConstant()
|
||||
{
|
||||
static::assertNull(Reflection::getConstantValue(H::class, 'users'));
|
||||
}
|
||||
|
||||
public function testGetConstantValue()
|
||||
{
|
||||
static::assertEquals(H::LOREM, Reflection::getConstantValue(H::class, 'LOREM'));
|
||||
}
|
||||
|
||||
public function testIsInterfaceImplementedUsingClassWithoutInterface()
|
||||
{
|
||||
static::assertFalse(Reflection::isInterfaceImplemented(A::class, I::class));
|
||||
}
|
||||
|
||||
public function testIsInterfaceImplemented()
|
||||
{
|
||||
static::assertTrue(Reflection::isInterfaceImplemented(B::class, I::class));
|
||||
}
|
||||
|
||||
public function testIsChildOfClassUsingClassWithoutChildClass()
|
||||
{
|
||||
static::assertFalse(Reflection::isChildOfClass(A::class, B::class));
|
||||
}
|
||||
|
||||
public function testIsChildOfClass()
|
||||
{
|
||||
static::assertTrue(Reflection::isChildOfClass(B::class, A::class));
|
||||
}
|
||||
|
||||
public function testGetPropertyUsingClassWithoutProperty()
|
||||
{
|
||||
static::assertNull(Reflection::getProperty(A::class, 'lorem'));
|
||||
}
|
||||
|
||||
public function testGetPropertyUsingClassWithPrivateProperty()
|
||||
{
|
||||
$property = Reflection::getProperty(A::class, 'count', ReflectionProperty::IS_PRIVATE);
|
||||
|
||||
static::assertInstanceOf(ReflectionProperty::class, $property);
|
||||
static::assertTrue($property->isPrivate());
|
||||
static::assertEquals('count', $property->getName());
|
||||
}
|
||||
|
||||
public function testGetPropertyUsingClassWithProtectedProperty()
|
||||
{
|
||||
$property = Reflection::getProperty(B::class, 'name', ReflectionProperty::IS_PROTECTED);
|
||||
|
||||
static::assertInstanceOf(ReflectionProperty::class, $property);
|
||||
static::assertTrue($property->isProtected());
|
||||
static::assertEquals('name', $property->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides invalid class and trait
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user