Skip to content
Snippets Groups Projects
Commit 18c93480 authored by Marcin Kolanko's avatar Marcin Kolanko
Browse files

init

parent 8806abcd
No related branches found
No related tags found
1 merge request!1Devel
Showing
with 904 additions and 0 deletions
tests/ export-ignore
vendor/ export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.git/ export-ignore
.gitlab-ci.yml export-ignore
.idea export-ignore
apigen.neon export-ignore
build-coverage/ export-ignore
docs/ export-ignore
LICENSE.md export-ignore
phpcs.xml.dist export-ignore
phpunit-integration.xml export-ignore
phpunit-unit.xml export-ignore
/vendor/
.idea
composer.lock
build-coverage
swagger
\ No newline at end of file
variables:
DISABLE_FUNCTIONAL: 1
DISABLE_ACCEPTANCE: 1
DISABLE_PHP_5_5: 1
DISABLE_CODECEPTION: 1
DISABLE_INTEGRATION_TESTS: 1
IS_LIBRARY: 1
include: 'https://gitlab.com/wpdesk/gitlab-ci/raw/master/gitlab-ci-1.2.yml'
= 1.0 - 2019-12-16 =
* First release
{
"name": "wpdesk/wp-forms",
"authors": [
{
"name": "Marcin",
"email": "marcin@wpdesk.pl"
}
],
"require": {
"php": ">=5.6",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "<7",
"wp-coding-standards/wpcs": "^0.14.1",
"squizlabs/php_codesniffer": "^3.0.2",
"mockery/mockery": "*",
"10up/wp_mock": "*"
},
"autoload": {
"psr-4": {
"WPDesk\\Forms\\": "src/"
}
},
"autoload-dev": {
},
"scripts": {
"phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never",
"phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage",
"phpunit-integration": "phpunit --configuration phpunit-integration.xml --coverage-text --colors=never",
"phpunit-integration-fast": "phpunit --configuration phpunit-integration.xml --no-coverage"
}
}
<phpunit bootstrap="tests/integration/bootstrap.php"
backupGlobals="false"
>
<testsuites>
<testsuite>
<directory prefix="Test" suffix=".php">./tests/integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build-coverage/report.junit.xml"/>
<log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build-coverage/coverage.txt"/>
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
<php>
<env name="WP_DEVELOP_DIR" value="/tmp/wordpress-develop"/>
<env name="WC_DEVELOP_DIR" value="/tmp/woocommerce"/>
</php>
</phpunit>
\ No newline at end of file
<phpunit bootstrap="tests/unit/bootstrap.php">
<testsuites>
<testsuite>
<directory prefix="Test" suffix=".php">./tests/unit/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<logging>
<log type="junit" target="build-coverage/report.junit.xml"/>
<log type="coverage-html" target="build-coverage/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build-coverage/coverage.txt"/>
<log type="coverage-clover" target="build-coverage/clover.xml"/>
</logging>
</phpunit>
<?php
namespace WPDesk\Forms;
/**
* Abstraction layer for forms.
*
* @package WPDesk\Forms
*/
abstract class AbstractForm {
/**
* Unique form_id.
*
* @var string
*/
protected $form_id = 'form';
/**
* Updated data.
*
* @var array
*/
protected $updated_data = array();
/**
* Create form data and return an associative array.
*
* @return array
*/
abstract protected function create_form_data();
/**
* Add array to update data.
*
* @param array $new_data new data to update.
*/
public function update_form_data( array $new_data = array() ) {
$this->updated_data = $new_data;
}
/**
* Merge created and updated data and return associative array. Add to all keys form prefix.
*
* @return array
*/
public function get_form_data() {
return array_merge(
$this->create_form_data(),
$this->updated_data
);
}
/**
* return form Id
*
* @return string
*/
public function get_form_id() {
return $this->form_id;
}
}
<?php
namespace WPDesk\Forms;
/**
* Interface ConditionalFormInterface
*
* @package WPDesk\Forms
*/
interface ConditionalFormInterface
{
public function is_active();
}
<?php
namespace WPDesk\Forms;
/**
* FormsCollection class store AbstractForm instances and merges forms data from all collections
*
* @package WPDesk\Forms
*/
class FormsCollection {
/**
* AbstractForm array collection.
*
* @var AbstractForm[]
*/
protected $forms = array();
/**
* Add forms. All keys in this array must be unique, otherwise add_form will throw exception.
*
* @param AbstractForm[] $forms
*/
public function add_forms( array $forms = array() ) {
foreach ( $forms as $form_object ) {
$this->add_form( $form_object );
}
}
/**
* Add form. If key is not unique throw exception.
*
* @param AbstractForm $form
*
* @throws \OutOfBoundsException
*/
public function add_form( AbstractForm $form ) {
if ( ! $this->is_form_exists( $form->get_form_id() ) ) {
$this->forms[ $form->get_form_id() ] = $form;
} else {
throw new \OutOfBoundsException( 'Form with this key already exists' );
}
}
/**
* Is form exists. Checks if key exists in the array of forms and return bool.
*
* @param string $form_id
*
* @return bool
*/
public function is_form_exists( $form_id ) {
return isset( $this->forms[ (string) $form_id ] );
}
/**
* Get form.
*
* @param string $form_id
*
* @return AbstractForm
* @throws \OutOfRangeException
*/
public function get_form( $form_id ) {
if ( $this->is_form_exists( $form_id ) ) {
return $this->forms[ (string) $form_id ];
}
throw new \OutOfRangeException( 'Form with this key not exists' );
}
/**
* Get forms data. This method merge all arrays from forms and return associative array for woocommerce form_fields.
*
* @param bool $prefixed if true add form_id as prefix to form keys
*
* @return array
*/
public function get_forms_data( $prefixed = false) {
$forms_data = array();
foreach ( $this->forms as $form ) {
if ( $form instanceof ConditionalFormInterface ) {
if ( ! $form->is_active() ) {
continue;
}
}
if( $prefixed ){
$forms_data = array_merge( $forms_data, $this->get_prefixed_array( $form->get_form_data(), $form->get_form_id() ) );
}else {
$forms_data = array_merge( $forms_data, $form->get_form_data() );
}
}
return $forms_data;
}
/**
* Get prefixed array returns array with prefixed form_id
*
* @param array $array.
* @param string $form_id as prefix for array.
*
* @return array
*/
private function get_prefixed_array( array $array, $form_id ) {
return array_combine(
array_map( function ( $k ) use ($form_id) {
return $form_id . '_' . $k;
}, array_keys( $array ) ),
$array
);
}
}
<?php
namespace WPDesk\HttpClient\Curl;
use WPDesk\HttpClient\HttpClient;
use WPDesk\HttpClient\HttpClientResponse;
use WPDesk\HttpClient\HttpClientRequestException;
class CurlClient implements HttpClient {
/** @var resource */
private $curlResource;
/** @var string|null|boolean */
private $rawResponse;
/** @var int */
private $httpResponseCode;
/** @var array */
private $headers = array();
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
*
* @return HttpClientResponse
* @throws HttpClientRequestException
*/
public function get( $url, $body, array $headers, $timeOut ) {
return $this->send( $url, 'GET', $body, $headers, $timeOut );
}
/**
* @param string $url
* @param string $method
* @param string $body
* @param array $headers
* @param int $timeOut
*
* @return HttpClientResponse
* @throws HttpClientRequestException
*/
public function send( $url, $method, $body, array $headers, $timeOut ) {
$this->initResource();
try {
$this->prepareConnection( $url, $method, $body, $headers, $timeOut );
$this->sendRequest();
$this->throwExceptionIfError();
$this->closeConnection();
return $this->prepareResponse();
} catch ( \Exception $e ) {
$this->closeConnection();
if ( $e instanceof HttpClientRequestException ) {
throw $e;
}
throw CurlExceptionFactory::createDefaultException( $e->getMessage(), $e->getCode(), $e );
}
}
private function initResource() {
$this->curlResource = \curl_init();
}
/**
* Opens a new curl connection.
*
* @param string $url The endpoint to send the request to.
* @param string $method The request method.
* @param string $body The body of the request.
* @param array $headers The request headers.
* @param int $timeOut The timeout in seconds for the request.
*/
private function prepareConnection( $url, $method, $body, array $headers, $timeOut ) {
$options = [
\CURLOPT_CUSTOMREQUEST => $method,
\CURLOPT_HTTPHEADER => $this->compileRequestHeaders( $headers ),
\CURLOPT_URL => $url,
\CURLOPT_CONNECTTIMEOUT => 10,
\CURLOPT_TIMEOUT => $timeOut,
\CURLOPT_RETURNTRANSFER => \true,
// Return response as string
\CURLOPT_HEADER => \false,
// Enable header processing
\CURLOPT_SSL_VERIFYHOST => 2,
\CURLOPT_SSL_VERIFYPEER => \true,
\CURLOPT_HEADERFUNCTION => function ( $curl, $header ) {
$len = strlen( $header );
$this->headers[] = trim( $header );
return $len;
}
];
if ( $method !== "GET" ) {
$options[ \CURLOPT_POSTFIELDS ] = $body;
}
\curl_setopt_array( $this->curlResource, $options );
}
/**
* Compiles the request headers into a curl-friendly format.
*
* @param array $headers The request headers.
*
* @return array
*/
private function compileRequestHeaders( array $headers ) {
$return = [];
foreach ( $headers as $key => $value ) {
$return[] = $key . ': ' . $value;
}
return $return;
}
/**
* Send the request and get the raw response from curl
*/
private function sendRequest() {
$this->rawResponse = \curl_exec( $this->curlResource );
$this->httpResponseCode = $this->getHttpResponseCode();
}
/** @return int */
private function getHttpResponseCode() {
return \intval( \curl_getinfo( $this->curlResource, \CURLINFO_HTTP_CODE ) );
}
private function throwExceptionIfError() {
$errorNumber = \curl_errno( $this->curlResource );
if ( $errorNumber === 0 ) {
return;
}
$errorMessage = \curl_error( $this->curlResource );
throw CurlExceptionFactory::createCurlException( $errorMessage, $errorNumber );
}
/**
* Closes an existing curl connection
*/
private function closeConnection() {
\curl_close( $this->curlResource );
$this->curlResource = null;
}
private function prepareResponse() {
list( $rawHeaders, $rawBody ) = $this->extractResponseHeadersAndBody();
return new HttpClientResponse( $rawHeaders, $rawBody, $this->httpResponseCode );
}
/**
* Extracts the headers and the body into a two-part array
*
* @return array
*/
private function extractResponseHeadersAndBody() {
$rawBody = \trim( $this->rawResponse );
$rawHeaders = \trim( implode( "\r\n", $this->headers ) );
return [ $rawHeaders, $rawBody ];
}
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
*
* @return HttpClientResponse
* @throws HttpClientRequestException
*/
public function post( $url, $body, array $headers, $timeOut ) {
return $this->send( $url, 'POST', $body, $headers, $timeOut );
}
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
*
* @return HttpClientResponse
* @throws HttpClientRequestException
*/
public function delete( $url, $body, array $headers, $timeOut ) {
return $this->send( $url, 'DELETE', $body, $headers, $timeOut );
}
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
*
* @return HttpClientResponse
* @throws HttpClientRequestException
*/
public function put( $url, $body, array $headers, $timeOut ) {
return $this->send( $url, 'PUT', $body, $headers, $timeOut );
}
}
<?php
namespace WPDesk\HttpClient\Curl;
use WPDesk\HttpClient\Curl\Exception\CurlException;
use WPDesk\HttpClient\Curl\Exception\CurlTimedOutException;
class CurlExceptionFactory
{
/**
* Convert curl code to appropriate exception class.
*
* @param int $curlCode Code from https://curl.haxx.se/libcurl/c/libcurl-errors.html
* @param string $curlMessage
* @return CurlException
*/
public static function createCurlException($curlCode, $curlMessage) {
switch ($curlCode) {
case CURLE_OPERATION_TIMEDOUT:
return new CurlTimedOutException($curlMessage, $curlCode);
default:
return self::createDefaultException($curlMessage, $curlCode);
}
}
/**
* Creates default Curl exception
*
* @param $code
* @param $message
* @param \Exception|null $prev
* @return CurlException
*/
public static function createDefaultException($code, $message, \Exception $prev = null) {
return new CurlException('Default exception: ' . $message, $code, $prev);
}
}
\ No newline at end of file
<?php
namespace WPDesk\HttpClient\Curl\Exception;
use WPDesk\HttpClient\HttpClientRequestException;
/**
* Base class for all curl exceptions.
*
* @package WPDesk\HttpClient\Curl\Exception
*/
class CurlException extends \RuntimeException implements HttpClientRequestException
{
}
\ No newline at end of file
<?php
namespace WPDesk\HttpClient\Curl\Exception;
class CurlTimedOutException extends CurlException
{
}
\ No newline at end of file
<?php
namespace WPDesk\HttpClient;
interface HttpClient
{
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
* @throw HttpClientRequestException
* @return HttpClientResponse
*/
public function put($url, $body, array $headers, $timeOut);
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
* @throw HttpClientRequestException
* @return HttpClientResponse
*/
public function get($url, $body, array $headers, $timeOut);
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
* @throw HttpClientRequestException
* @return HttpClientResponse
*/
public function post($url, $body, array $headers, $timeOut);
/**
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
* @throw HttpClientRequestException
* @return HttpClientResponse
*/
public function delete($url, $body, array $headers, $timeOut);
/**
* @param string $url
* @param string $method
* @param string $body
* @param array $headers
* @param int $timeOut
* @throw HttpClientRequestException
* @return HttpClientResponse
*/
public function send($url, $method, $body, array $headers, $timeOut);
}
\ No newline at end of file
<?php
namespace WPDesk\HttpClient;
class HttpClientFactory
{
/**
* @param HttpClientOptions $options
* @return HttpClient
*/
public function createClient(HttpClientOptions $options)
{
$className = $options->getHttpClientClass();
return new $className;
}
}
\ No newline at end of file
<?php
namespace WPDesk\HttpClient;
interface HttpClientOptions
{
/**
* @return string
*/
public function getHttpClientClass();
}
\ No newline at end of file
<?php
namespace WPDesk\HttpClient;
/**
* Interface for all exceptions that can be thrown by HttpClient
*
* @package WPDesk\HttpClient
*/
interface HttpClientRequestException
{
}
\ No newline at end of file
<?php
namespace WPDesk\HttpClient;
class HttpClientResponse
{
/** @var string */
private $headers;
/** @var string */
private $body;
/** @var int */
private $code;
/**
* HttpClientResponse constructor.
* @param string $headers
* @param string $body
* @param int $code
*/
public function __construct($headers, $body, $code)
{
$this->headers = $headers;
$this->body = $body;
$this->code = $code;
}
/**
* @return array
*/
public function getHeaders()
{
$headers = array();
$headers_rows = explode("\r\n", $this->headers);
foreach ($headers_rows as $headers_row) {
$header = explode(": ", $headers_row);
$headers[$header[0]] = isset($header[1])?$header[1]:$header[0];
}
return $headers;
}
/**
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* @return int
*/
public function getResponseCode()
{
return $this->code;
}
}
\ No newline at end of file
version: '2.0'
services:
wordpress:
image: wpdesknet/phpunit-woocommerce:0-0
volumes:
- .././:/opt/project
depends_on:
- mysql0
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql0
wordpress-0-1:
image: wpdesknet/phpunit-woocommerce:0-1
volumes:
- .././:/opt/project
depends_on:
- mysql1
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql1
wordpress-0-2:
image: wpdesknet/phpunit-woocommerce:0-2
volumes:
- .././:/opt/project
depends_on:
- mysql2
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql2
wordpress-0-3:
image: wpdesknet/phpunit-woocommerce:0-3
volumes:
- .././:/opt/project
depends_on:
- mysql3
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql3
wordpress-0-4:
image: wpdesknet/phpunit-woocommerce:0-4
volumes:
- .././:/opt/project
depends_on:
- mysql4
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql4
wordpress-0-5:
image: wpdesknet/phpunit-woocommerce:0-5
volumes:
- .././:/opt/project
depends_on:
- mysql5
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql5
wordpress-1-0:
image: wpdesknet/phpunit-woocommerce:1-0
volumes:
- .././:/opt/project
depends_on:
- mysql0
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql0
wordpress-2-0:
image: wpdesknet/phpunit-woocommerce:2-0
volumes:
- .././:/opt/project
depends_on:
- mysql0
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql0
wordpress-3-0:
image: wpdesknet/phpunit-woocommerce:3-0
volumes:
- .././:/opt/project
depends_on:
- mysql0
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql0
wordpress-4-0:
image: wpdesknet/phpunit-woocommerce:4-0
volumes:
- .././:/opt/project
depends_on:
- mysql0
environment:
WORDPRESS_DB_NAME: wptest
WORDPRESS_DB_USER: mysql
WORDPRESS_DB_PASSWORD: mysql
WORDPRESS_DB_HOST: mysql0
mysql0:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: mysql
MYSQL_DATABASE: wptest
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
mysql1:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: mysql
MYSQL_DATABASE: wptest
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
mysql2:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: mysql
MYSQL_DATABASE: wptest
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
mysql3:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: mysql
MYSQL_DATABASE: wptest
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
mysql4:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: mysql
MYSQL_DATABASE: wptest
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
mysql5:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: mysql
MYSQL_DATABASE: wptest
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment