mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Compare commits
68 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8bcf006e02 | ||
|
|
edc51aeee1 | ||
|
|
6d4e422165 | ||
|
|
6f441bb9ea | ||
|
|
73030d703b | ||
|
|
36ddb326b9 | ||
|
|
452a4ec458 | ||
|
|
325fe6b141 | ||
|
|
a1c26b3812 | ||
|
|
67d93036cf | ||
|
|
9368616dfe | ||
|
|
5ab68d3667 | ||
|
|
4613a63f02 | ||
|
|
9dac5bd11c | ||
|
|
12100db058 | ||
|
|
f9ab0a6194 | ||
|
|
b824808cd4 | ||
|
|
71e1eeb81b | ||
|
|
70c273750d | ||
|
|
e5e39651f3 | ||
|
|
4683970c87 | ||
|
|
559466c0ce | ||
|
|
bfd69c1098 | ||
|
|
45493b37b0 | ||
|
|
37e7b14ae2 | ||
|
|
a12aaf4bc0 | ||
|
|
f9c480aa19 | ||
|
|
ffa3fbffe7 | ||
|
|
48aa27fb86 | ||
|
|
86cc5ff79b | ||
|
|
633696ebc0 | ||
|
|
a0d28b326e | ||
|
|
318a635ffd | ||
|
|
6c70fdd673 | ||
|
|
204e8793ac | ||
|
|
3dd37ae202 | ||
|
|
ef017c9d6a | ||
|
|
5030dc2062 | ||
|
|
2c76158093 | ||
|
|
0b560fdf18 | ||
|
|
284d403061 | ||
|
|
7dbb3f9b2e | ||
|
|
fba821b798 | ||
|
|
3985c70076 | ||
|
|
921d4e6106 | ||
|
|
7aa2239dbd | ||
|
|
e1fefcdeae | ||
|
|
94a464cb4d | ||
|
|
463ee751b2 | ||
|
|
89af7145f6 | ||
|
|
09c8569938 | ||
|
|
5ab2cd9de8 | ||
|
|
60eff29e82 | ||
|
|
5aa5ff4380 | ||
|
|
6483a8f5b7 | ||
|
|
5c0ef79b15 | ||
|
|
324f64f912 | ||
|
|
5940ebba9a | ||
|
|
787b8c697c | ||
|
|
96c02cdc3d | ||
|
|
87d7bff5f5 | ||
|
|
bd7c874e88 | ||
|
|
e1ffb78214 | ||
|
|
d4dc274763 | ||
|
|
aeea1df04b | ||
|
|
b5e63ff619 | ||
|
|
ecaebcca0e | ||
|
|
17ac1dc72c |
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
64
.docker/config/Dockerfile
Normal file
64
.docker/config/Dockerfile
Normal file
@@ -0,0 +1,64 @@
|
||||
FROM php:5.5-cli
|
||||
|
||||
#
|
||||
# Tools & libraries
|
||||
#
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
vim \
|
||||
git \
|
||||
zip \
|
||||
unzip \
|
||||
zlib1g-dev \
|
||||
libicu-dev \
|
||||
&& apt-get clean \
|
||||
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
||||
|
||||
#
|
||||
# PHP extensions
|
||||
#
|
||||
RUN docker-php-ext-install \
|
||||
zip \
|
||||
intl \
|
||||
mbstring
|
||||
|
||||
#
|
||||
# PHP configuration:
|
||||
# - default configuration
|
||||
# - timezone
|
||||
#
|
||||
COPY php.ini /usr/local/etc/php/php.ini
|
||||
ARG TIMEZONE
|
||||
RUN echo "\n""date.timezone = $TIMEZONE""\n" >> /usr/local/etc/php/php.ini
|
||||
|
||||
#
|
||||
# Xdebug
|
||||
#
|
||||
RUN pecl install xdebug \
|
||||
&& docker-php-ext-enable xdebug
|
||||
COPY xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
|
||||
|
||||
#
|
||||
# Phing
|
||||
#
|
||||
RUN pear channel-discover pear.phing.info \
|
||||
&& pear install [--alldeps] phing/phing
|
||||
|
||||
#
|
||||
# Composer + https://packagist.org/packages/hirak/prestissimo package
|
||||
#
|
||||
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
|
||||
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === \
|
||||
'544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo \
|
||||
'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
|
||||
&& php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
|
||||
&& 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
|
||||
6
.docker/config/xdebug.ini
Normal file
6
.docker/config/xdebug.ini
Normal file
@@ -0,0 +1,6 @@
|
||||
[xdebug]
|
||||
zend_extension=xdebug.so
|
||||
|
||||
xdebug.remote_enable=1
|
||||
xdebug.remote_port=9001
|
||||
xdebug.remote_host=10.254.254.254
|
||||
66
.gitignore
vendored
66
.gitignore
vendored
@@ -7,13 +7,14 @@
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
### Composer
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
/composer.lock
|
||||
/composer.phar
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
### Phing
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
/phing/properties
|
||||
/.phing/properties
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
@@ -28,10 +29,16 @@
|
||||
/.php_cs.cache
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
### Build files
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
/.build/
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
### Generated databases
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
/data/tmp
|
||||
/.data/tmp
|
||||
*.sql
|
||||
*.sqlite
|
||||
|
||||
@@ -54,60 +61,9 @@
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
### JetBrains template
|
||||
### JetBrains
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff:
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/dictionaries
|
||||
.idea/vcs.xml
|
||||
.idea/jsLibraryMappings.xml
|
||||
|
||||
# Sensitive or high-churn files:
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.xml
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# CMake
|
||||
cmake-build-debug/
|
||||
|
||||
# Mongo Explorer plugin:
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
## File-based project format:
|
||||
*.iws
|
||||
|
||||
## Plugin-specific files:
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
/.idea
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
1
.idea/.name
generated
1
.idea/.name
generated
@@ -1 +0,0 @@
|
||||
Common Library (GitHub)
|
||||
210
.idea/Common Library (GitHub).iml
generated
210
.idea/Common Library (GitHub).iml
generated
@@ -1,210 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests/Meritoo/Common/Tests" isTestSource="false" packagePrefix="Meritoo\Common\Tests\" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/Meritoo/Common" isTestSource="false" packagePrefix="Meritoo\Common\" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/behat/transliterator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/annotations" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/cache" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/collections" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/common" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/dbal" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/inflector" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/lexer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/orm" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/gecko-packages/gecko-php-unit" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/gedmo/doctrine-extensions" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/paragonie/random_compat" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/pdepend/pdepend" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-common" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-docblock" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/type-resolver" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phploc/phploc" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpmd/phpmd" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpspec/prophecy" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-file-iterator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-text-template" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-timer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/php-token-stream" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpunit/phpunit-mock-objects" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/container" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/log" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/comparator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/diff" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/environment" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/exporter" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/finder-facade" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/git" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/global-state" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/object-enumerator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/phpcpd" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/resource-operations" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/version" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/squizlabs/php_codesniffer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/config" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/console" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/debug" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/dependency-injection" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/event-dispatcher" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/filesystem" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/finder" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/http-foundation" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/options-resolver" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-mbstring" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-php70" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-php72" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/process" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/stopwatch" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/yaml" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/theseer/fdomdocument" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/webmozart/assert" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module-library">
|
||||
<library name="PHP" type="php">
|
||||
<CLASSES>
|
||||
<root url="file://$MODULE_DIR$/vendor/behat/transliterator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/composer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/annotations" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/cache" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/collections" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/common" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/dbal" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/inflector" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/lexer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/orm" />
|
||||
<root url="file://$MODULE_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/gecko-packages/gecko-php-unit" />
|
||||
<root url="file://$MODULE_DIR$/vendor/gedmo/doctrine-extensions" />
|
||||
<root url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
|
||||
<root url="file://$MODULE_DIR$/vendor/paragonie/random_compat" />
|
||||
<root url="file://$MODULE_DIR$/vendor/pdepend/pdepend" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-common" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-docblock" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpdocumentor/type-resolver" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phploc/phploc" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpmd/phpmd" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpspec/prophecy" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-file-iterator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-text-template" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-timer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-token-stream" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/phpunit-mock-objects" />
|
||||
<root url="file://$MODULE_DIR$/vendor/psr/container" />
|
||||
<root url="file://$MODULE_DIR$/vendor/psr/log" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/comparator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/diff" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/environment" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/exporter" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/finder-facade" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/git" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/global-state" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/object-enumerator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/phpcpd" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/resource-operations" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/version" />
|
||||
<root url="file://$MODULE_DIR$/vendor/squizlabs/php_codesniffer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/config" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/console" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/debug" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/dependency-injection" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/event-dispatcher" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/filesystem" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/finder" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/http-foundation" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/options-resolver" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-mbstring" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php70" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php72" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/process" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/stopwatch" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/yaml" />
|
||||
<root url="file://$MODULE_DIR$/vendor/theseer/fdomdocument" />
|
||||
<root url="file://$MODULE_DIR$/vendor/webmozart/assert" />
|
||||
</CLASSES>
|
||||
<SOURCES>
|
||||
<root url="file://$MODULE_DIR$/vendor/behat/transliterator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/composer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/annotations" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/cache" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/collections" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/common" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/dbal" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/inflector" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/lexer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/doctrine/orm" />
|
||||
<root url="file://$MODULE_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/gecko-packages/gecko-php-unit" />
|
||||
<root url="file://$MODULE_DIR$/vendor/gedmo/doctrine-extensions" />
|
||||
<root url="file://$MODULE_DIR$/vendor/myclabs/deep-copy" />
|
||||
<root url="file://$MODULE_DIR$/vendor/paragonie/random_compat" />
|
||||
<root url="file://$MODULE_DIR$/vendor/pdepend/pdepend" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-common" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-docblock" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpdocumentor/type-resolver" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phploc/phploc" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpmd/phpmd" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpspec/prophecy" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-code-coverage" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-file-iterator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-text-template" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-timer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/php-token-stream" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/phpunit" />
|
||||
<root url="file://$MODULE_DIR$/vendor/phpunit/phpunit-mock-objects" />
|
||||
<root url="file://$MODULE_DIR$/vendor/psr/container" />
|
||||
<root url="file://$MODULE_DIR$/vendor/psr/log" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/comparator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/diff" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/environment" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/exporter" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/finder-facade" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/git" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/global-state" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/object-enumerator" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/phpcpd" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/resource-operations" />
|
||||
<root url="file://$MODULE_DIR$/vendor/sebastian/version" />
|
||||
<root url="file://$MODULE_DIR$/vendor/squizlabs/php_codesniffer" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/config" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/console" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/debug" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/dependency-injection" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/event-dispatcher" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/filesystem" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/finder" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/http-foundation" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/options-resolver" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-mbstring" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php70" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/polyfill-php72" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/process" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/stopwatch" />
|
||||
<root url="file://$MODULE_DIR$/vendor/symfony/yaml" />
|
||||
<root url="file://$MODULE_DIR$/vendor/theseer/fdomdocument" />
|
||||
<root url="file://$MODULE_DIR$/vendor/webmozart/assert" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
9
.idea/composerJson.xml
generated
9
.idea/composerJson.xml
generated
@@ -1,9 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ComposerJsonPluginSettings">
|
||||
<unboundedVersionInspectionSettings>
|
||||
<excludedPackages />
|
||||
</unboundedVersionInspectionSettings>
|
||||
<customRepositories />
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/encodings.xml
generated
6
.idea/encodings.xml
generated
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="PROJECT" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/Common Library (GitHub).iml" filepath="$PROJECT_DIR$/.idea/Common Library (GitHub).iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
75
.idea/php.xml
generated
75
.idea/php.xml
generated
@@ -1,75 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PhpIncludePathManager">
|
||||
<include_path>
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/inflector" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/annotations" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/lexer" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/common" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/cache" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/instantiator" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/collections" />
|
||||
<path value="$PROJECT_DIR$/vendor/composer" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpspec/prophecy" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/yaml" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/config" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-mbstring" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/stopwatch" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/http-foundation" />
|
||||
<path value="$PROJECT_DIR$/vendor/behat/transliterator" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/finder" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/filesystem" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/event-dispatcher" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php70" />
|
||||
<path value="$PROJECT_DIR$/vendor/theseer/fdomdocument" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/dependency-injection" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/debug" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php72" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/options-resolver" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpunit/php-code-coverage" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/process" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/console" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpunit/php-text-template" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpunit/php-timer" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpunit/phpunit-mock-objects" />
|
||||
<path value="$PROJECT_DIR$/vendor/gecko-packages/gecko-php-unit" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpunit/php-token-stream" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/finder-facade" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpunit/php-file-iterator" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/git" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/exporter" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/code-unit-reverse-lookup" />
|
||||
<path value="$PROJECT_DIR$/vendor/webmozart/assert" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/resource-operations" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/version" />
|
||||
<path value="$PROJECT_DIR$/vendor/psr/log" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/diff" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/environment" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/comparator" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/global-state" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/dbal" />
|
||||
<path value="$PROJECT_DIR$/vendor/psr/container" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/object-enumerator" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/recursion-context" />
|
||||
<path value="$PROJECT_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpdocumentor/type-resolver" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpunit/phpunit" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpdocumentor/reflection-docblock" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/orm" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpdocumentor/reflection-common" />
|
||||
<path value="$PROJECT_DIR$/vendor/pdepend/pdepend" />
|
||||
<path value="$PROJECT_DIR$/vendor/sebastian/phpcpd" />
|
||||
<path value="$PROJECT_DIR$/vendor/phploc/phploc" />
|
||||
<path value="$PROJECT_DIR$/vendor/squizlabs/php_codesniffer" />
|
||||
<path value="$PROJECT_DIR$/vendor/phpmd/phpmd" />
|
||||
<path value="$PROJECT_DIR$/vendor/paragonie/random_compat" />
|
||||
<path value="$PROJECT_DIR$/vendor/myclabs/deep-copy" />
|
||||
<path value="$PROJECT_DIR$/vendor/gedmo/doctrine-extensions" />
|
||||
</include_path>
|
||||
</component>
|
||||
<component name="PhpUnit">
|
||||
<phpunit_settings>
|
||||
<PhpUnitSettings load_method="CUSTOM_LOADER" custom_loader_path="$PROJECT_DIR$/vendor/autoload.php" phpunit_phar_path="" />
|
||||
</phpunit_settings>
|
||||
</component>
|
||||
</project>
|
||||
14
.idea/webResources.xml
generated
14
.idea/webResources.xml
generated
@@ -1,14 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="WebResourcesPaths">
|
||||
<contentEntries>
|
||||
<entry url="file://$PROJECT_DIR$">
|
||||
<entryData>
|
||||
<resourceRoots>
|
||||
<path value="file://$PROJECT_DIR$/data" />
|
||||
</resourceRoots>
|
||||
</entryData>
|
||||
</entry>
|
||||
</contentEntries>
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,13 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project name="Meritoo Common Library" basedir="." default="build:main" phingVersion="2.14.0">
|
||||
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
|
||||
<!-- Properties -->
|
||||
<if>
|
||||
<available file="phing/properties" property="custom.properties.available"/>
|
||||
<available file=".phing/properties" property="custom.properties.available"/>
|
||||
<then>
|
||||
<property file="phing/properties" />
|
||||
<property file=".phing/properties" />
|
||||
</then>
|
||||
<else>
|
||||
<property file="phing/properties.dist" />
|
||||
<property file=".phing/properties.dist" />
|
||||
</else>
|
||||
</if>
|
||||
|
||||
@@ -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
|
||||
@@ -59,7 +65,7 @@ composer.validate = false
|
||||
|
||||
# System directories
|
||||
#
|
||||
dir.data = ${project.basedir}/data
|
||||
dir.data = ${project.basedir}/.data
|
||||
dir.src = ${project.basedir}/src
|
||||
dir.tests = ${project.basedir}/tests
|
||||
|
||||
@@ -67,7 +73,7 @@ dir.tests = ${project.basedir}/tests
|
||||
# Build directories
|
||||
# --------------------------------------------------------------------------------
|
||||
|
||||
dir.build = ${project.basedir}/build
|
||||
dir.build = ${project.basedir}/.build
|
||||
dir.reports = ${dir.build}/logs
|
||||
dir.reports.pdepend = ${dir.reports}/pdepend
|
||||
dir.reports.coverage = ${dir.reports}/phpunit_coverage
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project name="Meritoo Common Library" basedir="." default="build:main" phingVersion="2.14.0">
|
||||
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
|
||||
<!--
|
||||
The AutoloaderTask is required to load binaries installed by Composer.
|
||||
The "autoloaderpath" attribute of this task is not required, because it's default value is: vendor/autoload.php.
|
||||
@@ -11,12 +11,12 @@
|
||||
|
||||
<!-- Properties -->
|
||||
<if>
|
||||
<available file="phing/properties" property="custom.properties.available"/>
|
||||
<available file=".phing/properties" property="custom.properties.available"/>
|
||||
<then>
|
||||
<property file="phing/properties" />
|
||||
<property file=".phing/properties" />
|
||||
</then>
|
||||
<else>
|
||||
<property file="phing/properties.dist" />
|
||||
<property file=".phing/properties.dist" />
|
||||
</else>
|
||||
</if>
|
||||
|
||||
8
.styleci.yml
Normal file
8
.styleci.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
preset: symfony
|
||||
|
||||
disabled:
|
||||
- phpdoc_annotation_without_dot
|
||||
- cast_spaces
|
||||
- concat_without_spaces
|
||||
- blank_line_before_return
|
||||
- trim_array_spaces
|
||||
12
.travis.yml
Normal file
12
.travis.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
language: php
|
||||
|
||||
php:
|
||||
- 5.6
|
||||
- 7.0
|
||||
- 7.1
|
||||
|
||||
install:
|
||||
- composer install
|
||||
|
||||
script:
|
||||
- php ./vendor/bin/phpunit
|
||||
120
README.md
120
README.md
@@ -1,38 +1,114 @@
|
||||
# Meritoo Common Library
|
||||
Useful classes, methods, extensions etc.
|
||||
Common and useful classes, methods, exceptions etc.
|
||||
|
||||
[](https://travis-ci.org/meritoo/common-library) [](https://packagist.org/packages/meritoo/common-library) [](https://styleci.io/repos/101790028) [](https://github.com/meritoo/common-library) [](https://github.com/meritoo/common-library) [](https://coveralls.io/github/meritoo/common-library?branch=master)
|
||||
|
||||
## Installation
|
||||
|
||||
#### Composer
|
||||
Run [Composer](https://getcomposer.org) to install this package in your project:
|
||||
|
||||
1. Open ```composer.json``` and add address of repository in ```repositories``` section:
|
||||
|
||||
```json
|
||||
"repositories": [
|
||||
{
|
||||
"type": "vcs",
|
||||
"url": "git@github.com:meritoo/common-library.git"
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
2. Run [Composer](https://getcomposer.org) to install new package:
|
||||
|
||||
```bash
|
||||
$ composer require meritoo/common-library
|
||||
```
|
||||
```bash
|
||||
$ composer require meritoo/common-library
|
||||
```
|
||||
|
||||
> How to install Composer: https://getcomposer.org/download
|
||||
|
||||
## Usage
|
||||
## Rebuilding project and tests running
|
||||
|
||||
This package contains a lot of static methods, so usage is not so complicated. Just run the static method who would you like to use. Example:
|
||||
```bash
|
||||
$ docker-compose up -d
|
||||
$ docker-compose exec php-cli phing
|
||||
```
|
||||
|
||||
> What is Docker? https://www.docker.com/what-docker
|
||||
|
||||
## Static methods
|
||||
|
||||
This package contains a lot of class with static methods, so usage is not so complicated. Just run the static method who would you like to use. Example:
|
||||
|
||||
```php
|
||||
use Meritoo\Common\Utilities\Arrays;
|
||||
|
||||
$firstElement = Arrays::getFirstElement(['lorem' 'ipsum']);
|
||||
// result: "lorem"
|
||||
$firstElement = Arrays::getFirstElement(['lorem', 'ipsum']);
|
||||
var_dump($firstElement); // string(5) "lorem"
|
||||
```
|
||||
|
||||
## Base test case with common methods and data providers
|
||||
|
||||
Located here: `Meritoo\Common\Test\Base\BaseTestCase`. Just extend the `BaseTestCase` class and use it like in `Meritoo\Common\Test\Utilities\DateTest` class:
|
||||
|
||||
```php
|
||||
class DateTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* @param mixed $value Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
*/
|
||||
public function testGetDateTimeEmptyValue($value)
|
||||
{
|
||||
self::assertFalse(Date::getDateTime($value));
|
||||
}
|
||||
|
||||
(...)
|
||||
}
|
||||
```
|
||||
|
||||
or in `Meritoo\Common\Test\Utilities\MimeTypesTest` class:
|
||||
|
||||
```php
|
||||
class MimeTypesTest extends BaseTestCase
|
||||
{
|
||||
(...)
|
||||
|
||||
/**
|
||||
* @param bool $mimeType The mime type, e.g. "video/mpeg"
|
||||
* @dataProvider provideBooleanValue
|
||||
*/
|
||||
public function testGetExtensionBooleanMimeType($mimeType)
|
||||
{
|
||||
self::assertEquals('', MimeTypes::getExtension($mimeType));
|
||||
}
|
||||
|
||||
(...)
|
||||
}
|
||||
```
|
||||
|
||||
## Collection of elements
|
||||
|
||||
Located here: `Meritoo\Common\Collection\Collection`. It's a set of some elements, e.g. objects. It's iterable and countable. Provides very useful methods. Some of them:
|
||||
- `getFirst()` - returns the first element in the collection
|
||||
- `getLast()` - returns the last element in the collection
|
||||
- `isEmpty()` - returns information if collection is empty
|
||||
- `add($element, $index = null)` - adds given element (at the end of collection)
|
||||
- `addMultiple($elements, $useIndexes = false)` - adds given elements (at the end of collection)
|
||||
- `prepend($element)` - prepends given element (adds given element at the beginning of collection)
|
||||
- `remove($element)` - removes given element
|
||||
|
||||
Examples of usage below.
|
||||
|
||||
#### An empty collection
|
||||
|
||||
```php
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
|
||||
$emptyCollection = new Collection();
|
||||
var_dump($emptyCollection->isEmpty()); // bool(true)
|
||||
```
|
||||
|
||||
#### Simple collection
|
||||
|
||||
```php
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
|
||||
$elements = [
|
||||
'lorem',
|
||||
'ipsum',
|
||||
123 => 'dolor',
|
||||
345 => 'sit',
|
||||
];
|
||||
|
||||
$simpleCollection = new Collection($elements);
|
||||
var_dump($simpleCollection->has('dolor')); // bool(true)
|
||||
```
|
||||
|
||||
Enjoy!
|
||||
|
||||
14
build.xml
14
build.xml
@@ -1,13 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project name="Meritoo Common Library" basedir="." default="build:main" phingVersion="2.14.0">
|
||||
<project name="Meritoo Package" basedir="." default="build:main" phingVersion="2.14.0">
|
||||
<!-- Properties -->
|
||||
<if>
|
||||
<available file="phing/properties" property="custom.properties.available"/>
|
||||
<available file=".phing/properties" property="custom.properties.available"/>
|
||||
<then>
|
||||
<property file="phing/properties" />
|
||||
<property file=".phing/properties" />
|
||||
</then>
|
||||
<else>
|
||||
<property file="phing/properties.dist" />
|
||||
<property file=".phing/properties.dist" />
|
||||
</else>
|
||||
</if>
|
||||
|
||||
@@ -18,12 +18,12 @@
|
||||
|
||||
<!-- Build app -->
|
||||
<target name="build:app" description="Prepares app to build and tests">
|
||||
<phing phingfile="phing/app.xml" haltonfailure="true" />
|
||||
<phing phingfile=".phing/app.xml" haltonfailure="true" />
|
||||
</target>
|
||||
|
||||
<!-- Build tests -->
|
||||
<target name="build:tests" description="Runs all tests, checks and creates docs">
|
||||
<phing phingfile="phing/tests.xml" haltonfailure="true" />
|
||||
<phing phingfile=".phing/tests.xml" haltonfailure="true" />
|
||||
|
||||
<!--
|
||||
Conditional running of tests.
|
||||
@@ -35,7 +35,7 @@
|
||||
<if>
|
||||
<equals arg1="${env}" arg2="test" />
|
||||
<then>
|
||||
<phing phingfile="phing/tests.xml" haltonfailure="true" />
|
||||
<phing phingfile=".phing/tests.xml" haltonfailure="true" />
|
||||
</then>
|
||||
<else>
|
||||
<echo message="[Skipped] Running tests, checks and creating docs, because it's a not 'test' environment..." />
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "meritoo/common-library",
|
||||
"description": "Useful classes, methods, extensions etc.",
|
||||
"license": "MIT",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.18",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Meritoo.pl",
|
||||
@@ -11,24 +11,31 @@
|
||||
}
|
||||
],
|
||||
"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": {
|
||||
"phpunit/phpunit": "^4.8 || ^5.0",
|
||||
"squizlabs/php_codesniffer": "^2.8",
|
||||
"phpmd/phpmd": "^2.6",
|
||||
"sebastian/phpcpd": "^3.0",
|
||||
"friendsofphp/php-cs-fixer": "^2.2",
|
||||
"pdepend/pdepend": "^2.5",
|
||||
"phploc/phploc": "^3.0",
|
||||
"friendsofphp/php-cs-fixer": "^2.1"
|
||||
"phploc/phploc": "^2.1",
|
||||
"phpmd/phpmd": "^2.6",
|
||||
"phpunit/phpunit": "^4.8",
|
||||
"sebastian/phpcpd": "^2.0",
|
||||
"squizlabs/php_codesniffer": "^2.9"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Meritoo\\Common\\": "src/Meritoo/Common/",
|
||||
"Meritoo\\Common\\Tests\\": "tests/Meritoo/Common/Tests/"
|
||||
"Meritoo\\Common\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Meritoo\\Common\\Test\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
}
|
||||
}
|
||||
|
||||
3518
composer.lock
generated
3518
composer.lock
generated
File diff suppressed because it is too large
Load Diff
15
docker-compose.yml
Normal file
15
docker-compose.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
version: '3'
|
||||
|
||||
services:
|
||||
php-cli:
|
||||
image: meritoo/common-library
|
||||
container_name: meritoo-common-library
|
||||
working_dir: /project
|
||||
entrypoint: php
|
||||
command: -S 0.0.0.0:9999
|
||||
build:
|
||||
context: ./.docker/config
|
||||
args:
|
||||
- TIMEZONE=$TIMEZONE
|
||||
volumes:
|
||||
- .:/project
|
||||
@@ -12,7 +12,7 @@
|
||||
bootstrap="./vendor/autoload.php"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Meritoo's Common Library Test Suite">
|
||||
<testsuite name="Meritoo Package - Main Test Suite">
|
||||
<directory>./tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
@@ -30,6 +30,6 @@
|
||||
</groups>
|
||||
|
||||
<logging>
|
||||
<log type="coverage-html" target="./build/logs/phpunit_coverage/html" />
|
||||
<log type="coverage-html" target="./.build/logs/phpunit_coverage/html" />
|
||||
</logging>
|
||||
</phpunit>
|
||||
|
||||
284
src/Collection/Collection.php
Normal file
284
src/Collection/Collection.php
Normal file
@@ -0,0 +1,284 @@
|
||||
<?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\Collection;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use Countable;
|
||||
use IteratorAggregate;
|
||||
use Meritoo\Common\Utilities\Arrays;
|
||||
|
||||
/**
|
||||
* Collection of elements.
|
||||
* It's a set of some elements, e.g. objects.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Collection implements Countable, ArrayAccess, IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* The elements of collection
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $elements;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param array $elements (optional) The elements of collection
|
||||
*/
|
||||
public function __construct(array $elements = [])
|
||||
{
|
||||
$this->elements = $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Required by interface Countable
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Required by interface ArrayAccess
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return $this->exists($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Required by interface ArrayAccess
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
if ($this->exists($offset)) {
|
||||
return $this->elements[$offset];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Required by interface ArrayAccess
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->elements[$offset] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Required by interface ArrayAccess
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
if ($this->exists($offset)) {
|
||||
unset($this->elements[$offset]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Required by interface IteratorAggregate
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new ArrayIterator($this->elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds given element (at the end of collection)
|
||||
*
|
||||
* @param mixed $element The element to add
|
||||
* @param mixed $index (optional) Index / key of the element
|
||||
* @return $this
|
||||
*/
|
||||
public function add($element, $index = null)
|
||||
{
|
||||
if (null === $index) {
|
||||
$this->elements[] = $element;
|
||||
} else {
|
||||
$this->elements[$index] = $element;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds given elements (at the end of collection)
|
||||
*
|
||||
* @param array|Collection $elements The elements to add
|
||||
* @param bool|false $useIndexes (optional) If is set to true, indexes of given elements will be used in
|
||||
* this collection. Otherwise - not.
|
||||
* @return $this
|
||||
*/
|
||||
public function addMultiple($elements, $useIndexes = false)
|
||||
{
|
||||
if (!empty($elements)) {
|
||||
foreach ($elements as $index => $element) {
|
||||
if (!$useIndexes) {
|
||||
$index = null;
|
||||
}
|
||||
|
||||
$this->add($element, $index);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepends given element (adds given element at the beginning of collection)
|
||||
*
|
||||
* @param mixed $element The element to prepend
|
||||
* @return $this
|
||||
*/
|
||||
public function prepend($element)
|
||||
{
|
||||
array_unshift($this->elements, $element);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes given element
|
||||
*
|
||||
* @param mixed $element The element to remove
|
||||
* @return $this
|
||||
*/
|
||||
public function remove($element)
|
||||
{
|
||||
if ($this->count() > 0) {
|
||||
foreach ($this->elements as $index => $existing) {
|
||||
if ($element === $existing) {
|
||||
unset($this->elements[$index]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if collection is empty
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty()
|
||||
{
|
||||
return empty($this->elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if given element is first in the collection
|
||||
*
|
||||
* @param mixed $element The element to verify
|
||||
* @return bool
|
||||
*/
|
||||
public function isFirst($element)
|
||||
{
|
||||
return reset($this->elements) === $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if given element is last in the collection
|
||||
*
|
||||
* @param mixed $element The element to verify
|
||||
* @return bool
|
||||
*/
|
||||
public function isLast($element)
|
||||
{
|
||||
return end($this->elements) === $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if the collection has given element, iow. if given element exists in the collection
|
||||
*
|
||||
* @param mixed $element The element to verify
|
||||
* @return bool
|
||||
*/
|
||||
public function has($element)
|
||||
{
|
||||
$index = Arrays::getIndexOf($this->elements, $element);
|
||||
|
||||
return null !== $index && false !== $index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns previous element for given element
|
||||
*
|
||||
* @param mixed $element The element to verify
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getPrevious($element)
|
||||
{
|
||||
return Arrays::getPreviousElement($this->elements, $element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns next element for given element
|
||||
*
|
||||
* @param mixed $element The element to verify
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getNext($element)
|
||||
{
|
||||
return Arrays::getNextElement($this->elements, $element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first element in the collection
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getFirst()
|
||||
{
|
||||
return Arrays::getFirstElement($this->elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last element in the collection
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getLast()
|
||||
{
|
||||
return Arrays::getLastElement($this->elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array representation of the collection
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information if element with given index/key exists
|
||||
*
|
||||
* @param string|int $index The index/key of element
|
||||
* @return bool
|
||||
*/
|
||||
private function exists($index)
|
||||
{
|
||||
return isset($this->elements[$index]) || array_key_exists($index, $this->elements);
|
||||
}
|
||||
}
|
||||
32
src/Exception/Date/UnknownDatePartTypeException.php
Normal file
32
src/Exception/Date/UnknownDatePartTypeException.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?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\Exception\Date;
|
||||
|
||||
use Meritoo\Common\Exception\Base\UnknownTypeException;
|
||||
use Meritoo\Common\Type\DatePartType;
|
||||
|
||||
/**
|
||||
* An exception used while type of date part, e.g. "year", is unknown
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class UnknownDatePartTypeException extends UnknownTypeException
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $unknownDatePart Type of date part, e.g. "year". One of DatePartType class constants.
|
||||
* @param string $value Incorrect value
|
||||
*/
|
||||
public function __construct($unknownDatePart, $value)
|
||||
{
|
||||
parent::__construct($unknownDatePart, new DatePartType(), sprintf('date part (with value %s)', $value));
|
||||
}
|
||||
}
|
||||
31
src/Exception/File/EmptyFileException.php
Normal file
31
src/Exception/File/EmptyFileException.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?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\Exception\File;
|
||||
|
||||
/**
|
||||
* An exception used while file with given path is empty (has no content)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class EmptyFileException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $emptyFilePath Path of the empty file
|
||||
*/
|
||||
public function __construct($emptyFilePath)
|
||||
{
|
||||
$template = 'File with path \'%s\' is empty (has no content). Did you provide path of proper file?';
|
||||
$message = sprintf($template, $emptyFilePath);
|
||||
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
26
src/Exception/File/EmptyFilePathException.php
Normal file
26
src/Exception/File/EmptyFilePathException.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?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\Exception\File;
|
||||
|
||||
/**
|
||||
* An exception used while path of given file is empty
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class EmptyFilePathException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('Path of the file is empty. Did you provide path of proper file?');
|
||||
}
|
||||
}
|
||||
31
src/Exception/File/NotExistingFileException.php
Normal file
31
src/Exception/File/NotExistingFileException.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?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\Exception\File;
|
||||
|
||||
/**
|
||||
* An exception used while file with given path does not exist
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class NotExistingFileException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $notExistingFilePath Path of not existing (or not readable) file
|
||||
*/
|
||||
public function __construct($notExistingFilePath)
|
||||
{
|
||||
$template = 'File with path \'%s\' does not exist (or is not readable). Did you provide path of proper file?';
|
||||
$message = sprintf($template, $notExistingFilePath);
|
||||
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
38
src/Exception/Method/DisabledMethodException.php
Normal file
38
src/Exception/Method/DisabledMethodException.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?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\Exception\Method;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* An exception used while method cannot be called, because is disabled
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class DisabledMethodException extends Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $disabledMethod Name of the disabled method
|
||||
* @param string $alternativeMethod (optional) Name of the alternative method
|
||||
*/
|
||||
public function __construct($disabledMethod, $alternativeMethod = '')
|
||||
{
|
||||
$template = 'Method %s() cannot be called, because is disabled.';
|
||||
|
||||
if (!empty($alternativeMethod)) {
|
||||
$template .= ' Use %s() instead.';
|
||||
}
|
||||
|
||||
$message = sprintf($template, $disabledMethod, $alternativeMethod);
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class CannotResolveClassNameException extends Exception
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param array|object|string $source Source of the class's / trait's name. It cane be an array of objects,
|
||||
* @param array|object|string $source Source of the class's / trait's name. It can be an array of objects,
|
||||
* namespaces, object or namespace.
|
||||
* @param bool $forClass (optional) If is set to true, message of this exception for class is
|
||||
* prepared. Otherwise - for trait.
|
||||
@@ -6,7 +6,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Exception;
|
||||
namespace Meritoo\Common\Exception\Regex;
|
||||
|
||||
/**
|
||||
* An exception used while length of given hexadecimal value of color is incorrect
|
||||
@@ -6,10 +6,10 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Exception;
|
||||
namespace Meritoo\Common\Exception\Regex;
|
||||
|
||||
/**
|
||||
* An exception used while given hexadecimal value of color is incorrect
|
||||
* An exception used while given hexadecimal value of color is invalid
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
@@ -19,11 +19,11 @@ class InvalidColorHexValueException extends \Exception
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $color Incorrect hexadecimal value of color
|
||||
* @param string $color Invalid hexadecimal value of color
|
||||
*/
|
||||
public function __construct($color)
|
||||
{
|
||||
$message = sprintf('Hexadecimal value of color \'%s\' is incorrect. Is there everything ok?', $color);
|
||||
$message = sprintf('Hexadecimal value of color \'%s\' is invalid. Is there everything ok?', $color);
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
23
src/Exception/Regex/InvalidUrlException.php
Normal file
23
src/Exception/Regex/InvalidUrlException.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Exception\Regex;
|
||||
|
||||
/**
|
||||
* An exception used while url is invalid
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class InvalidUrlException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $url Invalid url
|
||||
*/
|
||||
public function __construct($url)
|
||||
{
|
||||
$message = sprintf('Url \'%s\' is invalid. Is there everything ok?', $url);
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
23
src/Exception/Type/UnknownOopVisibilityTypeException.php
Normal file
23
src/Exception/Type/UnknownOopVisibilityTypeException.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Exception\Type;
|
||||
|
||||
use Meritoo\Common\Exception\Base\UnknownTypeException;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* An exception used while the visibility of a property, a method or (as of PHP 7.1.0) a constant is unknown
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class UnknownOopVisibilityTypeException extends UnknownTypeException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($unknownType)
|
||||
{
|
||||
parent::__construct($unknownType, new OopVisibilityType(), 'OOP-related visibility');
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
<?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\Exception\Date;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* An exception used while given part of date is incorrect, e.g. value of year is incorrect
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class IncorrectDatePartException extends Exception
|
||||
{
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $value Incorrect value
|
||||
* @param string $datePart Type of date part, e.g. "year". One of \Meritoo\Common\Type\DatePartType class constants.
|
||||
*/
|
||||
public function __construct($value, $datePart)
|
||||
{
|
||||
$message = sprintf('Value of %s \'%s\' is incorrect. Is there everything ok?', $datePart, $value);
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Utilities;
|
||||
|
||||
use DateTime;
|
||||
use Generator;
|
||||
|
||||
/**
|
||||
* Test case with common methods and data providers
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class TestCase extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* 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['suprise/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 = '')
|
||||
{
|
||||
if (!empty($directoryPath)) {
|
||||
$directoryPath = '/' . $directoryPath;
|
||||
}
|
||||
|
||||
return sprintf('%s/../../../../data/tests/%s%s', __DIR__, $fileName, $directoryPath);
|
||||
}
|
||||
}
|
||||
23
src/Test/Base/BaseTestCase.php
Normal file
23
src/Test/Base/BaseTestCase.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?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 Meritoo\Common\Traits\Test\Base\BaseTestCaseTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Base test case with common methods and data providers
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
abstract class BaseTestCase extends TestCase
|
||||
{
|
||||
use BaseTestCaseTrait;
|
||||
}
|
||||
22
src/Test/Base/BaseTypeTestCase.php
Normal file
22
src/Test/Base/BaseTypeTestCase.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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 Meritoo\Common\Traits\Test\Base\BaseTypeTestCaseTrait;
|
||||
|
||||
/**
|
||||
* Base test case for the type of something
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
abstract class BaseTypeTestCase extends BaseTestCase
|
||||
{
|
||||
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();
|
||||
}
|
||||
@@ -33,7 +33,7 @@ abstract class BaseType
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
if ($this->all === null) {
|
||||
if (null === $this->all) {
|
||||
$this->all = Reflection::getConstants($this);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
<?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\Type;
|
||||
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
@@ -6,18 +6,20 @@
|
||||
* 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.
|
||||
* Contains start and end date of the period.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @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
|
||||
*
|
||||
@@ -140,7 +131,7 @@ class DatePeriod
|
||||
/*
|
||||
* Unknown date or format is invalid?
|
||||
*/
|
||||
if ($date === null || !Date::isValidDateFormat($format)) {
|
||||
if (null === $date || !Date::isValidDateFormat($format)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
37
src/Type/OopVisibilityType.php
Normal file
37
src/Type/OopVisibilityType.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Type;
|
||||
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
|
||||
/**
|
||||
* The visibility of a property, a method or (as of PHP 7.1.0) a constant
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*
|
||||
* @see http://php.net/manual/en/language.oop5.visibility.php
|
||||
*/
|
||||
class OopVisibilityType extends BaseType
|
||||
{
|
||||
/**
|
||||
* The "private" visibility of OOP
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const IS_PRIVATE = 3;
|
||||
|
||||
/**
|
||||
* The "protected" visibility of OOP
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const IS_PROTECTED = 2;
|
||||
|
||||
/**
|
||||
* The "public" visibility of OOP
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const IS_PUBLIC = 1;
|
||||
}
|
||||
@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
|
||||
/**
|
||||
* Useful arrays methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Arrays
|
||||
{
|
||||
@@ -415,7 +415,7 @@ class Arrays
|
||||
* var autoGeneratedVariable = new Array(...);autoGeneratedVariable[0] = new Array(...);
|
||||
* autoGeneratedVariable[1] = new Array(...);
|
||||
*/
|
||||
if ($counter === 1) {
|
||||
if (1 === $counter) {
|
||||
$effect .= "\n";
|
||||
}
|
||||
|
||||
@@ -758,7 +758,7 @@ class Arrays
|
||||
foreach ($exploded as $item) {
|
||||
$exploded2 = explode($valuesKeysSeparator, $item);
|
||||
|
||||
if (count($exploded2) == 2) {
|
||||
if (2 == count($exploded2)) {
|
||||
$key = trim($exploded2[0]);
|
||||
$value = trim($exploded2[1]);
|
||||
|
||||
@@ -1124,7 +1124,7 @@ class Arrays
|
||||
if (!empty($array)) {
|
||||
$childPosition = 1;
|
||||
|
||||
if ($startPosition !== null) {
|
||||
if (null !== $startPosition) {
|
||||
$array[$keyName] = $startPosition;
|
||||
}
|
||||
|
||||
@@ -1316,7 +1316,7 @@ class Arrays
|
||||
*
|
||||
* If one of the above is true, not all elements of given array are empty
|
||||
*/
|
||||
if ((!is_array($element) && $strictNull && $element !== null) || !empty($element)) {
|
||||
if ((!is_array($element) && $strictNull && null !== $element) || !empty($element)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1353,7 +1353,7 @@ class Arrays
|
||||
* Values should be compared only and both arrays are one-dimensional?
|
||||
* Let's find difference by using simple function
|
||||
*/
|
||||
if ($valuesOnly && self::getDimensionsCount($array1) == 1 && self::getDimensionsCount($array2) == 1) {
|
||||
if ($valuesOnly && 1 == self::getDimensionsCount($array1) && 1 == self::getDimensionsCount($array2)) {
|
||||
return array_diff($array1, $array2);
|
||||
}
|
||||
|
||||
@@ -1381,7 +1381,7 @@ class Arrays
|
||||
}
|
||||
}
|
||||
|
||||
if ($difference !== null) {
|
||||
if (null !== $difference) {
|
||||
$effect[] = $difference;
|
||||
}
|
||||
} else {
|
||||
@@ -1472,7 +1472,7 @@ class Arrays
|
||||
* Start index not provided?
|
||||
* Let's look for the first index / key of given array
|
||||
*/
|
||||
if ($startIndex === null) {
|
||||
if (null === $startIndex) {
|
||||
$startIndex = self::getFirstKey($array);
|
||||
}
|
||||
|
||||
@@ -1617,7 +1617,7 @@ class Arrays
|
||||
* Index of element or of element's key is unknown?
|
||||
* Probably the element does not exist in given array, so... nothing to do
|
||||
*/
|
||||
if ($elementKey === null || $indexOfKey === null) {
|
||||
if (null === $elementKey || null === $indexOfKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
|
||||
/**
|
||||
* Useful methods for bundle
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Bundle
|
||||
{
|
||||
@@ -59,7 +59,7 @@ class Composer
|
||||
* Unknown data from the composer.json file or there is no node with given name?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($data === null || !isset($data->{$nodeName})) {
|
||||
if (null === $data || !isset($data->{$nodeName})) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -10,14 +10,15 @@ namespace Meritoo\Common\Utilities;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Meritoo\Common\Exception\Date\IncorrectDatePartException;
|
||||
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
|
||||
use Meritoo\Common\Type\DatePartType;
|
||||
use Meritoo\Common\Type\DatePeriod;
|
||||
|
||||
/**
|
||||
* Useful date methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Date
|
||||
{
|
||||
@@ -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 ($period == DatePeriod::LAST_YEAR || $period == DatePeriod::NEXT_YEAR) {
|
||||
$yearDifference = 1;
|
||||
|
||||
if ($period == DatePeriod::LAST_YEAR) {
|
||||
$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 ($dateStart !== null && $dateEnd !== null) {
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -231,7 +240,7 @@ class Date
|
||||
* @param int $day The day value
|
||||
*
|
||||
* @return int
|
||||
* @throws IncorrectDatePartException
|
||||
* @throws UnknownDatePartTypeException
|
||||
*/
|
||||
public static function getDayOfWeek($year, $month, $day)
|
||||
{
|
||||
@@ -243,21 +252,21 @@ class Date
|
||||
* Oops, incorrect year
|
||||
*/
|
||||
if ($year <= 0) {
|
||||
throw new IncorrectDatePartException($year, DatePartType::YEAR);
|
||||
throw new UnknownDatePartTypeException(DatePartType::YEAR, $year);
|
||||
}
|
||||
|
||||
/*
|
||||
* Oops, incorrect month
|
||||
*/
|
||||
if ($month < 1 || $month > 12) {
|
||||
throw new IncorrectDatePartException($month, DatePartType::MONTH);
|
||||
throw new UnknownDatePartTypeException(DatePartType::MONTH, $month);
|
||||
}
|
||||
|
||||
/*
|
||||
* Oops, incorrect day
|
||||
*/
|
||||
if ($day < 1 || $day > 31) {
|
||||
throw new IncorrectDatePartException($day, DatePartType::DAY);
|
||||
throw new UnknownDatePartTypeException(DatePartType::DAY, $day);
|
||||
}
|
||||
|
||||
if ($month < 3) {
|
||||
@@ -311,7 +320,7 @@ class Date
|
||||
|
||||
$encoding = mb_detect_encoding($name);
|
||||
|
||||
if ($encoding === false) {
|
||||
if (false === $encoding) {
|
||||
$name = mb_convert_encoding($name, 'UTF-8', 'ISO-8859-2');
|
||||
}
|
||||
|
||||
@@ -378,46 +387,46 @@ class Date
|
||||
self::DATE_DIFFERENCE_UNIT_MINUTES,
|
||||
];
|
||||
|
||||
if ($differenceUnit === null || $differenceUnit == self::DATE_DIFFERENCE_UNIT_YEARS) {
|
||||
if (null === $differenceUnit || self::DATE_DIFFERENCE_UNIT_YEARS == $differenceUnit) {
|
||||
$diff = $dateEnd->diff($dateStart);
|
||||
|
||||
/*
|
||||
* Difference between dates in years should be returned only?
|
||||
*/
|
||||
if ($differenceUnit == self::DATE_DIFFERENCE_UNIT_YEARS) {
|
||||
if (self::DATE_DIFFERENCE_UNIT_YEARS == $differenceUnit) {
|
||||
return $diff->y;
|
||||
}
|
||||
|
||||
$difference[self::DATE_DIFFERENCE_UNIT_YEARS] = $diff->y;
|
||||
}
|
||||
|
||||
if ($differenceUnit === null || $differenceUnit == self::DATE_DIFFERENCE_UNIT_MONTHS) {
|
||||
if (null === $differenceUnit || self::DATE_DIFFERENCE_UNIT_MONTHS == $differenceUnit) {
|
||||
$diff = $dateEnd->diff($dateStart);
|
||||
|
||||
/*
|
||||
* Difference between dates in months should be returned only?
|
||||
*/
|
||||
if ($differenceUnit == self::DATE_DIFFERENCE_UNIT_MONTHS) {
|
||||
if (self::DATE_DIFFERENCE_UNIT_MONTHS == $differenceUnit) {
|
||||
return $diff->m;
|
||||
}
|
||||
|
||||
$difference[self::DATE_DIFFERENCE_UNIT_MONTHS] = $diff->m;
|
||||
}
|
||||
|
||||
if ($differenceUnit === null || in_array($differenceUnit, $relatedUnits)) {
|
||||
if (null === $differenceUnit || in_array($differenceUnit, $relatedUnits)) {
|
||||
$days = (int)floor($dateDiff / $daySeconds);
|
||||
|
||||
/*
|
||||
* Difference between dates in days should be returned only?
|
||||
*/
|
||||
if ($differenceUnit == self::DATE_DIFFERENCE_UNIT_DAYS) {
|
||||
if (self::DATE_DIFFERENCE_UNIT_DAYS == $differenceUnit) {
|
||||
return $days;
|
||||
}
|
||||
|
||||
/*
|
||||
* All units should be returned?
|
||||
*/
|
||||
if ($differenceUnit === null) {
|
||||
if (null === $differenceUnit) {
|
||||
$difference[self::DATE_DIFFERENCE_UNIT_DAYS] = $days;
|
||||
}
|
||||
|
||||
@@ -427,20 +436,20 @@ class Date
|
||||
$daysInSeconds = $days * $daySeconds;
|
||||
}
|
||||
|
||||
if ($differenceUnit === null || in_array($differenceUnit, $relatedUnits)) {
|
||||
if (null === $differenceUnit || in_array($differenceUnit, $relatedUnits)) {
|
||||
$hours = (int)floor(($dateDiff - $daysInSeconds) / $hourSeconds);
|
||||
|
||||
/*
|
||||
* Difference between dates in hours should be returned only?
|
||||
*/
|
||||
if ($differenceUnit == self::DATE_DIFFERENCE_UNIT_HOURS) {
|
||||
if (self::DATE_DIFFERENCE_UNIT_HOURS == $differenceUnit) {
|
||||
return $hours;
|
||||
}
|
||||
|
||||
/*
|
||||
* All units should be returned?
|
||||
*/
|
||||
if ($differenceUnit === null) {
|
||||
if (null === $differenceUnit) {
|
||||
$difference[self::DATE_DIFFERENCE_UNIT_HOURS] = $hours;
|
||||
}
|
||||
|
||||
@@ -450,13 +459,13 @@ class Date
|
||||
$hoursInSeconds = $hours * $hourSeconds;
|
||||
}
|
||||
|
||||
if ($differenceUnit === null || $differenceUnit == self::DATE_DIFFERENCE_UNIT_MINUTES) {
|
||||
if (null === $differenceUnit || self::DATE_DIFFERENCE_UNIT_MINUTES == $differenceUnit) {
|
||||
$minutes = (int)floor(($dateDiff - $daysInSeconds - $hoursInSeconds) / 60);
|
||||
|
||||
/*
|
||||
* Difference between dates in minutes should be returned only?
|
||||
*/
|
||||
if ($differenceUnit == self::DATE_DIFFERENCE_UNIT_MINUTES) {
|
||||
if (self::DATE_DIFFERENCE_UNIT_MINUTES == $differenceUnit) {
|
||||
return $minutes;
|
||||
}
|
||||
|
||||
@@ -524,7 +533,7 @@ class Date
|
||||
*/
|
||||
public static function getRandomDate(DateTime $startDate = null, $start = 1, $end = 100, $intervalTemplate = 'P%sD')
|
||||
{
|
||||
if ($startDate === null) {
|
||||
if (null === $startDate) {
|
||||
$startDate = new DateTime();
|
||||
}
|
||||
|
||||
@@ -590,7 +599,7 @@ class Date
|
||||
*/
|
||||
$dateFromFormat = DateTime::createFromFormat($dateFormat, $value);
|
||||
|
||||
if ($dateFromFormat === false) {
|
||||
if (false === $dateFromFormat) {
|
||||
/*
|
||||
* Nothing to do more, because:
|
||||
* a) instance of the DateTime was created
|
||||
@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
|
||||
/**
|
||||
* Useful locale methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Locale
|
||||
{
|
||||
@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
|
||||
/**
|
||||
* Useful methods for mime types of files
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class MimeTypes
|
||||
{
|
||||
@@ -15,8 +15,8 @@ use Transliterator;
|
||||
/**
|
||||
* Miscellaneous methods (only static functions)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Miscellaneous
|
||||
{
|
||||
@@ -65,7 +65,7 @@ class Miscellaneous
|
||||
|
||||
if (!empty($directoryContent)) {
|
||||
foreach ($directoryContent as $fileName) {
|
||||
if ($fileName != '.' && $fileName != '..') {
|
||||
if ('.' != $fileName && '..' != $fileName) {
|
||||
$content = null;
|
||||
|
||||
if (!empty($startFileName) && !$startFileFound) {
|
||||
@@ -80,7 +80,7 @@ class Miscellaneous
|
||||
$content = self::getDirectoryContent($directoryPath . $fileName, true, $maxFilesCount - $count);
|
||||
}
|
||||
|
||||
if ($content !== null) {
|
||||
if (null !== $content) {
|
||||
$files[$fileName] = $content;
|
||||
|
||||
if (!empty($maxFilesCount)) {
|
||||
@@ -365,7 +365,7 @@ class Miscellaneous
|
||||
* Oops, cannot instantiate converter
|
||||
* Nothing to do
|
||||
*/
|
||||
if ($converter === null) {
|
||||
if (null === $converter) {
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -460,7 +460,7 @@ class Miscellaneous
|
||||
* Value to find is neither a string nor an array OR it's an empty string?
|
||||
* Nothing to do
|
||||
*/
|
||||
if ((!$searchIsString && !$searchIsArray) || ($searchIsString && strlen($search) == 0)) {
|
||||
if ((!$searchIsString && !$searchIsArray) || ($searchIsString && 0 == strlen($search))) {
|
||||
return $effect;
|
||||
}
|
||||
|
||||
@@ -612,7 +612,7 @@ class Miscellaneous
|
||||
$effect = mb_substr($text, 0, $maxLength, 'utf-8');
|
||||
$lastSpacePosition = mb_strrpos($effect, ' ', 'utf-8');
|
||||
|
||||
if ($lastSpacePosition !== false) {
|
||||
if (false !== $lastSpacePosition) {
|
||||
$effect = mb_substr($effect, 0, $lastSpacePosition, 'utf-8');
|
||||
}
|
||||
|
||||
@@ -730,7 +730,7 @@ class Miscellaneous
|
||||
}
|
||||
|
||||
foreach (scandir($directoryPath) as $item) {
|
||||
if ($item == '.' || $item == '..') {
|
||||
if ('.' == $item || '..' == $item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -776,7 +776,7 @@ class Miscellaneous
|
||||
foreach ($members as $key => $value) {
|
||||
$value = mb_strtolower($value);
|
||||
|
||||
if ($key == 0) {
|
||||
if (0 == $key) {
|
||||
$effect .= self::lowercaseFirst($value);
|
||||
} else {
|
||||
$effect .= self::uppercaseFirst($value);
|
||||
@@ -812,7 +812,7 @@ class Miscellaneous
|
||||
|
||||
if ($restLowercase) {
|
||||
$effect = mb_strtolower($effect);
|
||||
} elseif ($restLowercase === false) {
|
||||
} elseif (false === $restLowercase) {
|
||||
$effect = mb_strtoupper($effect);
|
||||
}
|
||||
|
||||
@@ -850,7 +850,7 @@ class Miscellaneous
|
||||
|
||||
if ($restLowercase) {
|
||||
$effect = mb_strtolower($effect);
|
||||
} elseif ($restLowercase === false) {
|
||||
} elseif (false === $restLowercase) {
|
||||
$effect = mb_strtoupper($effect);
|
||||
}
|
||||
|
||||
@@ -1032,6 +1032,8 @@ class Miscellaneous
|
||||
$separator = DIRECTORY_SEPARATOR;
|
||||
|
||||
foreach ($paths as $path) {
|
||||
$path = trim($path);
|
||||
|
||||
/*
|
||||
* Empty paths are useless
|
||||
*/
|
||||
@@ -1153,7 +1155,7 @@ class Miscellaneous
|
||||
{
|
||||
$value = filter_input($globalSourceType, $variableName);
|
||||
|
||||
if ($value === null) {
|
||||
if (null === $value) {
|
||||
$globalSource = null;
|
||||
|
||||
switch ($globalSourceType) {
|
||||
@@ -1178,7 +1180,7 @@ class Miscellaneous
|
||||
break;
|
||||
}
|
||||
|
||||
if ($globalSource !== null && isset($globalSource[$variableName])) {
|
||||
if (null !== $globalSource && isset($globalSource[$variableName])) {
|
||||
$value = $globalSource[$variableName];
|
||||
|
||||
if (!ini_get('magic_quotes_gpc')) {
|
||||
@@ -1232,7 +1234,7 @@ class Miscellaneous
|
||||
/*
|
||||
* First line is only HTTP status and is unneeded so skip it
|
||||
*/
|
||||
if ($i === 0) {
|
||||
if (0 === $i) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1242,7 +1244,7 @@ class Miscellaneous
|
||||
/*
|
||||
* If the header is a "set-cookie" let's save it to "cookies" array
|
||||
*/
|
||||
if ($key === 'Set-Cookie') {
|
||||
if ('Set-Cookie' === $key) {
|
||||
$cookieParameters = explode(';', $value);
|
||||
|
||||
$name = '';
|
||||
@@ -1260,7 +1262,7 @@ class Miscellaneous
|
||||
* First parameter will be always a cookie name and it's value. It is not needed to run
|
||||
* further actions for them, so save the values and move to next parameter.
|
||||
*/
|
||||
if ($j === 0) {
|
||||
if (0 === $j) {
|
||||
$name = trim($param[0]);
|
||||
$value = trim($param[1]);
|
||||
continue;
|
||||
@@ -1416,7 +1418,7 @@ class Miscellaneous
|
||||
if ($asHexadecimal) {
|
||||
$hexadecimal = dechex($colorComponent);
|
||||
|
||||
if (strlen($hexadecimal) == 1) {
|
||||
if (1 == strlen($hexadecimal)) {
|
||||
return sprintf('0%s', $hexadecimal, $hexadecimal);
|
||||
}
|
||||
|
||||
@@ -1471,4 +1473,37 @@ class Miscellaneous
|
||||
|
||||
return $invertedColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns project's root path.
|
||||
* Looks for directory that contains composer.json.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getProjectRootPath()
|
||||
{
|
||||
$projectRootPath = '';
|
||||
|
||||
$fileName = 'composer.json';
|
||||
$directoryPath = __DIR__;
|
||||
|
||||
/*
|
||||
* Path of directory it's not the path of last directory?
|
||||
*/
|
||||
while (DIRECTORY_SEPARATOR !== $directoryPath) {
|
||||
$filePath = static::concatenatePaths($directoryPath, $fileName);
|
||||
|
||||
/*
|
||||
* Is here file we are looking for?
|
||||
* Maybe it's a project's root path
|
||||
*/
|
||||
if (file_exists($filePath)) {
|
||||
$projectRootPath = $directoryPath;
|
||||
}
|
||||
|
||||
$directoryPath = dirname($directoryPath);
|
||||
}
|
||||
|
||||
return $projectRootPath;
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,8 @@ use Doctrine\ORM\QueryBuilder;
|
||||
/**
|
||||
* Useful methods for query builder (the Doctrine's QueryBuilder class)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class QueryBuilderUtility
|
||||
{
|
||||
@@ -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 (count($value) == 2) {
|
||||
$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 ($value === null) {
|
||||
$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;
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
namespace Meritoo\Common\Utilities;
|
||||
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\Common\Util\ClassUtils;
|
||||
use Doctrine\Common\Util\Inflector;
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
||||
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
||||
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
|
||||
@@ -23,8 +23,8 @@ use ReflectionProperty;
|
||||
/**
|
||||
* Useful reflection methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Reflection
|
||||
{
|
||||
@@ -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
|
||||
@@ -201,7 +201,7 @@ class Reflection
|
||||
* Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* 2016-11-07
|
||||
*/
|
||||
if ($object !== null) {
|
||||
if (null !== $object) {
|
||||
unset($exploded[0]);
|
||||
|
||||
$property = implode('.', $exploded);
|
||||
@@ -229,32 +229,45 @@ class Reflection
|
||||
if ($class->hasProperty($property) || $force) {
|
||||
$property = Inflector::classify($property);
|
||||
|
||||
$methodPrefixes = [
|
||||
$getterPrefixes = [
|
||||
'get',
|
||||
'has',
|
||||
'is',
|
||||
];
|
||||
|
||||
foreach ($methodPrefixes as $prefix) {
|
||||
$method = sprintf('%s%s', $prefix, $property);
|
||||
foreach ($getterPrefixes as $prefix) {
|
||||
$getterName = sprintf('%s%s', $prefix, $property);
|
||||
|
||||
if ($class->hasMethod($method)) {
|
||||
$value = $object->{$method}();
|
||||
if ($class->hasMethod($getterName)) {
|
||||
$method = new ReflectionMethod($object, $getterName);
|
||||
|
||||
/*
|
||||
* Getter is not accessible publicly?
|
||||
* I have to skip it, to avoid an error like this:
|
||||
*
|
||||
* Call to protected method My\ExtraClass::getExtraProperty() from context 'My\ExtraClass'
|
||||
*/
|
||||
if ($method->isProtected() || $method->isPrivate()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $object->{$getterName}();
|
||||
$valueFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$valueFound && $reflectionProperty !== null) {
|
||||
if (!$valueFound && null !== $reflectionProperty) {
|
||||
/*
|
||||
* Oops, we have got exception.
|
||||
* Oops, value of the property is still unknown
|
||||
*
|
||||
* 3rd try:
|
||||
* Let's try modify accessibility of the property and try again to get value.
|
||||
* Let's modify accessibility of the property and try again to get value
|
||||
*/
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$value = $reflectionProperty->getValue($object);
|
||||
$reflectionProperty->setAccessible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -275,6 +288,14 @@ class Reflection
|
||||
*/
|
||||
public static function getPropertyValues($objects, $property, $force = false)
|
||||
{
|
||||
/*
|
||||
* No objects?
|
||||
* Nothing to do
|
||||
*/
|
||||
if (empty($objects)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($objects instanceof Collection) {
|
||||
$objects = $objects->toArray();
|
||||
}
|
||||
@@ -285,7 +306,7 @@ class Reflection
|
||||
foreach ($objects as $entity) {
|
||||
$value = self::getPropertyValue($entity, $property, $force);
|
||||
|
||||
if ($value !== null) {
|
||||
if (null !== $value) {
|
||||
$values[] = $value;
|
||||
}
|
||||
}
|
||||
@@ -345,7 +366,7 @@ class Reflection
|
||||
if ($withoutNamespace) {
|
||||
$classOnly = Miscellaneous::getLastElementOfString($name, '\\');
|
||||
|
||||
if ($classOnly !== null) {
|
||||
if (null !== $classOnly) {
|
||||
$name = $classOnly;
|
||||
}
|
||||
|
||||
@@ -417,31 +438,45 @@ class Reflection
|
||||
/**
|
||||
* Returns given object properties
|
||||
*
|
||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||
* @param int $filter (optional) Filter of properties. Uses ReflectionProperty class constants.
|
||||
* By default all properties are returned.
|
||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||
* @param int $filter (optional) Filter of properties. Uses ReflectionProperty class
|
||||
* constants. By default all properties are returned.
|
||||
* @param bool $includeParents (optional) If is set to true, properties of parent classes are
|
||||
* included (recursively). Otherwise - not.
|
||||
* @return array|ReflectionProperty
|
||||
*/
|
||||
public static function getProperties($source, $filter = null)
|
||||
public static function getProperties($source, $filter = null, $includeParents = false)
|
||||
{
|
||||
$className = self::getClassName($source);
|
||||
$reflection = new ReflectionClass($className);
|
||||
|
||||
if ($filter === null) {
|
||||
if (null === $filter) {
|
||||
$filter = ReflectionProperty::IS_PRIVATE
|
||||
+ ReflectionProperty::IS_PROTECTED
|
||||
+ ReflectionProperty::IS_PUBLIC
|
||||
+ ReflectionProperty::IS_STATIC;
|
||||
}
|
||||
|
||||
return $reflection->getProperties($filter);
|
||||
$properties = $reflection->getProperties($filter);
|
||||
$parentProperties = [];
|
||||
|
||||
if ($includeParents) {
|
||||
$parent = self::getParentClass($source);
|
||||
|
||||
if (false !== $parent) {
|
||||
$parentClass = $parent->getName();
|
||||
$parentProperties = self::getProperties($parentClass, $filter, $includeParents);
|
||||
}
|
||||
}
|
||||
|
||||
return array_merge($properties, $parentProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a parent class
|
||||
* Returns a parent class or false if there is no parent class
|
||||
*
|
||||
* @param array|object|string $source An array of objects, namespaces, object or namespace
|
||||
* @return ReflectionClass
|
||||
* @return ReflectionClass|bool
|
||||
*/
|
||||
public static function getParentClass($source)
|
||||
{
|
||||
@@ -477,7 +512,7 @@ class Reflection
|
||||
/*
|
||||
* Oops, cannot resolve class
|
||||
*/
|
||||
if ($className === null) {
|
||||
if (null === $className) {
|
||||
throw new CannotResolveClassNameException($class);
|
||||
}
|
||||
|
||||
@@ -603,7 +638,7 @@ class Reflection
|
||||
if (!$uses && $verifyParents) {
|
||||
$parentClassName = self::getParentClassName($className);
|
||||
|
||||
if ($parentClassName !== null) {
|
||||
if (null !== $parentClassName) {
|
||||
return self::usesTrait($parentClassName, $trait, true);
|
||||
}
|
||||
}
|
||||
@@ -624,7 +659,7 @@ class Reflection
|
||||
$reflection = new ReflectionClass($className);
|
||||
$parentClass = $reflection->getParentClass();
|
||||
|
||||
if ($parentClass === null || $parentClass === false) {
|
||||
if (null === $parentClass || false === $parentClass) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@
|
||||
|
||||
namespace Meritoo\Common\Utilities;
|
||||
|
||||
use Meritoo\Common\Exception\IncorrectColorHexLengthException;
|
||||
use Meritoo\Common\Exception\InvalidColorHexValueException;
|
||||
use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException;
|
||||
use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
|
||||
|
||||
/**
|
||||
* Useful regular expressions methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Regex
|
||||
{
|
||||
@@ -84,7 +84,7 @@ class Regex
|
||||
$taxid = preg_replace('/[\s-]/', '', $taxidString);
|
||||
$sum = 0;
|
||||
|
||||
if (strlen($taxid) == 10 && is_numeric($taxid)) {
|
||||
if (10 == strlen($taxid) && is_numeric($taxid)) {
|
||||
for ($x = 0; $x <= 8; ++$x) {
|
||||
$sum += $taxid[$x] * $weights[$x];
|
||||
}
|
||||
@@ -185,9 +185,9 @@ class Regex
|
||||
$pattern = '|' . $filterExpression . '|';
|
||||
$matchesCount = preg_match($pattern, $value, $matches);
|
||||
|
||||
$remove = $matchesCount == 0;
|
||||
$remove = 0 == $matchesCount;
|
||||
} else {
|
||||
if ($value == '') {
|
||||
if ('' == $value) {
|
||||
$value = '\'\'';
|
||||
} elseif (is_string($value)) {
|
||||
$value = '\'' . $value . '\'';
|
||||
@@ -431,7 +431,7 @@ class Regex
|
||||
public static function startsWith($string, $beginning)
|
||||
{
|
||||
if (!empty($string) && !empty($beginning)) {
|
||||
if (strlen($beginning) == 1 && !self::isLetterOrDigit($beginning)) {
|
||||
if (1 == strlen($beginning) && !self::isLetterOrDigit($beginning)) {
|
||||
$beginning = '\\' . $beginning;
|
||||
}
|
||||
|
||||
@@ -452,7 +452,7 @@ class Regex
|
||||
*/
|
||||
public static function endsWith($string, $ending)
|
||||
{
|
||||
if (strlen($ending) == 1 && !self::isLetterOrDigit($ending)) {
|
||||
if (1 == strlen($ending) && !self::isLetterOrDigit($ending)) {
|
||||
$ending = '\\' . $ending;
|
||||
}
|
||||
|
||||
@@ -537,7 +537,7 @@ class Regex
|
||||
*/
|
||||
public static function contains($haystack, $needle)
|
||||
{
|
||||
if (strlen($needle) == 1 && !self::isLetterOrDigit($needle)) {
|
||||
if (1 == strlen($needle) && !self::isLetterOrDigit($needle)) {
|
||||
$needle = '\\' . $needle;
|
||||
}
|
||||
|
||||
@@ -653,7 +653,7 @@ class Regex
|
||||
}
|
||||
|
||||
$modulo = $sum % 11;
|
||||
$numberControl = ($modulo == 10) ? 0 : $modulo;
|
||||
$numberControl = (10 == $modulo) ? 0 : $modulo;
|
||||
|
||||
return $numberControl == $nip[9];
|
||||
}
|
||||
@@ -698,10 +698,10 @@ class Regex
|
||||
$color = Miscellaneous::replace($color, '/#/', '');
|
||||
$length = strlen($color);
|
||||
|
||||
if ($length === 3) {
|
||||
if (3 === $length) {
|
||||
$color = Miscellaneous::replace($color, '/(.)(.)(.)/', '$1$1$2$2$3$3');
|
||||
} else {
|
||||
if ($length !== 6) {
|
||||
if (6 !== $length) {
|
||||
if ($throwException) {
|
||||
throw new IncorrectColorHexLengthException($color);
|
||||
}
|
||||
@@ -14,8 +14,8 @@ use Doctrine\ORM\QueryBuilder;
|
||||
/**
|
||||
* Useful methods for repository
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Repository
|
||||
{
|
||||
@@ -31,14 +31,14 @@ class Repository
|
||||
{
|
||||
$position = self::getExtremePosition($items, $asLast);
|
||||
|
||||
if ($position === null && $force) {
|
||||
if (null === $position && $force) {
|
||||
$position = 0;
|
||||
}
|
||||
|
||||
if ($position !== null && !empty($items)) {
|
||||
if (null !== $position && !empty($items)) {
|
||||
foreach ($items as $item) {
|
||||
if (method_exists($item, 'getPosition')) {
|
||||
if ($item->getPosition() === null) {
|
||||
if (null === $item->getPosition()) {
|
||||
if ($asLast) {
|
||||
++$position;
|
||||
} else {
|
||||
@@ -11,8 +11,8 @@ namespace Meritoo\Common\Utilities;
|
||||
/**
|
||||
* Useful uri methods (only static functions)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Uri
|
||||
{
|
||||
@@ -15,8 +15,8 @@ use SimpleXMLElement;
|
||||
/**
|
||||
* Useful XML-related methods (only static functions)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Xml
|
||||
{
|
||||
@@ -40,7 +40,7 @@ class Xml
|
||||
$query = $path->query('/*/*');
|
||||
$nodesCount = $query->length;
|
||||
|
||||
if ($nodesCount == 0) {
|
||||
if (0 == $nodesCount) {
|
||||
return $element1;
|
||||
}
|
||||
|
||||
329
tests/Collection/CollectionTest.php
Normal file
329
tests/Collection/CollectionTest.php
Normal file
@@ -0,0 +1,329 @@
|
||||
<?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\Collection;
|
||||
|
||||
use ArrayIterator;
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of the collection of elements
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class CollectionTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* An empty collection
|
||||
*
|
||||
* @var Collection
|
||||
*/
|
||||
private $emptyCollection;
|
||||
|
||||
/**
|
||||
* Simple collection
|
||||
*
|
||||
* @var Collection
|
||||
*/
|
||||
private $simpleCollection;
|
||||
|
||||
/**
|
||||
* Elements of simple collection
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $simpleElements;
|
||||
|
||||
public function testEmptyCollection()
|
||||
{
|
||||
static::assertEquals(0, $this->emptyCollection->count());
|
||||
static::assertCount(0, $this->emptyCollection);
|
||||
static::assertEmpty($this->emptyCollection);
|
||||
|
||||
static::assertTrue($this->emptyCollection->isEmpty());
|
||||
static::assertEquals([], $this->emptyCollection->toArray());
|
||||
static::assertEmpty($this->emptyCollection->toArray());
|
||||
|
||||
static::assertNull($this->emptyCollection->getFirst());
|
||||
static::assertNull($this->emptyCollection->getLast());
|
||||
static::assertNull($this->emptyCollection[1]);
|
||||
static::assertNull($this->emptyCollection['abc']);
|
||||
}
|
||||
|
||||
public function testNotEmptyCollection()
|
||||
{
|
||||
static::assertEquals(4, $this->simpleCollection->count());
|
||||
static::assertCount(4, $this->simpleCollection);
|
||||
static::assertNotEmpty($this->simpleCollection);
|
||||
|
||||
static::assertFalse($this->simpleCollection->isEmpty());
|
||||
static::assertEquals($this->simpleElements, $this->simpleCollection->toArray());
|
||||
static::assertNotEmpty($this->simpleCollection->toArray());
|
||||
|
||||
static::assertEquals('lorem', $this->simpleCollection->getFirst());
|
||||
static::assertEquals('sit', $this->simpleCollection->getLast());
|
||||
static::assertEquals('dolor', $this->simpleCollection[123]);
|
||||
}
|
||||
|
||||
public function testCount()
|
||||
{
|
||||
static::assertEquals(0, $this->emptyCollection->count());
|
||||
static::assertEquals(4, $this->simpleCollection->count());
|
||||
}
|
||||
|
||||
public function testOffsetExists()
|
||||
{
|
||||
static::assertFalse(isset($this->emptyCollection['abc']));
|
||||
static::assertFalse(isset($this->simpleCollection['abc']));
|
||||
|
||||
static::assertTrue(isset($this->simpleCollection[0]));
|
||||
static::assertTrue(isset($this->simpleCollection[345]));
|
||||
}
|
||||
|
||||
public function testOffsetGet()
|
||||
{
|
||||
static::assertNull($this->emptyCollection['abc']);
|
||||
static::assertNull($this->simpleCollection['abc']);
|
||||
|
||||
static::assertEquals('lorem', $this->simpleCollection[0]);
|
||||
static::assertEquals('sit', $this->simpleCollection[345]);
|
||||
}
|
||||
|
||||
public function testOffsetSet()
|
||||
{
|
||||
$this->emptyCollection['test1'] = 1234;
|
||||
$this->simpleCollection['test2'] = 5678;
|
||||
|
||||
static::assertTrue($this->emptyCollection->has(1234));
|
||||
static::assertEquals(1234, $this->emptyCollection['test1']);
|
||||
|
||||
static::assertTrue($this->simpleCollection->has(5678));
|
||||
static::assertEquals(5678, $this->simpleCollection['test2']);
|
||||
}
|
||||
|
||||
public function testOffsetUnset()
|
||||
{
|
||||
unset($this->simpleCollection[0]);
|
||||
|
||||
static::assertFalse($this->simpleCollection->has('lorem'));
|
||||
static::assertEquals('ipsum', $this->simpleCollection[1]);
|
||||
static::assertEquals(3, $this->simpleCollection->count());
|
||||
|
||||
unset($this->simpleCollection[123]);
|
||||
|
||||
static::assertFalse($this->simpleCollection->has('dolor'));
|
||||
static::assertEquals('ipsum', $this->simpleCollection[1]);
|
||||
static::assertEquals(2, $this->simpleCollection->count());
|
||||
}
|
||||
|
||||
public function testGetIterator()
|
||||
{
|
||||
static::assertInstanceOf(ArrayIterator::class, $this->simpleCollection->getIterator());
|
||||
}
|
||||
|
||||
public function testAdd()
|
||||
{
|
||||
$this->emptyCollection->add('test1');
|
||||
|
||||
static::assertTrue($this->emptyCollection->has('test1'));
|
||||
static::assertEquals(1, $this->emptyCollection->count());
|
||||
static::assertEquals('test1', $this->emptyCollection[0]);
|
||||
}
|
||||
|
||||
public function testAddWithIndex()
|
||||
{
|
||||
$this->emptyCollection->add('test2', 1234);
|
||||
|
||||
static::assertTrue($this->emptyCollection->has('test2'));
|
||||
static::assertEquals(1, $this->emptyCollection->count());
|
||||
static::assertEquals('test2', $this->emptyCollection[1234]);
|
||||
}
|
||||
|
||||
public function testAddMultipleUsingEmptyArray()
|
||||
{
|
||||
$this->emptyCollection->addMultiple([]);
|
||||
|
||||
static::assertEquals(0, $this->emptyCollection->count());
|
||||
static::assertTrue($this->emptyCollection->isEmpty());
|
||||
}
|
||||
|
||||
public function testAddMultiple()
|
||||
{
|
||||
$elements = [
|
||||
'test1',
|
||||
'test2',
|
||||
1234 => 'test3',
|
||||
5678 => 'test4',
|
||||
];
|
||||
|
||||
$this->emptyCollection->addMultiple($elements);
|
||||
|
||||
static::assertFalse($this->emptyCollection->isEmpty());
|
||||
static::assertEquals(4, $this->emptyCollection->count());
|
||||
|
||||
static::assertEquals('test1', $this->emptyCollection[0]);
|
||||
static::assertEquals('test2', $this->emptyCollection[1]);
|
||||
static::assertEquals('test3', $this->emptyCollection[2]);
|
||||
static::assertEquals('test4', $this->emptyCollection[3]);
|
||||
}
|
||||
|
||||
public function testAddMultipleUsingIndexes()
|
||||
{
|
||||
$elements = [
|
||||
'test1',
|
||||
'test2',
|
||||
1234 => 'test3',
|
||||
5678 => 'test4',
|
||||
];
|
||||
|
||||
$this->emptyCollection->addMultiple($elements, true);
|
||||
|
||||
static::assertFalse($this->emptyCollection->isEmpty());
|
||||
static::assertEquals(4, $this->emptyCollection->count());
|
||||
|
||||
static::assertEquals('test1', $this->emptyCollection[0]);
|
||||
static::assertEquals('test2', $this->emptyCollection[1]);
|
||||
static::assertEquals('test3', $this->emptyCollection[1234]);
|
||||
static::assertEquals('test4', $this->emptyCollection[5678]);
|
||||
}
|
||||
|
||||
public function testPrepend()
|
||||
{
|
||||
$this->emptyCollection->prepend('lorem-ipsum');
|
||||
|
||||
static::assertFalse($this->emptyCollection->isEmpty());
|
||||
static::assertEquals(1, $this->emptyCollection->count());
|
||||
static::assertEquals('lorem-ipsum', $this->emptyCollection[0]);
|
||||
|
||||
$this->simpleCollection->prepend('lorem-ipsum');
|
||||
|
||||
static::assertFalse($this->simpleCollection->isEmpty());
|
||||
static::assertEquals(5, $this->simpleCollection->count());
|
||||
static::assertEquals('lorem-ipsum', $this->simpleCollection[0]);
|
||||
}
|
||||
|
||||
public function testRemoveNotExistingElement()
|
||||
{
|
||||
$this->emptyCollection->remove('abc');
|
||||
|
||||
static::assertTrue($this->emptyCollection->isEmpty());
|
||||
static::assertEquals(0, $this->emptyCollection->count());
|
||||
|
||||
$this->simpleCollection->remove('abc');
|
||||
|
||||
static::assertFalse($this->simpleCollection->isEmpty());
|
||||
static::assertEquals(4, $this->simpleCollection->count());
|
||||
}
|
||||
|
||||
public function testRemove()
|
||||
{
|
||||
static::assertFalse($this->simpleCollection->isEmpty());
|
||||
static::assertEquals(4, $this->simpleCollection->count());
|
||||
static::assertEquals('ipsum', $this->simpleCollection[1]);
|
||||
|
||||
$this->simpleCollection->remove('ipsum');
|
||||
|
||||
static::assertFalse($this->simpleCollection->isEmpty());
|
||||
static::assertEquals(3, $this->simpleCollection->count());
|
||||
static::assertNull($this->simpleCollection[1]);
|
||||
}
|
||||
|
||||
public function testIsEmpty()
|
||||
{
|
||||
static::assertTrue($this->emptyCollection->isEmpty());
|
||||
static::assertFalse($this->simpleCollection->isEmpty());
|
||||
}
|
||||
|
||||
public function testIsFirst()
|
||||
{
|
||||
static::assertFalse($this->emptyCollection->isFirst('abc'));
|
||||
static::assertFalse($this->simpleCollection->isFirst('abc'));
|
||||
static::assertFalse($this->simpleCollection->isFirst('dolor'));
|
||||
static::assertTrue($this->simpleCollection->isFirst('lorem'));
|
||||
}
|
||||
|
||||
public function testIsLast()
|
||||
{
|
||||
static::assertFalse($this->emptyCollection->isLast('abc'));
|
||||
static::assertFalse($this->simpleCollection->isLast('abc'));
|
||||
static::assertFalse($this->simpleCollection->isLast('dolor'));
|
||||
static::assertTrue($this->simpleCollection->isLast('sit'));
|
||||
}
|
||||
|
||||
public function testHas()
|
||||
{
|
||||
static::assertFalse($this->emptyCollection->has('abc'));
|
||||
static::assertFalse($this->simpleCollection->has('abc'));
|
||||
static::assertTrue($this->simpleCollection->has('lorem'));
|
||||
static::assertTrue($this->simpleCollection->has('dolor'));
|
||||
}
|
||||
|
||||
public function testGetPrevious()
|
||||
{
|
||||
static::assertNull($this->emptyCollection->getPrevious('abc'));
|
||||
static::assertNull($this->simpleCollection->getPrevious('abc'));
|
||||
static::assertNull($this->simpleCollection->getPrevious('lorem'));
|
||||
|
||||
static::assertEquals('lorem', $this->simpleCollection->getPrevious('ipsum'));
|
||||
static::assertEquals('dolor', $this->simpleCollection->getPrevious('sit'));
|
||||
}
|
||||
|
||||
public function testGetNext()
|
||||
{
|
||||
static::assertNull($this->emptyCollection->getNext('abc'));
|
||||
static::assertNull($this->simpleCollection->getNext('abc'));
|
||||
static::assertNull($this->simpleCollection->getNext('sit'));
|
||||
|
||||
static::assertEquals('dolor', $this->simpleCollection->getNext('ipsum'));
|
||||
static::assertEquals('sit', $this->simpleCollection->getNext('dolor'));
|
||||
}
|
||||
|
||||
public function testGetFirst()
|
||||
{
|
||||
static::assertNull($this->emptyCollection->getFirst());
|
||||
static::assertEquals('lorem', $this->simpleCollection->getFirst());
|
||||
}
|
||||
|
||||
public function testGetLast()
|
||||
{
|
||||
static::assertNull($this->emptyCollection->getLast());
|
||||
static::assertEquals('sit', $this->simpleCollection->getLast());
|
||||
}
|
||||
|
||||
public function testToArray()
|
||||
{
|
||||
static::assertEquals([], $this->emptyCollection->toArray());
|
||||
static::assertEquals($this->simpleElements, $this->simpleCollection->toArray());
|
||||
}
|
||||
|
||||
public function testExistsVisibilityAndArguments()
|
||||
{
|
||||
static::assertMethodVisibilityAndArguments(Collection::class, 'exists', OopVisibilityType::IS_PRIVATE, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->simpleElements = [
|
||||
'lorem',
|
||||
'ipsum',
|
||||
123 => 'dolor',
|
||||
345 => 'sit',
|
||||
];
|
||||
|
||||
$this->emptyCollection = new Collection();
|
||||
$this->simpleCollection = new Collection($this->simpleElements);
|
||||
}
|
||||
}
|
||||
@@ -6,19 +6,26 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Exception\Base;
|
||||
namespace Meritoo\Common\Test\Exception\Base;
|
||||
|
||||
use Meritoo\Common\Exception\Base\UnknownTypeException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Tests of the exception used while type of something is unknown
|
||||
* Test case of the exception used while type of something is unknown
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class UnknownTypeExceptionTest extends \PHPUnit_Framework_TestCase
|
||||
class UnknownTypeExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(UnknownTestTypeException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
public function testWithoutException()
|
||||
{
|
||||
self::assertEquals('Test 2', (new TestService())->getTranslatedType('test_2'));
|
||||
@@ -26,7 +33,7 @@ class UnknownTypeExceptionTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testTheException()
|
||||
{
|
||||
$this->expectException(UnknownTestTypeException::class);
|
||||
$this->setExpectedException(UnknownTestTypeException::class);
|
||||
self::assertEmpty((new TestService())->getTranslatedType('test_3'));
|
||||
}
|
||||
}
|
||||
71
tests/Exception/Date/UnknownDatePartTypeExceptionTest.php
Normal file
71
tests/Exception/Date/UnknownDatePartTypeExceptionTest.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?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\Exception\Date;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\DatePartType;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while type of date part, e.g. "year", is unknown
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class UnknownDatePartTypeExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(UnknownDatePartTypeException::class, OopVisibilityType::IS_PUBLIC, 2, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $unknownDatePart Type of date part, e.g. "year". One of DatePartType class constants.
|
||||
* @param string $value Incorrect value
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideDatePartAndValue
|
||||
*/
|
||||
public function testConstructorMessage($unknownDatePart, $value, $expectedMessage)
|
||||
{
|
||||
$exception = new UnknownDatePartTypeException($unknownDatePart, $value);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides type of date part, incorrect value and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideDatePartAndValue()
|
||||
{
|
||||
$template = 'The \'%s\' type of date part (with value %s) is unknown. Probably doesn\'t exist or there is a'
|
||||
. ' typo. You should use one of these types: day, hour, minute, month, second, year.';
|
||||
|
||||
yield[
|
||||
DatePartType::DAY,
|
||||
'44',
|
||||
sprintf($template, DatePartType::DAY, '44'),
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePartType::MONTH,
|
||||
'22',
|
||||
sprintf($template, DatePartType::MONTH, '22'),
|
||||
];
|
||||
|
||||
yield[
|
||||
DatePartType::MINUTE,
|
||||
'77',
|
||||
sprintf($template, DatePartType::MINUTE, '77'),
|
||||
];
|
||||
}
|
||||
}
|
||||
60
tests/Exception/File/EmptyFileExceptionTest.php
Normal file
60
tests/Exception/File/EmptyFileExceptionTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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\Exception\File;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\File\EmptyFileException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while file with given path is empty (has no content)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class EmptyFileExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(EmptyFileException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $emptyFilePath Path of the empty file
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider providePathOfFile
|
||||
*/
|
||||
public function testConstructorMessage($emptyFilePath, $expectedMessage)
|
||||
{
|
||||
$exception = new EmptyFileException($emptyFilePath);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides path of the empty file and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function providePathOfFile()
|
||||
{
|
||||
$template = 'File with path \'%s\' is empty (has no content). Did you provide path of proper file?';
|
||||
|
||||
yield[
|
||||
'aa/bb/cc',
|
||||
sprintf($template, 'aa/bb/cc'),
|
||||
];
|
||||
|
||||
yield[
|
||||
'images/show/car.jpg',
|
||||
sprintf($template, 'images/show/car.jpg'),
|
||||
];
|
||||
}
|
||||
}
|
||||
33
tests/Exception/File/EmptyFilePathExceptionTest.php
Normal file
33
tests/Exception/File/EmptyFilePathExceptionTest.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?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\Exception\File;
|
||||
|
||||
use Meritoo\Common\Exception\File\EmptyFilePathException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while path of given file is empty
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class EmptyFilePathExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(EmptyFilePathException::class, OopVisibilityType::IS_PUBLIC);
|
||||
}
|
||||
|
||||
public function testConstructorMessage()
|
||||
{
|
||||
$exception = new EmptyFilePathException();
|
||||
static::assertEquals('Path of the file is empty. Did you provide path of proper file?', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
60
tests/Exception/File/NotExistingFileExceptionTest.php
Normal file
60
tests/Exception/File/NotExistingFileExceptionTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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\Exception\File;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\File\NotExistingFileException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while file with given path does not exist
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class NotExistingFileExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(NotExistingFileException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $notExistingFilePath Path of not existing (or not readable) file
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider providePathOfFile
|
||||
*/
|
||||
public function testConstructorMessage($notExistingFilePath, $expectedMessage)
|
||||
{
|
||||
$exception = new NotExistingFileException($notExistingFilePath);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides path of not existing file and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function providePathOfFile()
|
||||
{
|
||||
$template = 'File with path \'%s\' does not exist (or is not readable). Did you provide path of proper file?';
|
||||
|
||||
yield[
|
||||
'aa/bb/cc',
|
||||
sprintf($template, 'aa/bb/cc'),
|
||||
];
|
||||
|
||||
yield[
|
||||
'images/show/car.jpg',
|
||||
sprintf($template, 'images/show/car.jpg'),
|
||||
];
|
||||
}
|
||||
}
|
||||
65
tests/Exception/Method/DisabledMethodExceptionTest.php
Normal file
65
tests/Exception/Method/DisabledMethodExceptionTest.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?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\Exception\Method;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Method\DisabledMethodException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while method cannot be called, because is disabled
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class DisabledMethodExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(DisabledMethodException::class, OopVisibilityType::IS_PUBLIC, 2, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $disabledMethod Name of the disabled method
|
||||
* @param string $alternativeMethod Name of the alternative method
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @internal param string $emptyFilePath Path of the empty file
|
||||
* @dataProvider provideMethodsNames
|
||||
*/
|
||||
public function testConstructorMessage($disabledMethod, $alternativeMethod, $expectedMessage)
|
||||
{
|
||||
$exception = new DisabledMethodException($disabledMethod, $alternativeMethod);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides name of the disabled method, name of the alternative method and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideMethodsNames()
|
||||
{
|
||||
$templateShort = 'Method %s() cannot be called, because is disabled.';
|
||||
$templateLong = $templateShort . ' Use %s() instead.';
|
||||
|
||||
yield[
|
||||
'FooBar::loremIpsum',
|
||||
'',
|
||||
sprintf($templateShort, 'FooBar::loremIpsum'),
|
||||
];
|
||||
|
||||
yield[
|
||||
'FooBar::loremIpsum',
|
||||
'AnotherClass::alternativeMethod',
|
||||
sprintf($templateLong, 'FooBar::loremIpsum', 'AnotherClass::alternativeMethod'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?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\Exception\Reflection;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while name of class or trait cannot be resolved
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class CannotResolveClassNameExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(CannotResolveClassNameException::class, OopVisibilityType::IS_PUBLIC, 2, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|object|string $source Source of the class's / trait's name. It can be an array of objects,
|
||||
* namespaces, object or namespace.
|
||||
* @param bool $forClass If is set to true, message of this exception for class is prepared.
|
||||
* Otherwise - for trait.
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideClassName
|
||||
*/
|
||||
public function testConstructorMessage($source, $forClass, $expectedMessage)
|
||||
{
|
||||
$exception = new CannotResolveClassNameException($source, $forClass);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides source of the class's / trait's name, information if message of this exception should be prepared for
|
||||
* class and the expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideClassName()
|
||||
{
|
||||
yield[
|
||||
'Not\Existing\Class',
|
||||
true,
|
||||
'Name of class from given \'string\' Not\Existing\Class cannot be resolved. Is there everything ok?',
|
||||
];
|
||||
|
||||
yield[
|
||||
'Not\Existing\Trait',
|
||||
false,
|
||||
'Name of trait from given \'string\' Not\Existing\Trait cannot be resolved. Is there everything ok?',
|
||||
];
|
||||
|
||||
yield[
|
||||
[
|
||||
new \stdClass(),
|
||||
new \stdClass(),
|
||||
],
|
||||
true,
|
||||
'Name of class from given \'array\' cannot be resolved. Is there everything ok?',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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\Exception\Reflection;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while given class has no child classes
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class MissingChildClassesExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(MissingChildClassesException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|object|string $parentClass Class that hasn't child classes, but it should. An array of objects,
|
||||
* strings, object or string.
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideParentClass
|
||||
*/
|
||||
public function testConstructorMessage($parentClass, $expectedMessage)
|
||||
{
|
||||
$exception = new MissingChildClassesException($parentClass);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides name of class that hasn't child classes, but it should, and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideParentClass()
|
||||
{
|
||||
$template = 'The \'%s\' class requires one child class at least who will extend her (maybe is an abstract'
|
||||
. ' class), but the child classes are missing. Did you forget to extend this class?';
|
||||
|
||||
yield[
|
||||
MissingChildClassesException::class,
|
||||
sprintf($template, MissingChildClassesException::class),
|
||||
];
|
||||
|
||||
yield[
|
||||
[
|
||||
new \stdClass(),
|
||||
new \stdClass(),
|
||||
],
|
||||
sprintf($template, \stdClass::class),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?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\Exception\Reflection;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while given class has more than one child class
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class TooManyChildClassesExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(TooManyChildClassesException::class, OopVisibilityType::IS_PUBLIC, 2, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|object|string $parentClass Class that has more than one child class, but it shouldn't. An array
|
||||
* of objects, strings, object or string.
|
||||
* @param array $childClasses Child classes
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideParentAndChildClasses
|
||||
*/
|
||||
public function testConstructorMessage($parentClass, array $childClasses, $expectedMessage)
|
||||
{
|
||||
$exception = new TooManyChildClassesException($parentClass, $childClasses);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides name of class that has more than one child class, but it shouldn't, child classes, and expected
|
||||
* exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideParentAndChildClasses()
|
||||
{
|
||||
$template = "The '%s' class requires one child class at most who will extend her, but more than one child"
|
||||
. " class was found:\n- %s\n\nWhy did you create more than one classes that extend '%s' class?";
|
||||
|
||||
yield[
|
||||
BaseTestCase::class,
|
||||
[
|
||||
\stdClass::class,
|
||||
OopVisibilityType::class,
|
||||
],
|
||||
sprintf($template, BaseTestCase::class, implode("\n- ", [
|
||||
\stdClass::class,
|
||||
OopVisibilityType::class,
|
||||
]), BaseTestCase::class),
|
||||
];
|
||||
|
||||
yield[
|
||||
TooManyChildClassesException::class,
|
||||
[
|
||||
\stdClass::class,
|
||||
],
|
||||
sprintf($template, TooManyChildClassesException::class, implode("\n- ", [\stdClass::class]), TooManyChildClassesException::class),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?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\Exception\Regex;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while length of given hexadecimal value of color is incorrect
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class IncorrectColorHexLengthExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(IncorrectColorHexLengthException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $color Incorrect hexadecimal value of color
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideColor
|
||||
*/
|
||||
public function testConstructorMessage($color, $expectedMessage)
|
||||
{
|
||||
$exception = new IncorrectColorHexLengthException($color);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides incorrect hexadecimal value of color and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideColor()
|
||||
{
|
||||
$template = 'Length of hexadecimal value of color \'%s\' is incorrect. It\'s %d, but it should be 3 or 6. Is there everything ok?';
|
||||
|
||||
yield[
|
||||
'',
|
||||
sprintf($template, '', strlen('')),
|
||||
];
|
||||
|
||||
yield[
|
||||
'aa-bb-cc',
|
||||
sprintf($template, 'aa-bb-cc', strlen('aa-bb-cc')),
|
||||
];
|
||||
}
|
||||
}
|
||||
60
tests/Exception/Regex/InvalidColorHexValueExceptionTest.php
Normal file
60
tests/Exception/Regex/InvalidColorHexValueExceptionTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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\Exception\Regex;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while given hexadecimal value of color is invalid
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class InvalidColorHexValueExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(InvalidColorHexValueException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $color Invalid hexadecimal value of color
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideColor
|
||||
*/
|
||||
public function testConstructorMessage($color, $expectedMessage)
|
||||
{
|
||||
$exception = new InvalidColorHexValueException($color);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides invalid hexadecimal value of color and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideColor()
|
||||
{
|
||||
$template = 'Hexadecimal value of color \'%s\' is invalid. Is there everything ok?';
|
||||
|
||||
yield[
|
||||
'',
|
||||
sprintf($template, ''),
|
||||
];
|
||||
|
||||
yield[
|
||||
'aa-bb-cc',
|
||||
sprintf($template, 'aa-bb-cc'),
|
||||
];
|
||||
}
|
||||
}
|
||||
60
tests/Exception/Regex/InvalidUrlExceptionTest.php
Normal file
60
tests/Exception/Regex/InvalidUrlExceptionTest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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\Exception\Regex;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Regex\InvalidUrlException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while url is invalid
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class InvalidUrlExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(InvalidUrlException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url Invalid url
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideUrl
|
||||
*/
|
||||
public function testConstructorMessage($url, $expectedMessage)
|
||||
{
|
||||
$exception = new InvalidUrlException($url);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides invalid url and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideUrl()
|
||||
{
|
||||
$template = 'Url \'%s\' is invalid. Is there everything ok?';
|
||||
|
||||
yield[
|
||||
'aa/bb/cc',
|
||||
sprintf($template, 'aa/bb/cc'),
|
||||
];
|
||||
|
||||
yield[
|
||||
'http:/images\show\car.jpg',
|
||||
sprintf($template, 'http:/images\show\car.jpg'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?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\Exception\Type;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Type\UnknownOopVisibilityTypeException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Test case of an exception used while the visibility of a property, a method or (as of PHP 7.1.0) a constant is
|
||||
* unknown
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class UnknownOopVisibilityTypeExceptionTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(UnknownOopVisibilityTypeException::class, OopVisibilityType::IS_PUBLIC, 1, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $unknownType Unknown OOP-related visibility
|
||||
* @param string $expectedMessage Expected exception's message
|
||||
*
|
||||
* @dataProvider provideUnknownType
|
||||
*/
|
||||
public function testConstructorMessage($unknownType, $expectedMessage)
|
||||
{
|
||||
$exception = new UnknownOopVisibilityTypeException($unknownType);
|
||||
static::assertEquals($expectedMessage, $exception->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides path of the empty file and expected exception's message
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideUnknownType()
|
||||
{
|
||||
$allTypes = (new OopVisibilityType())->getAll();
|
||||
|
||||
$template = 'The \'%s\' type of OOP-related visibility is unknown. Probably doesn\'t exist or there is a typo.'
|
||||
. ' You should use one of these types: %s.';
|
||||
|
||||
yield[
|
||||
'',
|
||||
sprintf($template, '', implode(', ', $allTypes)),
|
||||
];
|
||||
|
||||
yield[
|
||||
123,
|
||||
sprintf($template, 123, implode(', ', $allTypes)),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities\Reflection;
|
||||
|
||||
/**
|
||||
* The A class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class A
|
||||
{
|
||||
use E;
|
||||
|
||||
protected function lorem()
|
||||
{
|
||||
return 'ipsum';
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities\Reflection;
|
||||
|
||||
/**
|
||||
* The B class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class B extends A
|
||||
{
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities\Reflection;
|
||||
|
||||
/**
|
||||
* The D class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class D
|
||||
{
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities\Reflection;
|
||||
|
||||
/**
|
||||
* The E trait.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
trait E
|
||||
{
|
||||
}
|
||||
@@ -1,246 +0,0 @@
|
||||
<?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\Tests\Utilities;
|
||||
|
||||
use DateTime;
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Reflection\CannotResolveClassNameException;
|
||||
use Meritoo\Common\Exception\Reflection\MissingChildClassesException;
|
||||
use Meritoo\Common\Exception\Reflection\TooManyChildClassesException;
|
||||
use Meritoo\Common\Tests\Utilities\Reflection\A;
|
||||
use Meritoo\Common\Tests\Utilities\Reflection\B;
|
||||
use Meritoo\Common\Tests\Utilities\Reflection\C;
|
||||
use Meritoo\Common\Tests\Utilities\Reflection\D;
|
||||
use Meritoo\Common\Tests\Utilities\Reflection\E;
|
||||
use Meritoo\Common\Utilities\Reflection;
|
||||
use Meritoo\Common\Utilities\TestCase;
|
||||
|
||||
/**
|
||||
* Tests of the useful reflection methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ReflectionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param mixed $invalidClass Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
*/
|
||||
public function testGetClassNameInvalidClass($invalidClass)
|
||||
{
|
||||
self::assertNull(Reflection::getClassName($invalidClass));
|
||||
self::assertNull(Reflection::getClassName(123));
|
||||
}
|
||||
|
||||
public function testGetClassNameNotExistingClass()
|
||||
{
|
||||
/*
|
||||
* Not existing class
|
||||
*/
|
||||
self::assertEquals('', Reflection::getClassName('xyz'));
|
||||
self::assertEquals('', Reflection::getClassName('xyz', true));
|
||||
}
|
||||
|
||||
public function testGetClassNameExistingClass()
|
||||
{
|
||||
/*
|
||||
* Existing class
|
||||
*/
|
||||
self::assertEquals(self::class, Reflection::getClassName(self::class));
|
||||
self::assertEquals('ReflectionTest', Reflection::getClassName(self::class, true));
|
||||
self::assertEquals(DateTime::class, Reflection::getClassName(new DateTime()));
|
||||
self::assertEquals(DateTime::class, Reflection::getClassName(new DateTime(), true));
|
||||
|
||||
self::assertEquals(DateTime::class, Reflection::getClassName([
|
||||
new DateTime(),
|
||||
new DateTime('yesterday'),
|
||||
]));
|
||||
}
|
||||
|
||||
public function testGetClassNameDuplicatedName()
|
||||
{
|
||||
/*
|
||||
* Class with namespace containing name of class (duplicated string)
|
||||
*/
|
||||
if (class_exists('Symfony\Bundle\SecurityBundle\SecurityBundle')) {
|
||||
self::assertEquals('Symfony\Bundle\SecurityBundle\SecurityBundle', Reflection::getClassName('Symfony\Bundle\SecurityBundle\SecurityBundle'));
|
||||
self::assertEquals('SecurityBundle', Reflection::getClassName('Symfony\Bundle\SecurityBundle\SecurityBundle', true));
|
||||
}
|
||||
}
|
||||
|
||||
public function testGetClassNamespaceNotExistingClass()
|
||||
{
|
||||
/*
|
||||
* Not existing class
|
||||
*/
|
||||
self::assertEquals('', Reflection::getClassNamespace('xyz'));
|
||||
}
|
||||
|
||||
public function testGetClassNamespaceExistingClass()
|
||||
{
|
||||
/*
|
||||
* Existing class
|
||||
*/
|
||||
self::assertEquals('Meritoo\Common\Tests\Utilities', Reflection::getClassNamespace(self::class));
|
||||
self::assertEquals(DateTime::class, Reflection::getClassNamespace(new DateTime()));
|
||||
|
||||
self::assertEquals(DateTime::class, Reflection::getClassNamespace([
|
||||
new DateTime(),
|
||||
new DateTime('yesterday'),
|
||||
]));
|
||||
}
|
||||
|
||||
public function testGetClassNamespaceDuplicatedName()
|
||||
{
|
||||
/*
|
||||
* Class with namespace containing name of class (duplicated string)
|
||||
*/
|
||||
if (class_exists('Symfony\Bundle\SecurityBundle\SecurityBundle')) {
|
||||
self::assertEquals('Symfony\Bundle\SecurityBundle', Reflection::getClassNamespace('Symfony\Bundle\SecurityBundle\SecurityBundle'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $invalidClass Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
*/
|
||||
public function testGetChildClassesInvalidClass($invalidClass)
|
||||
{
|
||||
$this->expectException(CannotResolveClassNameException::class);
|
||||
|
||||
self::assertNull(Reflection::getChildClasses($invalidClass));
|
||||
self::assertNull(Reflection::getChildClasses(123));
|
||||
}
|
||||
|
||||
public function testGetChildClassesNotExistingClass()
|
||||
{
|
||||
$this->expectException(CannotResolveClassNameException::class);
|
||||
self::assertEquals('', Reflection::getChildClasses('xyz'));
|
||||
}
|
||||
|
||||
public function testGetChildClassesExistingClass()
|
||||
{
|
||||
/*
|
||||
* Attention. I have to create instances of these classes to load them and be available while using
|
||||
* get_declared_classes() function in the Reflection::getChildClasses() method. Without these instances the
|
||||
* Reflection::getChildClasses() method returns an empty array even if given class has child classes.
|
||||
*/
|
||||
new A();
|
||||
new B();
|
||||
new C();
|
||||
|
||||
$effect = [
|
||||
C::class,
|
||||
];
|
||||
|
||||
self::assertEquals($effect, Reflection::getChildClasses(B::class));
|
||||
|
||||
$effect = [
|
||||
B::class,
|
||||
C::class,
|
||||
];
|
||||
|
||||
self::assertEquals($effect, Reflection::getChildClasses(A::class));
|
||||
}
|
||||
|
||||
public function testGetOneChildClassWithMissingChildClasses()
|
||||
{
|
||||
$this->expectException(MissingChildClassesException::class);
|
||||
self::assertEquals('LoremIpsum', Reflection::getOneChildClass(C::class));
|
||||
}
|
||||
|
||||
public function testGetOneChildClassWithTooManyChildClasses()
|
||||
{
|
||||
$this->expectException(TooManyChildClassesException::class);
|
||||
|
||||
self::assertEquals(B::class, Reflection::getOneChildClass(A::class));
|
||||
self::assertEquals(C::class, Reflection::getOneChildClass(A::class));
|
||||
}
|
||||
|
||||
public function testGetOneChildClass()
|
||||
{
|
||||
self::assertEquals(C::class, Reflection::getOneChildClass(B::class));
|
||||
}
|
||||
|
||||
public function testGetMethods()
|
||||
{
|
||||
self::assertEquals(0, count(Reflection::getMethods(B::class, true)));
|
||||
self::assertEquals(1, count(Reflection::getMethods(B::class)));
|
||||
self::assertEquals(1, count(Reflection::getMethods(A::class)));
|
||||
self::assertEquals(2, count(Reflection::getMethods(C::class, true)));
|
||||
self::assertEquals(3, count(Reflection::getMethods(C::class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|object|string $class An array of objects, namespaces, object or namespace
|
||||
* @param array|string $trait An array of strings or string
|
||||
*
|
||||
* @dataProvider provideInvalidClassAndTrait
|
||||
*/
|
||||
public function testUsesTraitInvalidClass($class, $trait)
|
||||
{
|
||||
$this->expectException(CannotResolveClassNameException::class);
|
||||
self::assertNull(Reflection::usesTrait($class, $trait));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $trait Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
*/
|
||||
public function testUsesTraitInvalidTrait($trait)
|
||||
{
|
||||
$this->expectException(CannotResolveClassNameException::class);
|
||||
self::assertNull(Reflection::usesTrait(DateTime::class, $trait));
|
||||
}
|
||||
|
||||
public function testUsesTraitExistingClass()
|
||||
{
|
||||
self::assertTrue(Reflection::usesTrait(A::class, E::class));
|
||||
self::assertFalse(Reflection::usesTrait(B::class, E::class));
|
||||
self::assertFalse(Reflection::usesTrait(C::class, E::class));
|
||||
self::assertFalse(Reflection::usesTrait(D::class, E::class));
|
||||
}
|
||||
|
||||
public function testUsesTraitExistingClassAndVerifyParents()
|
||||
{
|
||||
self::assertTrue(Reflection::usesTrait(A::class, E::class, true));
|
||||
self::assertTrue(Reflection::usesTrait(B::class, E::class, true));
|
||||
self::assertTrue(Reflection::usesTrait(C::class, E::class, true));
|
||||
self::assertFalse(Reflection::usesTrait(D::class, E::class, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides invalid class and trait
|
||||
*
|
||||
* @return Generator
|
||||
*/
|
||||
public function provideInvalidClassAndTrait()
|
||||
{
|
||||
yield[
|
||||
'',
|
||||
'',
|
||||
];
|
||||
|
||||
yield[
|
||||
null,
|
||||
null,
|
||||
];
|
||||
|
||||
yield[
|
||||
0,
|
||||
0,
|
||||
];
|
||||
|
||||
yield[
|
||||
[],
|
||||
[],
|
||||
];
|
||||
}
|
||||
}
|
||||
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
|
||||
{
|
||||
}
|
||||
@@ -6,19 +6,25 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Type\Base;
|
||||
namespace Meritoo\Common\Test\Type\Base;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
|
||||
/**
|
||||
* Tests of the base / abstract type of something
|
||||
* Test case of the base / abstract type of something
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class BaseTypeTest extends \PHPUnit_Framework_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,22 +6,25 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Type;
|
||||
namespace Meritoo\Common\Test\Type;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTypeTestCase;
|
||||
use Meritoo\Common\Type\DatePartType;
|
||||
|
||||
/**
|
||||
* Tests of the type of date part, e.g. "year"
|
||||
* Test case of the type of date part, e.g. "year"
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class DatePartTypeTest extends \PHPUnit_Framework_TestCase
|
||||
class DatePartTypeTest extends BaseTypeTestCase
|
||||
{
|
||||
public function testGetAll()
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getAllExpectedTypes()
|
||||
{
|
||||
$expectedTypes = [
|
||||
return [
|
||||
'DAY' => DatePartType::DAY,
|
||||
'HOUR' => DatePartType::HOUR,
|
||||
'MINUTE' => DatePartType::MINUTE,
|
||||
@@ -29,29 +32,20 @@ class DatePartTypeTest extends \PHPUnit_Framework_TestCase
|
||||
'SECOND' => DatePartType::SECOND,
|
||||
'YEAR' => DatePartType::YEAR,
|
||||
];
|
||||
|
||||
$all = (new DatePartType())->getAll();
|
||||
self::assertEquals($expectedTypes, $all);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $toVerifyType Concrete type to verify (of given instance of type)
|
||||
* @param bool $isCorrect Expected information if given type is correct
|
||||
*
|
||||
* @dataProvider provideConcreteType
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testIsCorrectType($toVerifyType, $isCorrect)
|
||||
protected function getTestedTypeInstance()
|
||||
{
|
||||
$type = new DatePartType();
|
||||
self::assertEquals($isCorrect, $type->isCorrectType($toVerifyType));
|
||||
return new DatePartType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides type of something for testing the isCorrectType() method
|
||||
*
|
||||
* @return Generator
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function provideConcreteType()
|
||||
public function provideTypeToVerify()
|
||||
{
|
||||
yield[
|
||||
'',
|
||||
@@ -1,20 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
/**
|
||||
* (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\Type;
|
||||
|
||||
use DateTime;
|
||||
use Generator;
|
||||
use Meritoo\Common\Utilities\DatePeriod;
|
||||
use Meritoo\Common\Utilities\TestCase;
|
||||
use Meritoo\Common\Test\Base\BaseTypeTestCase;
|
||||
use Meritoo\Common\Type\DatePeriod;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
|
||||
/**
|
||||
* Tests of date's period
|
||||
* Test case of date's period
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class DatePeriodTest extends TestCase
|
||||
class DatePeriodTest extends BaseTypeTestCase
|
||||
{
|
||||
public function testConstructorVisibilityAndArguments()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(DatePeriod::class, OopVisibilityType::IS_PUBLIC, 2, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DateTime $startDate (optional) Start date of period
|
||||
* @param DateTime $endDate (optional) End date of period
|
||||
@@ -46,33 +58,6 @@ class DatePeriodTest extends TestCase
|
||||
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
|
||||
@@ -130,36 +115,6 @@ class DatePeriodTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
@@ -258,4 +213,68 @@ class DatePeriodTest extends TestCase
|
||||
'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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -6,17 +6,18 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Utilities\Arrays;
|
||||
|
||||
/**
|
||||
* Tests of the useful arrays methods
|
||||
* Test case of the useful arrays methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ArraysTest extends \PHPUnit_Framework_TestCase
|
||||
class ArraysTest extends BaseTestCase
|
||||
{
|
||||
private $simpleArray;
|
||||
private $simpleArrayWithKeys;
|
||||
@@ -24,6 +25,11 @@ class ArraysTest extends \PHPUnit_Framework_TestCase
|
||||
private $complexArray;
|
||||
private $superComplexArray;
|
||||
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(Arrays::class);
|
||||
}
|
||||
|
||||
public function testValues2string()
|
||||
{
|
||||
/*
|
||||
@@ -6,18 +6,24 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Utilities\Bundle;
|
||||
|
||||
/**
|
||||
* Tests of the useful methods for bundle
|
||||
* Test case of the useful methods for bundle
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class BundleTest extends \PHPUnit_Framework_TestCase
|
||||
class BundleTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(Bundle::class);
|
||||
}
|
||||
|
||||
public function testGetBundleViewPathEmptyPathAndBundle()
|
||||
{
|
||||
self::assertNull(Bundle::getBundleViewPath('', ''));
|
||||
@@ -6,18 +6,19 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Utilities\Composer;
|
||||
use Meritoo\Common\Utilities\TestCase;
|
||||
|
||||
/**
|
||||
* Tests of the useful Composer-related methods
|
||||
* Test case of the useful Composer-related methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ComposerTest extends TestCase
|
||||
class ComposerTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* Path of existing composer.json used as source of data for tests
|
||||
@@ -26,6 +27,11 @@ class ComposerTest extends TestCase
|
||||
*/
|
||||
private $composerJsonPath;
|
||||
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(Composer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $composerJsonPath Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
@@ -60,7 +66,7 @@ class ComposerTest extends TestCase
|
||||
/**
|
||||
* Provides names and values of existing nodes
|
||||
*
|
||||
* @return \Generator
|
||||
* @return Generator
|
||||
*/
|
||||
public function getExistingNode()
|
||||
{
|
||||
@@ -82,6 +88,6 @@ class ComposerTest extends TestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->composerJsonPath = $this->getFilePathToTests(Composer::FILE_NAME_MAIN);
|
||||
$this->composerJsonPath = $this->getFilePathForTesting(Composer::FILE_NAME_MAIN);
|
||||
}
|
||||
}
|
||||
@@ -6,23 +6,29 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\Date\IncorrectDatePartException;
|
||||
use Meritoo\Common\Exception\Date\UnknownDatePartTypeException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\DatePeriod;
|
||||
use Meritoo\Common\Utilities\Date;
|
||||
use Meritoo\Common\Utilities\TestCase;
|
||||
|
||||
/**
|
||||
* Tests of the Date methods (only static functions)
|
||||
* Test case of the Date methods (only static functions)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class DateTest extends TestCase
|
||||
class DateTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(Date::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
@@ -218,7 +224,7 @@ class DateTest extends TestCase
|
||||
*/
|
||||
public function testGetDayOfWeekIncorrectValues($year, $month, $day)
|
||||
{
|
||||
$this->expectException(IncorrectDatePartException::class);
|
||||
$this->setExpectedException(UnknownDatePartTypeException::class);
|
||||
self::assertEmpty(Date::getDayOfWeek($year, $month, $day));
|
||||
}
|
||||
|
||||
@@ -454,8 +460,11 @@ class DateTest extends TestCase
|
||||
$start = 1;
|
||||
$end = 100;
|
||||
|
||||
$intervalMinDate = (clone $startDate)->add(new DateInterval(sprintf('P%dD', $start)));
|
||||
$intervalMaxDate = (clone $startDate)->add(new DateInterval(sprintf('P%dD', $end)));
|
||||
$minDate = clone $startDate;
|
||||
$maxDate = clone $startDate;
|
||||
|
||||
$intervalMinDate = $minDate->add(new DateInterval(sprintf('P%dD', $start)));
|
||||
$intervalMaxDate = $maxDate->add(new DateInterval(sprintf('P%dD', $end)));
|
||||
|
||||
$randomDate = Date::getRandomDate();
|
||||
self::assertTrue($randomDate >= $intervalMinDate && $randomDate <= $intervalMaxDate);
|
||||
@@ -471,7 +480,9 @@ class DateTest extends TestCase
|
||||
public function testGetRandomDateIncorrectEnd(DateTime $startDate, $start, $end)
|
||||
{
|
||||
$randomDate = Date::getRandomDate($startDate, $start, $end);
|
||||
$intervalDate = (clone $startDate)->add(new DateInterval(sprintf('P%dD', $start)));
|
||||
|
||||
$cloned = clone $startDate;
|
||||
$intervalDate = $cloned->add(new DateInterval(sprintf('P%dD', $start)));
|
||||
|
||||
self::assertTrue($randomDate >= $intervalDate && $randomDate <= $intervalDate);
|
||||
}
|
||||
@@ -487,12 +498,44 @@ class DateTest extends TestCase
|
||||
{
|
||||
$randomDate = Date::getRandomDate($startDate, $start, $end);
|
||||
|
||||
$intervalMinDate = (clone $startDate)->add(new DateInterval(sprintf('P%dD', $start)));
|
||||
$intervalMaxDate = (clone $startDate)->add(new DateInterval(sprintf('P%dD', $end)));
|
||||
$minDate = clone $startDate;
|
||||
$maxDate = clone $startDate;
|
||||
|
||||
$intervalMinDate = $minDate->add(new DateInterval(sprintf('P%dD', $start)));
|
||||
$intervalMaxDate = $maxDate->add(new DateInterval(sprintf('P%dD', $end)));
|
||||
|
||||
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
|
||||
*
|
||||
@@ -745,4 +788,112 @@ class DateTest extends TestCase
|
||||
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)
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -6,19 +6,24 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Utilities\GeneratorUtility;
|
||||
use Meritoo\Common\Utilities\TestCase;
|
||||
|
||||
/**
|
||||
* Tests of the useful methods for the Generator class
|
||||
* Test case of the useful methods for the Generator class
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class GeneratorUtilityTest extends TestCase
|
||||
class GeneratorUtilityTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(GeneratorUtility::class);
|
||||
}
|
||||
|
||||
public function testGetGeneratorElements()
|
||||
{
|
||||
/*
|
||||
@@ -6,20 +6,25 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Utilities\Locale;
|
||||
use Meritoo\Common\Utilities\TestCase;
|
||||
|
||||
/**
|
||||
* Tests of the useful locale methods
|
||||
* Test case of the useful locale methods
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class LocaleTest extends TestCase
|
||||
class LocaleTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(Locale::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $languageCode Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
@@ -6,20 +6,25 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Utilities\MimeTypes;
|
||||
use Meritoo\Common\Utilities\TestCase;
|
||||
|
||||
/**
|
||||
* Tests of the useful methods for mime types of files
|
||||
* Test case of the useful methods for mime types of files
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class MimeTypesTest extends TestCase
|
||||
class MimeTypesTest extends BaseTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(MimeTypes::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $mimeType Empty value, e.g. ""
|
||||
* @dataProvider provideEmptyValue
|
||||
@@ -442,12 +447,12 @@ class MimeTypesTest extends TestCase
|
||||
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',
|
||||
];
|
||||
}
|
||||
@@ -460,12 +465,12 @@ class MimeTypesTest extends TestCase
|
||||
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,
|
||||
];
|
||||
}
|
||||
@@ -6,28 +6,33 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities;
|
||||
namespace Meritoo\Common\Test\Utilities;
|
||||
|
||||
use Generator;
|
||||
use Meritoo\Common\Exception\IncorrectColorHexLengthException;
|
||||
use Meritoo\Common\Exception\InvalidColorHexValueException;
|
||||
use Meritoo\Common\Exception\Regex\IncorrectColorHexLengthException;
|
||||
use Meritoo\Common\Exception\Regex\InvalidColorHexValueException;
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Utilities\Locale;
|
||||
use Meritoo\Common\Utilities\Miscellaneous;
|
||||
use Meritoo\Common\Utilities\TestCase;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Tests of the Miscellaneous methods (only static functions)
|
||||
* Test case of the Miscellaneous methods (only static functions)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class MiscellaneousTest extends TestCase
|
||||
class MiscellaneousTest extends BaseTestCase
|
||||
{
|
||||
private $stringSmall;
|
||||
private $stringCommaSeparated;
|
||||
private $stringDotSeparated;
|
||||
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertHasNoConstructor(Miscellaneous::class);
|
||||
}
|
||||
|
||||
public function testGetDirectoryContent()
|
||||
{
|
||||
$directoryPath = __DIR__ . '/../';
|
||||
@@ -154,8 +159,8 @@ class MiscellaneousTest extends TestCase
|
||||
$expected = "int(123)\n";
|
||||
|
||||
if ($xdebugLoaded) {
|
||||
$libraryPath = realpath(sprintf('%s%s', dirname(__FILE__), '/../../../../..'));
|
||||
$filePath = sprintf('%s%s', $libraryPath, '/src/Meritoo/Common/Utilities/Miscellaneous.php:');
|
||||
$libraryPath = realpath(sprintf('%s%s', dirname(__FILE__), '/../..'));
|
||||
$filePath = sprintf('%s%s', $libraryPath, '/src/Utilities/Miscellaneous.php:');
|
||||
|
||||
/*
|
||||
* Attention. I have to use "\d+" at the end of $filePath, because number of line may be different if new
|
||||
@@ -503,16 +508,35 @@ class MiscellaneousTest extends TestCase
|
||||
self::assertEquals('lorem ipsum', Miscellaneous::trimSmart(' lorem ipsum '));
|
||||
}
|
||||
|
||||
public function testConcatenatePaths()
|
||||
/**
|
||||
* @param mixed $emptyPaths Empty paths co concatenate
|
||||
* @dataProvider provideEmptyValue
|
||||
*/
|
||||
public function testConcatenatePathsWithEmptyPaths($emptyPaths)
|
||||
{
|
||||
self::assertEquals('', Miscellaneous::concatenatePaths($emptyPaths));
|
||||
}
|
||||
|
||||
public function testConcatenatePathsWithOneEmptyPath()
|
||||
{
|
||||
$paths = [
|
||||
'first/directory',
|
||||
'second/one',
|
||||
'',
|
||||
'and/the/third',
|
||||
];
|
||||
|
||||
$concatenated = Miscellaneous::concatenatePaths($paths);
|
||||
unset($paths[2]);
|
||||
$imploded = implode('/', $paths);
|
||||
|
||||
self::assertEquals('/' . $imploded, $concatenated);
|
||||
}
|
||||
|
||||
public function testConcatenatePathsInNixOs()
|
||||
{
|
||||
/*
|
||||
* Common cases
|
||||
*/
|
||||
self::assertEquals('', Miscellaneous::concatenatePaths(null));
|
||||
self::assertEquals('', Miscellaneous::concatenatePaths([]));
|
||||
|
||||
/*
|
||||
* *nix operating system
|
||||
* For *nix operating system
|
||||
*/
|
||||
$paths1 = [
|
||||
'first/directory',
|
||||
@@ -522,7 +546,10 @@ class MiscellaneousTest extends TestCase
|
||||
|
||||
self::assertEquals('/' . implode('/', $paths1), Miscellaneous::concatenatePaths($paths1));
|
||||
self::assertEquals('/' . implode('/', $paths1), Miscellaneous::concatenatePaths($paths1[0], $paths1[1], $paths1[2]));
|
||||
}
|
||||
|
||||
public function testConcatenatePathsInWindowsOs()
|
||||
{
|
||||
/*
|
||||
* For Windows operating system
|
||||
*/
|
||||
@@ -636,7 +663,8 @@ class MiscellaneousTest extends TestCase
|
||||
|
||||
public function testGetInvertedColorWithIncorrectLength()
|
||||
{
|
||||
$this->expectException(IncorrectColorHexLengthException::class);
|
||||
$this->setExpectedException(IncorrectColorHexLengthException::class);
|
||||
|
||||
Miscellaneous::getInvertedColor(null);
|
||||
Miscellaneous::getInvertedColor('');
|
||||
Miscellaneous::getInvertedColor(1);
|
||||
@@ -649,7 +677,8 @@ class MiscellaneousTest extends TestCase
|
||||
|
||||
public function testGetInvertedColorWithInvalidValue()
|
||||
{
|
||||
$this->expectException(InvalidColorHexValueException::class);
|
||||
$this->setExpectedException(InvalidColorHexValueException::class);
|
||||
|
||||
Miscellaneous::getInvertedColor('0011zz');
|
||||
Miscellaneous::getInvertedColor('001#zz');
|
||||
Miscellaneous::getInvertedColor('001!zz');
|
||||
@@ -720,6 +749,11 @@ class MiscellaneousTest extends TestCase
|
||||
self::assertSame($expected, Miscellaneous::fillMissingZeros($number, $length, $before));
|
||||
}
|
||||
|
||||
public function testGetProjectRootPath()
|
||||
{
|
||||
self::assertNotEmpty(Miscellaneous::getProjectRootPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides string to convert characters to latin characters and not lower cased and not human-readable
|
||||
*
|
||||
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),
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
33
tests/Utilities/Reflection/A.php
Normal file
33
tests/Utilities/Reflection/A.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?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 A class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class A
|
||||
{
|
||||
use E;
|
||||
|
||||
private $count = 1;
|
||||
|
||||
protected function lorem()
|
||||
{
|
||||
return 'ipsum';
|
||||
}
|
||||
|
||||
protected function getCount()
|
||||
{
|
||||
return $this->count;
|
||||
}
|
||||
}
|
||||
26
tests/Utilities/Reflection/B.php
Normal file
26
tests/Utilities/Reflection/B.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?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 B class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class B extends A implements I
|
||||
{
|
||||
protected $name = 'Lorem Ipsum';
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\Common\Tests\Utilities\Reflection;
|
||||
/**
|
||||
* (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 C class.
|
||||
20
tests/Utilities/Reflection/D.php
Normal file
20
tests/Utilities/Reflection/D.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 D class.
|
||||
* Used for testing the Reflection class.
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class D
|
||||
{
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user