diff --git a/src/HS/Builder/DeleteQueryBuilder.php b/src/HS/Builder/DeleteQueryBuilder.php index 34aae51..b77d6a5 100644 --- a/src/HS/Builder/DeleteQueryBuilder.php +++ b/src/HS/Builder/DeleteQueryBuilder.php @@ -1,7 +1,7 @@ @@ -27,7 +27,7 @@ public function getQueryClassPath() * @param array $list * * @return $this - * @throws WrongParameterException + * @throws InvalidArgumentException */ public function where($comparison, array $list) { diff --git a/src/HS/Builder/FindQueryBuilderAbstract.php b/src/HS/Builder/FindQueryBuilderAbstract.php index 20086bb..4881003 100644 --- a/src/HS/Builder/FindQueryBuilderAbstract.php +++ b/src/HS/Builder/FindQueryBuilderAbstract.php @@ -4,7 +4,7 @@ use HS\Component\Comparison; use HS\Component\Filter; use HS\Component\InList; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; /** * @author KonstantinKuklin @@ -68,7 +68,7 @@ public function getColumnList() * @param array $list * * @return $this - * @throws WrongParameterException + * @throws InvalidArgumentException */ public function where($comparison, array $list) { @@ -79,7 +79,7 @@ public function where($comparison, array $list) for ($i = 0, $countWhere = count($list); $i < $countWhere; $i++) { $key = $columnList[$i]; if (!isset($list[$key])) { - throw new WrongParameterException( + throw new InvalidArgumentException( "The key`s must be set with out skip on select( key1, key2). Where(key2,key1)" ); } @@ -95,7 +95,7 @@ public function where($comparison, array $list) * @param array $values * * @return $this - * @throws \Exception + * @throws InvalidArgumentException */ public function whereIn($key, array $values) { @@ -104,7 +104,7 @@ public function whereIn($key, array $values) $this->getParameterBag()->setParameter('keyList', array(1)); if (false === $index = array_search($key, $columnList)) { - throw new WrongParameterException("Can't find key in columns list."); + throw new InvalidArgumentException("Can't find key in columns list."); } $inList = new InList($index, $values); $this->getParameterBag()->setParameter('inKeyList', $inList); diff --git a/src/HS/Builder/IncrementQueryBuilder.php b/src/HS/Builder/IncrementQueryBuilder.php index 4296347..c64e1f0 100644 --- a/src/HS/Builder/IncrementQueryBuilder.php +++ b/src/HS/Builder/IncrementQueryBuilder.php @@ -2,7 +2,7 @@ namespace HS\Builder; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; /** * @author KonstantinKuklin @@ -12,7 +12,7 @@ class IncrementQueryBuilder extends UpdateQueryBuilder /** * @param array $incrementList * - * @throws WrongParameterException + * @throws InvalidArgumentException */ public function __construct(array $incrementList) { @@ -26,7 +26,7 @@ public function __construct(array $incrementList) $columnList[] = $value; $valueList[] = 1; } else { - throw new WrongParameterException("Wrong increment parameter."); + throw new InvalidArgumentException("Wrong increment parameter."); } } diff --git a/src/HS/CommonClient.php b/src/HS/CommonClient.php index 8111ffd..1eaffd3 100644 --- a/src/HS/CommonClient.php +++ b/src/HS/CommonClient.php @@ -4,7 +4,7 @@ use HS\Builder\QueryBuilderInterface; use HS\Exception\Exception; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\Query\OpenIndexQuery; use HS\Query\QueryInterface; use PHP_Timer; @@ -39,9 +39,6 @@ abstract class CommonClient /** @var array */ private $keysList = array(); - /** @var Driver */ - private $driver = null; - /** @var null|string */ private $authKey = null; @@ -50,6 +47,15 @@ abstract class CommonClient public $debugResultList = array(); + /** + * @param string $authKey + */ + abstract public function authenticate($authKey); + + abstract public function getIndexId( + $dbName, $tableName, $indexName, array $columnList, $returnOnlyId = true, array $filterColumnList = array() + ); + /** * @param string $url * @param int $port @@ -63,8 +69,7 @@ public function __construct($url, $port, $authKey = null, $debug = false) } $this->authKey = $authKey; - $this->driver = new Driver(); - $this->stream = new Stream($url, Connection::PROTOCOL_TCP, $port, $this->driver); + $this->stream = new Stream($url, Connection::PROTOCOL_TCP, $port); $this->stream->open(); $this->stream->setBlockingOff(); $this->stream->setReadTimeOut(0, (float)500000); @@ -101,8 +106,8 @@ public function getResultList() } $this->getStream()->isReadyForReading(); try { - $result = $this->getStream()->getContents(); - $query->setResultData($result); + $resultList = Driver::prepareReceiveDataStatic($this->getStream()->getContents()); + $query->setResultData($resultList); $ResultObject = $query->getResult(); @@ -118,7 +123,7 @@ public function getResultList() $resultsList[] = $ResultObject; // add time to general time counter } catch (ReadStreamException $e) { - // TODO check + throw new Exception("Read stream error. Can't read from stream. URL: " . $this->getUrlConnection()); } } @@ -239,7 +244,7 @@ protected function isQueryQueueEmpty() */ protected function sendQuery(QueryInterface $query) { - if ($this->getStream()->sendContents($query->getQueryParameters()) > 0) { + if ($this->getStream()->sendContents($query->getQueryString() . Driver::EOL) > 0) { // increment count of queries $this->countQueries++; @@ -261,13 +266,13 @@ protected function setKeysToIndexId($indexId, $keys) /** * @param int $indexId * - * @throws WrongParameterException + * @throws InvalidArgumentException * @return array */ protected function getKeysByIndexId($indexId) { if (!array_key_exists($indexId, $this->keysList)) { - throw new WrongParameterException(sprintf("Don't find any Index with this indexId:%d.", $indexId)); + throw new InvalidArgumentException(sprintf("Don't find any Index with this indexId:%d.", $indexId)); } return $this->keysList[$indexId]; @@ -319,7 +324,7 @@ protected function addIndexIdToArray($indexMapValue, $indexId) } /** - * @throws WrongParameterException + * @throws InvalidArgumentException */ private function authenticateWithConstructor() { diff --git a/src/HS/Component/Filter.php b/src/HS/Component/Filter.php index 39ed2bc..2b5b254 100644 --- a/src/HS/Component/Filter.php +++ b/src/HS/Component/Filter.php @@ -6,7 +6,7 @@ namespace HS\Component; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; class Filter { @@ -24,7 +24,7 @@ class Filter * @param string $key * @param string $type * - * @throws WrongParameterException + * @throws InvalidArgumentException */ public function __construct($comparison, $position, $key, $type = self::FILTER_TYPE_SKIP) { @@ -35,14 +35,14 @@ public function __construct($comparison, $position, $key, $type = self::FILTER_T } if (!is_numeric($position)) { - throw new WrongParameterException("Position must be numeric"); + throw new InvalidArgumentException("Position must be numeric"); } $this->position = (int)$position; $this->key = $key; if ($type === self::FILTER_TYPE_SKIP || $type === self::FILTER_TYPE_STOP) { $this->type = $type; } else { - throw new WrongParameterException("Filter type is wrong must be 'F' or 'W'."); + throw new InvalidArgumentException("Filter type is wrong must be 'F' or 'W'."); } } diff --git a/src/HS/Component/InList.php b/src/HS/Component/InList.php index 53035e7..76f5474 100644 --- a/src/HS/Component/InList.php +++ b/src/HS/Component/InList.php @@ -5,7 +5,7 @@ namespace HS\Component; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; class InList { @@ -16,12 +16,12 @@ class InList * @param int $position * @param array $keyList * - * @throws WrongParameterException + * @throws InvalidArgumentException */ public function __construct($position, array $keyList) { if (!is_numeric($position)) { - throw new WrongParameterException("ColumnNumber must be a number"); + throw new InvalidArgumentException("ColumnNumber must be a number"); } $this->position = $position; $this->keyList = $keyList; diff --git a/src/HS/Driver.php b/src/HS/Driver.php index b675764..96c6c40 100644 --- a/src/HS/Driver.php +++ b/src/HS/Driver.php @@ -64,7 +64,7 @@ public static function prepareSendDataStatic($data) { $encodedData = array_map('self::encodeData', $data); - return implode(self::DELIMITER, $encodedData) . self::EOL; + return implode(self::DELIMITER, $encodedData); } /** diff --git a/src/HS/Exception/ComparisonException.php b/src/HS/Exception/ComparisonException.php index 7b054f8..df25ea7 100644 --- a/src/HS/Exception/ComparisonException.php +++ b/src/HS/Exception/ComparisonException.php @@ -4,6 +4,6 @@ */ namespace HS\Exception; -class ComparisonException extends WrongParameterException +class ComparisonException extends InvalidArgumentException { } \ No newline at end of file diff --git a/src/HS/Exception/WrongParameterException.php b/src/HS/Exception/InvalidArgumentException.php similarity index 67% rename from src/HS/Exception/WrongParameterException.php rename to src/HS/Exception/InvalidArgumentException.php index b403822..610dcc3 100644 --- a/src/HS/Exception/WrongParameterException.php +++ b/src/HS/Exception/InvalidArgumentException.php @@ -5,6 +5,6 @@ namespace HS\Exception; -class WrongParameterException extends Exception +class InvalidArgumentException extends Exception { } \ No newline at end of file diff --git a/src/HS/Query/AuthQuery.php b/src/HS/Query/AuthQuery.php index c456465..f00374d 100644 --- a/src/HS/Query/AuthQuery.php +++ b/src/HS/Query/AuthQuery.php @@ -5,17 +5,15 @@ namespace HS\Query; +use HS\Driver; + class AuthQuery extends QueryAbstract { /** * {@inheritdoc} */ - public function getQueryParameters() + public function getQueryString() { - return array( - 'A', - '1', // - $this->getParameter('authKey') // - ); + return sprintf("A" . Driver::DELIMITER . "1" . Driver::DELIMITER, $this->getParameter('authKey')); } } \ No newline at end of file diff --git a/src/HS/Query/InsertQuery.php b/src/HS/Query/InsertQuery.php index 96629cd..f6a7019 100644 --- a/src/HS/Query/InsertQuery.php +++ b/src/HS/Query/InsertQuery.php @@ -4,6 +4,7 @@ */ namespace HS\Query; +use HS\Driver; use HS\Result\InsertResult; class InsertQuery extends QueryAbstract @@ -11,21 +12,21 @@ class InsertQuery extends QueryAbstract /** * {@inheritdoc} */ - public function getQueryParameters() + public function getQueryString() { $valueList = $this->getParameter('valueList', array()); - $returnList = array( + $queryString = sprintf( + "%d" . Driver::DELIMITER . "+" . Driver::DELIMITER . "%d", $this->getIndexId(), - '+', count($valueList[0]) ); foreach ($valueList as $row) { - $returnList = array_merge($returnList, $row); + $queryString .= "\t" . Driver::prepareSendDataStatic($row); } - return $returnList; + return $queryString; } /** diff --git a/src/HS/Query/ModifyQueryAbstract.php b/src/HS/Query/ModifyQueryAbstract.php index 0edcec7..1e107fe 100644 --- a/src/HS/Query/ModifyQueryAbstract.php +++ b/src/HS/Query/ModifyQueryAbstract.php @@ -5,6 +5,8 @@ namespace HS\Query; +use HS\Driver; + abstract class ModifyQueryAbstract extends SelectQuery { abstract public function getModificator(); @@ -20,17 +22,16 @@ public function isSuffics() /** * {@inheritdoc} */ - public function getQueryParameters() + public function getQueryString() { - $parameters = parent::getQueryParameters(); + $queryString = parent::getQueryString(); $modificator = $this->getModificator(); if ($this->isSuffics()) { $modificator .= '?'; } + $queryString .= Driver::DELIMITER . $modificator . Driver::DELIMITER; - $parameters[] = $modificator; - - return $parameters; + return $queryString; } } \ No newline at end of file diff --git a/src/HS/Query/ModifyStepQueryAbstract.php b/src/HS/Query/ModifyStepQueryAbstract.php index d8d88ed..c7e3039 100644 --- a/src/HS/Query/ModifyStepQueryAbstract.php +++ b/src/HS/Query/ModifyStepQueryAbstract.php @@ -5,15 +5,20 @@ namespace HS\Query; +use HS\Driver; + abstract class ModifyStepQueryAbstract extends ModifyQueryAbstract { /** * {@inheritdoc} */ - public function getQueryParameters() + public function getQueryString() { - $parameters = parent::getQueryParameters(); - $parameters = array_merge($parameters, $this->getParameter('valueList', array())); - return $parameters; + $queryString = parent::getQueryString(); + if (($valueList = $this->getParameter('valueList', array())) && !empty($valueList)) { + $queryString .= Driver::prepareSendDataStatic($valueList); + } + + return $queryString; } } \ No newline at end of file diff --git a/src/HS/Query/OpenIndexQuery.php b/src/HS/Query/OpenIndexQuery.php index 929efa9..b3ad464 100644 --- a/src/HS/Query/OpenIndexQuery.php +++ b/src/HS/Query/OpenIndexQuery.php @@ -22,10 +22,9 @@ public function __construct(array $parameterList) /** * {@inheritdoc} */ - public function getQueryParameters() + public function getQueryString() { - return array( - 'P', + return sprintf("P\t%d\t%s\t%s\t%s\t%s\t%s", $this->getParameter('indexId'), $this->getParameter('dbName'), $this->getParameter('tableName'), diff --git a/src/HS/Query/QueryAbstract.php b/src/HS/Query/QueryAbstract.php index 74fd31f..dd1b4c9 100644 --- a/src/HS/Query/QueryAbstract.php +++ b/src/HS/Query/QueryAbstract.php @@ -6,8 +6,7 @@ namespace HS\Query; use HS\Component\ParameterBag; -use HS\Driver; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\ReaderInterface; use HS\Result\SelectResult; use HS\Validator; @@ -27,11 +26,6 @@ abstract class QueryAbstract implements QueryInterface 'HS\Query\DecrementQuery' => 'HS\Result\DecrementResult', ); - /** - * {@inheritdoc} - */ - abstract public function getQueryParameters(); - /** * @param array $parameterList */ @@ -85,10 +79,7 @@ public function getResult() /** * {@inheritdoc} */ - public function getQueryString() - { - return Driver::prepareSendDataStatic($this->getQueryParameters()); - } + abstract public function getQueryString(); /** * @return int @@ -115,7 +106,7 @@ public function setResultData($data) /** * @return $this - * @throws WrongParameterException + * @throws InvalidArgumentException */ public function execute() { @@ -142,14 +133,14 @@ protected function getParameterBag() } /** - * @throws WrongParameterException + * @throws InvalidArgumentException * @return ReaderInterface */ protected function getSocket() { $socket = $this->getParameter('socket'); if (!($socket instanceof ReaderInterface)) { - throw new WrongParameterException('Socket not found'); + throw new InvalidArgumentException('Socket not found'); } return $socket; diff --git a/src/HS/Query/QueryInterface.php b/src/HS/Query/QueryInterface.php index c426e6d..21eb5bd 100644 --- a/src/HS/Query/QueryInterface.php +++ b/src/HS/Query/QueryInterface.php @@ -12,11 +12,6 @@ interface QueryInterface */ public function getResult(); - /** - * @return array - */ - public function getQueryParameters(); - /** * @return string */ diff --git a/src/HS/Query/SelectQuery.php b/src/HS/Query/SelectQuery.php index 5be7a95..5c33036 100644 --- a/src/HS/Query/SelectQuery.php +++ b/src/HS/Query/SelectQuery.php @@ -8,14 +8,15 @@ use HS\Component\ComparisonInterface; use HS\Component\Filter; use HS\Component\InList; -use HS\Exception\WrongParameterException; +use HS\Driver; +use HS\Exception\InvalidArgumentException; class SelectQuery extends QueryAbstract { const VECTOR = 1; const ASSOC = 2; -// const OBJECT = 3; +// const OBJECT = 3; TODO public function getReturnType() { @@ -25,18 +26,18 @@ public function getReturnType() /** * @param int $type * - * @throws WrongParameterException + * @throws InvalidArgumentException */ public function setReturnType($type) { if ($type === self::ASSOC || $type === self::VECTOR -// || $type === self::OBJECT +// || $type === self::OBJECT TODO ) { $this->getParameterBag()->setParameter('returnType', $type); } else { - throw new WrongParameterException("Got unknown return type."); + throw new InvalidArgumentException("Got unknown return type."); } } @@ -73,44 +74,50 @@ public function getOffset() } /** - * @return array + * @return string */ public function getIn() { - /** @var InList $inKeyList */ $inKeyList = $this->getParameter('inKeyList'); if ($inKeyList === null) { - return array(); + return ''; } - $output[] = '@'; - - return array_merge( - array( - '@', - $inKeyList->getPosition(), - $inKeyList->getCount() - ), - $inKeyList->getKeyList() + return sprintf( + Driver::DELIMITER . "@" . // in marker + Driver::DELIMITER . "%d" . // position + Driver::DELIMITER . "%d" . // count + Driver::DELIMITER . "%s", // key list + $inKeyList->getPosition(), + $inKeyList->getCount(), + Driver::prepareSendDataStatic($inKeyList->getKeyList()) ); - } + /** + * @return string + */ public function getFilterList() { /** @var Filter[] $filterList */ $filterList = $this->getParameter('filterList'); if ($filterList === null) { - return array(); + return ''; } - $output = array(); + $output = ''; foreach ($filterList as $filter) { - $output[] = $filter->getType(); - $output[] = $filter->getComparison(); - $output[] = $filter->getPosition(); - $output[] = $filter->getKey(); + $output .= sprintf( + Driver::DELIMITER . "%s" . // type + Driver::DELIMITER . "%s" . // comparison + Driver::DELIMITER . "%d" . // position + Driver::DELIMITER . "%s", // key + $filter->getType(), + $filter->getComparison(), + $filter->getPosition(), + $filter->getKey() + ); } return $output; @@ -119,21 +126,25 @@ public function getFilterList() /** * {@inheritdoc} */ - public function getQueryParameters() + public function getQueryString() { // ... [LIM] [IN] [FILTER ...] - return array_merge( - array( - $this->getIndexId(), - $this->getComparison(), - count($this->getKeyList()), - ), - $this->getKeyList(), - array( - $this->getLimit(), - $this->getOffset() - ), + return sprintf( + "%d" . Driver::DELIMITER . // index + "%s" . Driver::DELIMITER . // comparison + "%d" . Driver::DELIMITER . // key list count + "%s" . Driver::DELIMITER . // key list + "%d" . Driver::DELIMITER . // limit + "%d" . // offset + "%s" . // in list + "%s", // filter list + $this->getIndexId(), + $this->getComparison(), + count($this->getKeyList()), + Driver::prepareSendDataStatic($this->getKeyList()), + $this->getLimit(), + $this->getOffset(), $this->getIn(), $this->getFilterList() ); diff --git a/src/HS/Query/TextQuery.php b/src/HS/Query/TextQuery.php index 9397a50..a8fc0ed 100644 --- a/src/HS/Query/TextQuery.php +++ b/src/HS/Query/TextQuery.php @@ -20,8 +20,8 @@ public function __construct(array $parameterList, $queryObject) /** * {@inheritdoc} */ - public function getQueryParameters() + public function getQueryString() { - return array($this->getParameter('text')); + return $this->getParameter('text'); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/HS/Query/UpdateQuery.php b/src/HS/Query/UpdateQuery.php index 7741811..5d87316 100644 --- a/src/HS/Query/UpdateQuery.php +++ b/src/HS/Query/UpdateQuery.php @@ -5,6 +5,8 @@ namespace HS\Query; +use HS\Driver; + class UpdateQuery extends ModifyQueryAbstract { public function getModificator() @@ -15,12 +17,15 @@ public function getModificator() /** * {@inheritdoc} */ - public function getQueryParameters() + public function getQueryString() { - $parameters = parent::getQueryParameters(); + $queryString = parent::getQueryString(); $valueList = $this->getParameter('valueList', array()); + if (!empty($valueList)) { + $queryString .= Driver::prepareSendDataStatic($valueList); + } - return array_merge($parameters, $valueList); + return $queryString; } } \ No newline at end of file diff --git a/src/HS/Reader.php b/src/HS/Reader.php index 2643311..2ebf456 100644 --- a/src/HS/Reader.php +++ b/src/HS/Reader.php @@ -5,7 +5,7 @@ use HS\Component\Comparison; use HS\Component\Filter; use HS\Component\InList; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\Query\AuthQuery; use HS\Query\OpenIndexQuery; use HS\Query\SelectQuery; @@ -22,7 +22,7 @@ class Reader extends CommonClient implements ReaderInterface public function authenticate($authKey) { if (!is_string($authKey) || is_string($authKey) && strlen($authKey) < 1) { - throw new WrongParameterException( + throw new InvalidArgumentException( sprintf( "Authenticate command require string password value, but got %s with value %s.", gettype($authKey), @@ -133,7 +133,7 @@ public function selectByIndex( public function selectInByIndex($indexId, $in, $offset = null, $limit = null, array $filterList = array()) { if ($limit !== null && $limit < count($in)) { - throw new WrongParameterException("Limit must be > count of in"); + throw new InvalidArgumentException("Limit must be > count of in"); } $selectQuery = new SelectQuery( @@ -208,7 +208,7 @@ public function select( * @param int $offset * @param int $limit * - * @throws WrongParameterException + * @throws InvalidArgumentException * @return SelectQuery */ public function selectIn( @@ -224,7 +224,7 @@ public function selectIn( /** @var int $indexId */ if ($limit !== null && $limit < count($in)) { - throw new WrongParameterException("Limit must be > count of in"); + throw new InvalidArgumentException("Limit must be > count of in"); } $selectQuery = new SelectQuery( diff --git a/src/HS/ReaderHSInterface.php b/src/HS/ReaderHSInterface.php index ef34649..3e6f948 100644 --- a/src/HS/ReaderHSInterface.php +++ b/src/HS/ReaderHSInterface.php @@ -6,7 +6,7 @@ use HS\Query\SelectQuery; use HS\Query\OpenIndexQuery; -use HS\Exceptions\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\Query\AuthQuery; @@ -18,7 +18,7 @@ interface ReaderHSInterface /** * @param string $authKey * - * @throws WrongParameterException + * @throws InvalidArgumentException * @return AuthQuery */ public function authenticate($authKey); diff --git a/src/HS/Result/ResultAbstract.php b/src/HS/Result/ResultAbstract.php index faf3120..fb02702 100644 --- a/src/HS/Result/ResultAbstract.php +++ b/src/HS/Result/ResultAbstract.php @@ -48,6 +48,23 @@ abstract class ResultAbstract implements ResultInterface * @param QueryInterface $query * @param array $data * @param null|OpenIndexQuery $openIndexQuery + * + * @throws AuthenticationError + * @throws ColumnParseError + * @throws CommandError + * @throws ComparisonOperatorError + * @throws Error + * @throws FilterColumnError + * @throws FilterTypeError + * @throws InListSizeError + * @throws IndexOverFlowError + * @throws InternalMysqlError + * @throws KeyIndexError + * @throws KeyLengthError + * @throws LockTableError + * @throws OpenTableError + * @throws ReadOnlyError + * @throws UnknownError */ public function __construct(QueryInterface $query, array &$data, $openIndexQuery = null) { @@ -121,6 +138,10 @@ public function __construct(QueryInterface $query, array &$data, $openIndexQuery break; } } + // if got error + if ($this->error !== null) { + throw $this->error; + } $this->data = $data; } @@ -129,6 +150,7 @@ public function __construct(QueryInterface $query, array &$data, $openIndexQuery */ public function isSuccessfully() { + // check depended result if ($this->openIndexQuery !== null && !$this->openIndexQuery->getResult()->isSuccessfully()) { return false; } diff --git a/src/HS/Validator.php b/src/HS/Validator.php index 5786a10..8d80f3a 100644 --- a/src/HS/Validator.php +++ b/src/HS/Validator.php @@ -5,7 +5,7 @@ namespace HS; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; class Validator { @@ -13,65 +13,65 @@ class Validator /** * @param int $indexId * - * @throws WrongParameterException + * @throws InvalidArgumentException * @return void */ public static function validateIndexId($indexId) { if (!self::validateInt($indexId)) { - self::getWrongParameterException("Wrong indexId value, must be integer >= 0.", $indexId); + self::getInvalidArgumentException("Wrong indexId value, must be integer >= 0.", $indexId); } } /** * @param string $dbName * - * @throws WrongParameterException + * @throws InvalidArgumentException * @return void */ public static function validateDbName($dbName) { if (!self::validateString($dbName)) { - self::getWrongParameterException("Wrong dbName value, must be string and length > 0.", $dbName); + self::getInvalidArgumentException("Wrong dbName value, must be string and length > 0.", $dbName); } } /** * @param string $indexName * - * @throws WrongParameterException + * @throws InvalidArgumentException * @return void */ public static function validateIndexName($indexName) { if (!self::validateString($indexName)) { - self::getWrongParameterException("Wrong indexName value, must be string and length > 0.", $indexName); + self::getInvalidArgumentException("Wrong indexName value, must be string and length > 0.", $indexName); } } /** * @param string $tableName * - * @throws WrongParameterException + * @throws InvalidArgumentException * @return void */ public static function validateTableName($tableName) { if (!self::validateString($tableName)) { - self::getWrongParameterException("Wrong tableName value, must be string and length > 0.", $tableName); + self::getInvalidArgumentException("Wrong tableName value, must be string and length > 0.", $tableName); } } /** * @param array $columnList * - * @throws WrongParameterException + * @throws InvalidArgumentException * @return void */ public static function validateColumnList(array $columnList) { if (!self::validateArray($columnList, true)) { - self::getWrongParameterException("Wrong columnList, must be array and length > 0.", $columnList); + self::getInvalidArgumentException("Wrong columnList, must be array and length > 0.", $columnList); } } @@ -79,11 +79,11 @@ public static function validateColumnList(array $columnList) * @param string $message * @param mixed $data * - * @throws WrongParameterException + * @throws InvalidArgumentException */ - public static function getWrongParameterException($message, $data) + public static function getInvalidArgumentException($message, $data) { - throw new WrongParameterException( + throw new InvalidArgumentException( $message . sprintf( "Got %s with values %s.", gettype($data), diff --git a/tests/HS/Builder/DecrementQueryBuilderTest.php b/tests/HS/Builder/DecrementQueryBuilderTest.php index 695b6af..972fac4 100644 --- a/tests/HS/Builder/DecrementQueryBuilderTest.php +++ b/tests/HS/Builder/DecrementQueryBuilderTest.php @@ -6,7 +6,7 @@ namespace HS\Tests\Builder; use HS\Component\Comparison; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\QueryBuilder; use HS\Tests\TestCommon; @@ -47,7 +47,7 @@ public function testDecrementException() ->fromDataBase($this->getDatabase()) ->fromTable($this->getTableName()) ->where(Comparison::EQUAL, array('key' => 105)); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return true; } $this->fail('Not fall decrementBuilder with wrong parameters.'); diff --git a/tests/HS/Builder/IncrementQueryBuilderTest.php b/tests/HS/Builder/IncrementQueryBuilderTest.php index 5baea0b..b1c66a4 100644 --- a/tests/HS/Builder/IncrementQueryBuilderTest.php +++ b/tests/HS/Builder/IncrementQueryBuilderTest.php @@ -6,7 +6,7 @@ namespace HS\Tests\Builder; use HS\Component\Comparison; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\QueryBuilder; use HS\Tests\TestCommon; @@ -47,10 +47,10 @@ public function testIncrementException() ->fromDataBase($this->getDatabase()) ->fromTable($this->getTableName()) ->where(Comparison::EQUAL, array('key' => 104)); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return true; } $this->fail('Not fall incrementBuilder with wrong parameters.'); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/tests/HS/Builder/SelectQueryBuilderTest.php b/tests/HS/Builder/SelectQueryBuilderTest.php index 5b86bd6..997e0fc 100644 --- a/tests/HS/Builder/SelectQueryBuilderTest.php +++ b/tests/HS/Builder/SelectQueryBuilderTest.php @@ -6,7 +6,7 @@ namespace HS\Tests\Builder; use HS\Component\Comparison; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\QueryBuilder; use HS\Tests\TestCommon; @@ -56,7 +56,7 @@ public function testSingleSelectExceptionWhere() ->fromDataBase($this->getDatabase()) ->fromTable($this->getTableName()) ->where(Comparison::EQUAL, array('float' => 42)); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return true; } $this->fail('Fail where not throw exception on wrong key position.'); diff --git a/tests/HS/Component/InListTest.php b/tests/HS/Component/InListTest.php index 8cd1a03..2490a3c 100644 --- a/tests/HS/Component/InListTest.php +++ b/tests/HS/Component/InListTest.php @@ -6,9 +6,8 @@ namespace HS\Tests\Component; use HS\Component\InList; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use PHPUnit_Framework_TestCase; -use Symfony\Component\Yaml\Inline; class InListTest extends PHPUnit_Framework_TestCase { @@ -38,7 +37,7 @@ public function testConstructorException() { try { new InList('s', array()); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return true; } diff --git a/tests/HS/Reader/AuthenticateTest.php b/tests/HS/Reader/AuthenticateTest.php index 2d7c70d..bbb901c 100644 --- a/tests/HS/Reader/AuthenticateTest.php +++ b/tests/HS/Reader/AuthenticateTest.php @@ -5,7 +5,7 @@ namespace HS\Tests\Reader; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\Tests\TestCommon; use Stream\Stream; @@ -18,7 +18,7 @@ public function testInt() try { $reader->authenticate(90); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return true; } @@ -31,7 +31,7 @@ public function testEmptyString() try { $reader->authenticate(Stream::STR_EMPTY); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return true; } @@ -44,7 +44,7 @@ public function testValidMissedStringToAuth() $authKey = "text"; try { $reader->authenticate($authKey); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { $this->fail(sprintf("Fail authentication request with valid parameter, sent string:%s.", $authKey)); } diff --git a/tests/HS/Reader/ErrorTest.php b/tests/HS/Reader/ErrorTest.php index 551dc70..7d98a6c 100644 --- a/tests/HS/Reader/ErrorTest.php +++ b/tests/HS/Reader/ErrorTest.php @@ -5,6 +5,7 @@ namespace HS\Tests\Reader; +use HS\Error; use HS\Result\ResultAbstract; use HS\Tests\TestCommon; @@ -47,19 +48,26 @@ public function testErrors() foreach ($this->errorMapList as $cmd => $error) { $data = array(1, 2, $cmd, 'simple data'); - $result = new ResultTest($queryTest, $data); + try { + $result = new ResultTest($queryTest, $data); + } catch (Error $e) { + $actualError = get_class($e); + $this->assertEquals( + 'HS\Errors\\' . $error, + $actualError, + sprintf( + 'Returned wrong error class on error %s. Must be %s, but got %s.', + $cmd, + $error, + $actualError + ) + ); + + continue; + } + + $this->fail('Fail, error won"t catched.'); - $actualError = get_class($result->getError()); - $this->assertEquals( - 'HS\Errors\\' . $error, - $actualError, - sprintf( - 'Returned wrong error class on error %s. Must be %s, but got %s.', - $cmd, - $error, - $actualError - ) - ); } } diff --git a/tests/HS/Reader/OpenIndexTest.php b/tests/HS/Reader/OpenIndexTest.php index 6dcb383..2bfd01a 100644 --- a/tests/HS/Reader/OpenIndexTest.php +++ b/tests/HS/Reader/OpenIndexTest.php @@ -5,7 +5,8 @@ namespace HS\Tests\Reader; -use HS\Exception\WrongParameterException; +use HS\Errors\OpenTableError; +use HS\Exception\InvalidArgumentException; use HS\Tests\TestCommon; use Stream\Stream; @@ -17,7 +18,7 @@ public function testStringIndexId() $reader = $this->getReader(); try { $reader->openIndex("1", $this->getDatabase(), $this->getTableName(), '', array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -29,7 +30,7 @@ public function testZeroIndexId() $reader = $this->getReader(); try { $reader->openIndex(0, $this->getDatabase(), $this->getTableName(), '', array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { $this->fail(sprintf("Fall zero set as indexId: %s", $e->getMessage())); } @@ -40,7 +41,7 @@ public function testNegativeIndexId() $reader = $this->getReader(); try { $reader->openIndex(-99, $this->getDatabase(), $this->getTableName(), '', array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -52,7 +53,7 @@ public function testEmptyDatabase() $reader = $this->getReader(); try { $reader->openIndex(1, Stream::STR_EMPTY, $this->getTableName(), '', array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -64,7 +65,7 @@ public function testNullDatabase() $reader = $this->getReader(); try { $reader->openIndex(1, null, $this->getTableName(), '', array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -76,7 +77,7 @@ public function testEmptyTableName() $reader = $this->getReader(); try { $reader->openIndex(1, $this->getDatabase(), Stream::STR_EMPTY, '', array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -88,7 +89,7 @@ public function testNullTableName() $reader = $this->getReader(); try { $reader->openIndex(1, $this->getDatabase(), null, '', array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -100,7 +101,7 @@ public function testIntTableName() $reader = $this->getReader(); try { $reader->openIndex(1, $this->getDatabase(), 23, '', array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -112,7 +113,7 @@ public function testNullIndexName() $reader = $this->getReader(); try { $reader->openIndex(1, $this->getDatabase(), $this->getTableName(), null, array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { $this->fail(sprintf("Fall null set as indexName: %s", $e->getMessage())); } } @@ -122,7 +123,7 @@ public function testEmptyIndexName() $reader = $this->getReader(); try { $reader->openIndex(1, $this->getDatabase(), $this->getTableName(), Stream::STR_EMPTY, array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { $this->fail("Fall empty string set as indexName."); } @@ -133,7 +134,7 @@ public function testIntIndexName() $reader = $this->getReader(); try { $reader->openIndex(1, $this->getDatabase(), $this->getTableName(), 22, array('text')); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -181,7 +182,7 @@ public function testColumnsContainObject() $reader = $this->getReader(); try { $reader->openIndex(1, $this->getDatabase(), $this->getTableName(), '', array("text", new OpenIndexTest())); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -193,7 +194,7 @@ public function testColumnsContainArray() $reader = $this->getReader(); try { $reader->openIndex(1, $this->getDatabase(), $this->getTableName(), '', array("text", array("test"))); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return; } @@ -206,15 +207,14 @@ public function testMissedDatabaseSuccessfully() $openIndex = null; try { $openIndex = $reader->openIndex(1, "randomdatabase", $this->getTableName(), '', array("text")); - } catch (WrongParameterException $e) { + $reader->getResultList(); + } catch (InvalidArgumentException $e) { $this->fail("Fall with valid parameters."); + } catch (OpenTableError $e) { + return true; } - $reader->getResultList(); - $this->assertFalse( - $openIndex->getResult()->isSuccessfully(), - "Successfully openIndex on not existed database." - ); + $this->fail('Not fall with missed database name.'); } @@ -224,17 +224,18 @@ public function testMissedDatabaseError() $openIndex = null; try { $openIndex = $reader->openIndex(1, "randomdatabase", $this->getTableName(), '', array("text")); - } catch (WrongParameterException $e) { + $reader->getResultList(); + } catch (InvalidArgumentException $e) { $this->fail("Fall with valid parameters."); + } catch (OpenTableError $e) { + $this->assertEquals( + 'HS\Errors\OpenTableError', + get_class($e), + "Error object os not instance of OpenTableError" + ); + return true; } - - $reader->getResultList(); - $this->assertEquals( - 'HS\Errors\OpenTableError', - get_class($openIndex->getResult()->getError()), - "Error object os not instance of OpenTableError" - ); - + $this->fail("Not fall with missed database."); } public function testReopenIndex() @@ -245,7 +246,7 @@ public function testReopenIndex() try { $openIndex = $reader->openIndex(1, $this->getDatabase(), $this->getTableName(), '', array("text")); $openIndexSecond = $reader->openIndex(1, $this->getDatabase(), $this->getTableName(), '', array("text")); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { $this->fail("Fall with valid parameters."); } diff --git a/tests/HS/Reader/ReaderTest.php b/tests/HS/Reader/ReaderTest.php index 0aa6a64..5ff7b39 100644 --- a/tests/HS/Reader/ReaderTest.php +++ b/tests/HS/Reader/ReaderTest.php @@ -7,7 +7,7 @@ use HS\Component\Comparison; use HS\Component\Filter; -use HS\Exception\WrongParameterException; +use HS\Exception\InvalidArgumentException; use HS\Tests\TestCommon; class ReaderTest extends TestCommon @@ -40,7 +40,7 @@ public function testExceptionOnMissedIndexId() 99, array(new Filter(Comparison::EQUAL, 0, 1)) ); - } catch (WrongParameterException $e) { + } catch (InvalidArgumentException $e) { return true; } diff --git a/tests/HS/Reader/SelectQueryTest.php b/tests/HS/Reader/SelectQueryTest.php index aceabaa..a8c825d 100644 --- a/tests/HS/Reader/SelectQueryTest.php +++ b/tests/HS/Reader/SelectQueryTest.php @@ -56,7 +56,7 @@ public function testSelectExistedValue() 'PRIMARY', array('key', 'date', 'float', 'varchar', 'text', 'set', 'null', 'union') ); - $reader->selectByIndex($indexId, Comparison::EQUAL, array(42)); + $selectQuery = $reader->selectByIndex($indexId, Comparison::EQUAL, array(42)); $expectedResult = array( array( diff --git a/tests/HS/Reader/TextQueryTest.php b/tests/HS/Reader/TextQueryTest.php index 0c2c0e0..dbeb99f 100644 --- a/tests/HS/Reader/TextQueryTest.php +++ b/tests/HS/Reader/TextQueryTest.php @@ -13,14 +13,12 @@ class TextQueryTest extends TestCommon public function testTextQueryErrorCommand() { $reader = $this->getReader(); - $textQuery = $reader->text("dfgsdgsd", 'HS\Query\SelectQuery'); - $result = $textQuery->execute()->getResult(); - if (!($result->getError() instanceof CommandError)) { - $this->fail('Wrong instance of error.'); + try { + $textQuery = $reader->text("dfgsdgsd", 'HS\Query\SelectQuery'); + $result = $textQuery->execute()->getResult(); + } catch (CommandError $e) { + return true; } - - $this->assertEquals('cmd', $result->getErrorMessage(), 'Wrong message.'); - $this->assertEquals(false, $result->isSuccessfully(), 'Wrong query type'); - $this->assertEquals($textQuery, $result->getQuery(), 'Wrong query class'); + $this->fail('Wrong instance of error.'); } } \ No newline at end of file