diff --git a/composer.json b/composer.json index 9d227c8..5dc24fb 100644 --- a/composer.json +++ b/composer.json @@ -10,19 +10,12 @@ } ], "require": { - "php": ">=5.5.0" - }, - "require-dev": { - "phpunit/phpunit": "^5.7" + "php": ">=8.2", + "ext-curl": "*" }, "autoload": { "psr-4": { "AmoCRM\\": "src" } - }, - "autoload-dev": { - "classmap": [ - "tests/TestCase.php" - ] } } diff --git a/src/Client.php b/src/Client.php index 022ec85..3697d46 100644 --- a/src/Client.php +++ b/src/Client.php @@ -92,7 +92,7 @@ public function __construct($domain, $login, $apikey, $proxy = null) * @return ModelInterface * @throws ModelException */ - public function __get($name) + public function __get($name): ModelInterface { $classname = '\\AmoCRM\\Models\\' . Format::camelCase($name); diff --git a/src/Exception.php b/src/Exception.php index 363b16e..c90c3f9 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -20,7 +20,7 @@ class Exception extends \Exception /** * @var array Справочник ошибок и ответов amoCRM API */ - protected $errors = [ + protected array $errors = [ '101' => 'Аккаунт не найден', '102' => 'POST-параметры должны передаваться в формате JSON', '103' => 'Параметры не переданы', diff --git a/src/Helpers/Fields.php b/src/Helpers/Fields.php index 2b298c9..3875ff2 100644 --- a/src/Helpers/Fields.php +++ b/src/Helpers/Fields.php @@ -28,7 +28,7 @@ class Fields implements \IteratorAggregate, \ArrayAccess, \Countable * @param mixed $name Название поля * @param mixed $value Значение поля */ - public function __set($name, $value) + public function __set($name, $value): void { $this->offsetSet($name, $value); } @@ -39,7 +39,7 @@ public function __set($name, $value) * @param mixed $name Название поля * @return mixed Значение поля */ - public function __get($name) + public function __get($name): mixed { return $this->offsetGet($name); } @@ -50,7 +50,7 @@ public function __get($name) * @param mixed $key Название поля * @param mixed $value Значение поля */ - public function add($key, $value = null) + public function add($key, $value = null): void { $this->offsetSet($key, $value); } @@ -61,7 +61,7 @@ public function add($key, $value = null) * @param mixed $key Название поля * @return mixed Значение поля */ - public function get($key) + public function get($key): mixed { return $this->offsetGet($key); } @@ -73,7 +73,7 @@ public function get($key) * @param mixed $offset Смещение (ключ) для проверки * @return boolean Возвращает true или false */ - public function offsetExists($offset) + public function offsetExists($offset): bool { return isset($this->fields[$offset]); } @@ -85,7 +85,7 @@ public function offsetExists($offset) * @param mixed $offset Смещение (ключ) для возврата * @return mixed Значение смещения (ключа) */ - public function offsetGet($offset) + public function offsetGet($offset): mixed { if (isset($this->fields[$offset])) { return $this->fields[$offset]; @@ -101,7 +101,7 @@ public function offsetGet($offset) * @param mixed $offset Смещение (ключ), которому будет присваиваться значение * @param mixed $value Значение для присвоения */ - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { $this->fields[$offset] = $value; } @@ -112,7 +112,7 @@ public function offsetSet($offset, $value) * @link http://php.net/manual/en/arrayaccess.offsetunset.php * @param mixed $offset Смещение для удаления */ - public function offsetUnset($offset) + public function offsetUnset($offset): void { if (isset($this->fields[$offset])) { unset($this->fields[$offset]); @@ -125,7 +125,7 @@ public function offsetUnset($offset) * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return \Traversable Экземпляр объекта, использующего Iterator или Traversable */ - public function getIterator() + public function getIterator(): \ArrayIterator { return new \ArrayIterator($this->fields); } @@ -136,7 +136,7 @@ public function getIterator() * @link http://php.net/manual/en/countable.count.php * @return int Количество элементов объекта */ - public function count() + public function count(): int { return count($this->fields); } diff --git a/src/Models/AbstractModel.php b/src/Models/AbstractModel.php index 75b1065..e625c31 100644 --- a/src/Models/AbstractModel.php +++ b/src/Models/AbstractModel.php @@ -30,7 +30,7 @@ abstract class AbstractModel extends Request implements ArrayAccess, ModelInterf /** * @var array Список значений полей для модели */ - protected $values = []; + protected array $values = []; /** * Возвращает называние Модели @@ -49,7 +49,7 @@ public function __toString() * @param mixed $offset Название поля для проверки * @return boolean Возвращает true или false */ - public function offsetExists($offset) + public function offsetExists($offset): bool { return isset($this->values[$offset]); } @@ -61,7 +61,7 @@ public function offsetExists($offset) * @param mixed $offset Название поля для возврата * @return mixed Значение поля */ - public function offsetGet($offset) + public function offsetGet($offset): mixed { if (isset($this->values[$offset])) { return $this->values[$offset]; @@ -79,12 +79,12 @@ public function offsetGet($offset) * @param mixed $offset Название поля, которому будет присваиваться значение * @param mixed $value Значение для присвоения */ - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { $setter = 'set' . Format::camelCase($offset); if (method_exists($this, $setter)) { - return $this->$setter($value); + $this->$setter($value); } elseif (in_array($offset, $this->fields)) { $this->values[$offset] = $value; } @@ -96,7 +96,7 @@ public function offsetSet($offset, $value) * @link http://php.net/manual/en/arrayaccess.offsetunset.php * @param mixed $offset Название поля для удаления */ - public function offsetUnset($offset) + public function offsetUnset($offset): void { if (isset($this->values[$offset])) { unset($this->values[$offset]); @@ -108,7 +108,7 @@ public function offsetUnset($offset) * * @return array Список значений полей модели */ - public function getValues() + public function getValues(): array { return $this->values; } @@ -192,7 +192,7 @@ public function addCustomMultiField($id, $values) */ protected function checkId($id) { - if (intval($id) != $id || $id < 1) { + if (filter_var($id, FILTER_VALIDATE_INT) === false || $id < 1) { throw new Exception('Id must be integer and positive'); } diff --git a/src/Models/Company.php b/src/Models/Company.php index 2d86e62..5ca57a3 100644 --- a/src/Models/Company.php +++ b/src/Models/Company.php @@ -110,17 +110,43 @@ public function apiUpdate($id, $modified = 'now') { $this->checkId($id); + $company = $this->getValues(); + $company['id'] = $id; + $company['last_modified'] = strtotime($modified); + + return $this->apiUpdateList([$company]); + } + + /** + * Пакетное обновление компаний + * + * @link https://web.archive.org/web/20140903201932/https://developers.amocrm.ru/rest_api/company_set.php + * @param \AmoCRM\Models\Company[]|array[] $companies Уникальный идентификатор компании + * @param string $modified Дата последнего изменения данной сущности + * @return bool Флаг успешности выполнения запроса + * @throws \AmoCRM\Exception + */ + public function apiUpdateList($companies = [], $modified = 'now') + { $parameters = [ 'contacts' => [ 'update' => [], ], ]; - $company = $this->getValues(); - $company['id'] = $id; - $company['last_modified'] = strtotime($modified); - - $parameters['contacts']['update'][] = $company; + /** @var \AmoCRM\Models\Company|array $company */ + foreach ($companies as $company) { + if ($company instanceof Company) { + $company = $company->getValues(); + } + $company['id'] = isset($company['id']) ? $company['id'] : null; + $company['last_modified'] = isset($company['last_modified']) + ? $company['last_modified'] + : strtotime($modified); + + $this->checkId($company['id']); + $parameters['contacts']['update'][] = $company; + } $response = $this->postRequest('/private/api/v2/json/company/set', $parameters); diff --git a/src/Models/Contact.php b/src/Models/Contact.php index 7886fbb..988bbc2 100644 --- a/src/Models/Contact.php +++ b/src/Models/Contact.php @@ -104,7 +104,7 @@ public function apiAdd($contacts = []) * * Метод позволяет обновлять данные по уже существующим контактам * - * @link https://developers.amocrm.ru/rest_api/contacts_set.php + * @link https://web.archive.org/web/20140903111912/https://developers.amocrm.ru/rest_api/contacts_set.php * @param int $id Уникальный идентификатор контакта * @param string $modified Дата последнего изменения данной сущности * @return bool Флаг успешности выполнения запроса @@ -114,23 +114,51 @@ public function apiUpdate($id, $modified = 'now') { $this->checkId($id); + $contact = $this->getValues(); + $contact['id'] = $id; + $contact['last_modified'] = strtotime($modified); + + return $this->apiUpdateList([$contact]); + } + + /** + * Пакетное обновление контактов + * + * @link https://web.archive.org/web/20140903111912/https://developers.amocrm.ru/rest_api/contacts_set.php + * @param \AmoCRM\Models\Contact[]|array[] $contacts массив контактов для обновления + * @param string $modified + * @return bool + * @throws \AmoCRM\Exception + * @throws \AmoCRM\NetworkException + */ + public function apiUpdateList($contacts = [], $modified = 'now') + { $parameters = [ 'contacts' => [ 'update' => [], ], ]; - $contact = $this->getValues(); - $contact['id'] = $id; - $contact['last_modified'] = strtotime($modified); - - $parameters['contacts']['update'][] = $contact; + /** @var \AmoCRM\Models\Contact|array $contact */ + foreach ($contacts as $contact) { + if ($contact instanceof Contact) { + $contact = $contact->getValues(); + } + $contact['id'] = isset($contact['id']) ? $contact['id'] : null; + $contact['last_modified'] = isset($contact['last_modified']) + ? $contact['last_modified'] + : strtotime($modified); + + $this->checkId($contact['id']); + $parameters['contacts']['update'][] = $contact; + } $response = $this->postRequest('/private/api/v2/json/contacts/set', $parameters); return empty($response['contacts']['update']['errors']); } + /** * Связи между сделками и контактами * diff --git a/src/Models/Lead.php b/src/Models/Lead.php index 1aa9505..b928282 100644 --- a/src/Models/Lead.php +++ b/src/Models/Lead.php @@ -106,7 +106,7 @@ public function apiAdd($leads = []) * * Метод позволяет обновлять данные по уже существующим сделкам * - * @link https://developers.amocrm.ru/rest_api/leads_set.php + * @link https://web.archive.org/web/20140903201937/https://developers.amocrm.ru/rest_api/leads_set.php * @param int $id Уникальный идентификатор сделки * @param string $modified Дата последнего изменения данной сущности * @return bool Флаг успешности выполнения запроса @@ -116,17 +116,44 @@ public function apiUpdate($id, $modified = 'now') { $this->checkId($id); + $lead = $this->getValues(); + $lead['id'] = $id; + $lead['last_modified'] = strtotime($modified); + + return $this->apiUpdateList([$lead]); + } + + /** + * Пакетное обновление сделок + * + * @link https://web.archive.org/web/20140903201937/https://developers.amocrm.ru/rest_api/leads_set.php + * @param \AmoCRM\Models\Lead[]|array[] $leads массив сделок для обновления + * @param string $modified + * @return bool + * @throws \AmoCRM\Exception + * @throws \AmoCRM\NetworkException + */ + public function apiUpdateList($leads = [], $modified = 'now') + { $parameters = [ 'leads' => [ 'update' => [], ], ]; - $lead = $this->getValues(); - $lead['id'] = $id; - $lead['last_modified'] = strtotime($modified); - - $parameters['leads']['update'][] = $lead; + /** @var \AmoCRM\Models\Lead|array $lead */ + foreach ($leads as $lead) { + if ($lead instanceof Lead) { + $lead = $lead->getValues(); + } + $lead['id'] = isset($lead['id']) ? $lead['id'] : null; + $lead['last_modified'] = isset($lead['last_modified']) + ? $lead['last_modified'] + : strtotime($modified); + + $this->checkId($lead['id']); + $parameters['leads']['update'][] = $lead; + } $response = $this->postRequest('/private/api/v2/json/leads/set', $parameters); diff --git a/src/Models/Note.php b/src/Models/Note.php index bfa69b0..5565794 100644 --- a/src/Models/Note.php +++ b/src/Models/Note.php @@ -155,7 +155,7 @@ public function apiAdd($notes = []) * * Метод позволяет обновлять данные по уже существующим примечаниям * - * @link https://developers.amocrm.ru/rest_api/notes_set.php + * @link https://web.archive.org/web/20140903171813/https://developers.amocrm.ru/rest_api/notes_set.php * @param int $id Уникальный идентификатор примечания * @param string $modified Дата последнего изменения данной сущности * @return bool Флаг успешности выполнения запроса @@ -165,17 +165,45 @@ public function apiUpdate($id, $modified = 'now') { $this->checkId($id); + $note = $this->getValues(); + $note['id'] = $id; + $note['last_modified'] = strtotime($modified); + + return $this->apiUpdateList([$note]); + } + + /** + * Обновление примечания + * + * Метод позволяет обновлять данные по уже существующим примечаниям + * + * @link https://web.archive.org/web/20140903171813/https://developers.amocrm.ru/rest_api/notes_set.php + * @param \AmoCRM\Models\Note[]|array[] $notes массив примечаний дл обновления + * @param string $modified Дата последнего изменения + * @return bool Флаг успешности выполнения запроса + * @throws \AmoCRM\Exception + */ + public function apiUpdateList($notes = [], $modified = 'now') + { $parameters = [ 'notes' => [ 'update' => [], ], ]; - $lead = $this->getValues(); - $lead['id'] = $id; - $lead['last_modified'] = strtotime($modified); - - $parameters['notes']['update'][] = $lead; + /** @var \AmoCRM\Models\Note|array $note */ + foreach ($notes as $note) { + if ($note instanceof Note) { + $note = $note->getValues(); + } + $note['id'] = isset($note['id']) ? $note['id'] : null; + $note['last_modified'] = isset($note['last_modified']) + ? $note['last_modified'] + : strtotime($modified); + + $this->checkId($note['id']); + $parameters['notes']['update'][] = $note; + } $response = $this->postRequest('/private/api/v2/json/notes/set', $parameters); diff --git a/src/Models/Task.php b/src/Models/Task.php index 00b2ada..f32c07f 100644 --- a/src/Models/Task.php +++ b/src/Models/Task.php @@ -137,18 +137,45 @@ public function apiUpdate($id, $text, $modified = 'now') { $this->checkId($id); + $task = $this->getValues(); + $task['id'] = $id; + $task['text'] = $text; + $task['last_modified'] = strtotime($modified); + + return $this->apiUpdateList([$task]); + } + + /** + * Пакетное обновление задач + * + * @link https://web.archive.org/web/20140903175023/https://developers.amocrm.ru/rest_api/tasks_set.php + * @param \AmoCRM\Models\Task[]|array[] $tasks массив задач для обновления + * @param string $modified + * @return bool + * @throws \AmoCRM\Exception + * @throws \AmoCRM\NetworkException + */ + public function apiUpdateList($tasks = [], $modified = 'now') + { $parameters = [ 'tasks' => [ 'update' => [], ], ]; - $task = $this->getValues(); - $task['id'] = $id; - $task['text'] = $text; - $task['last_modified'] = strtotime($modified); - - $parameters['tasks']['update'][] = $task; + /** @var \AmoCRM\Models\Task|array $task */ + foreach ($tasks as $task) { + if ($task instanceof Task) { + $task = $task->getValues(); + } + $task['id'] = isset($task['id']) ? $task['id'] : null; + $task['last_modified'] = isset($task['last_modified']) + ? $task['last_modified'] + : strtotime($modified); + + $this->checkId($task['id']); + $parameters['tasks']['update'][] = $task; + } $response = $this->postRequest('/private/api/v2/json/tasks/set', $parameters); diff --git a/src/Request/CurlHandle.php b/src/Request/CurlHandle.php index d44165d..5ddaae2 100644 --- a/src/Request/CurlHandle.php +++ b/src/Request/CurlHandle.php @@ -22,7 +22,7 @@ class CurlHandle /** * @var resource Повторно используемый обработчик cURL */ - private $handle; + private ?\CurlHandle $handle = null; /** * Закрывает обработчик cURL @@ -49,6 +49,7 @@ public function open() if (!function_exists('curl_init')) { throw new NetworkException('The cURL PHP extension was not loaded.'); } + $this->handle = curl_init(); return $this->handle; diff --git a/src/Request/Request.php b/src/Request/Request.php index 5f5ed42..8f7b0be 100644 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -176,15 +176,15 @@ protected function prepareHeaders($modified = null) protected function prepareEndpoint($url) { if ($this->v1 === false) { - $query = http_build_query(array_merge($this->parameters->getGet(), [ + $query = http_build_query(data: array_merge($this->parameters->getGet(), [ 'USER_LOGIN' => $this->parameters->getAuth('login'), 'USER_HASH' => $this->parameters->getAuth('apikey'), - ]), null, '&'); + ]), arg_separator: '&'); } else { - $query = http_build_query(array_merge($this->parameters->getGet(), [ + $query = http_build_query(data: array_merge($this->parameters->getGet(), [ 'login' => $this->parameters->getAuth('login'), 'api_key' => $this->parameters->getAuth('apikey'), - ]), null, '&'); + ]), arg_separator: '&'); } return sprintf('https://%s%s?%s', $this->parameters->getAuth('domain'), $url, $query); @@ -219,7 +219,7 @@ protected function request($url, $modified = null) if ($this->parameters->hasPost()) { $fields = json_encode([ 'request' => $this->parameters->getPost(), - ]); + ], JSON_THROW_ON_ERROR); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); $this->printDebug('post params', $fields); diff --git a/tests/ClientTest.php b/tests/ClientTest.php deleted file mode 100644 index 5b382be..0000000 --- a/tests/ClientTest.php +++ /dev/null @@ -1,85 +0,0 @@ -amo = new \AmoCRM\Client('example', 'login', 'hash'); - } - - public function testSubDomain() - { - $amo = new \AmoCRM\Client('test', 'login', 'hash'); - - $this->assertEquals('test.amocrm.ru', $amo->parameters->getAuth('domain')); - $this->assertCount(3, $amo->parameters->getAuth()); - } - - public function testFullDomain() - { - $amo = new \AmoCRM\Client('test-ru.amocrm.ru', 'login', 'hash'); - - $this->assertEquals('test-ru.amocrm.ru', $amo->parameters->getAuth('domain')); - $this->assertCount(3, $amo->parameters->getAuth()); - } - - public function testFullDomainCom() - { - $amo = new \AmoCRM\Client('test-com.amocrm.com', 'login', 'hash'); - - $this->assertEquals('test-com.amocrm.com', $amo->parameters->getAuth('domain')); - $this->assertCount(3, $amo->parameters->getAuth()); - } - - /** - * @dataProvider modelsProvider - */ - public function testGetModel($name, $expected) - { - $model = $this->amo->{$name}; - - $this->assertInstanceOf($expected, $model); - $this->assertInstanceOf('\AmoCRM\Models\ModelInterface', $model); - $this->assertSame($expected, (string)$model); - } - - /** - * @expectedException \AmoCRM\ModelException - */ - public function testIncorrectModel() - { - $this->amo->foobar; - } - - public function testHelperFields() - { - $this->assertInstanceOf('\AmoCRM\Helpers\Fields', $this->amo->fields); - } - - public function modelsProvider() - { - return [ - // model name, expected - ['account', 'AmoCRM\Models\Account'], - ['call', 'AmoCRM\Models\Call'], - ['catalog', 'AmoCRM\Models\Catalog'], - ['catalog_element', 'AmoCRM\Models\CatalogElement'], - ['company', 'AmoCRM\Models\Company'], - ['contact', 'AmoCRM\Models\Contact'], - ['customer', 'AmoCRM\Models\Customer'], - ['customers_periods', 'AmoCRM\Models\CustomersPeriods'], - ['custom_field', 'AmoCRM\Models\CustomField'], - ['lead', 'AmoCRM\Models\Lead'], - ['links', 'AmoCRM\Models\Links'], - ['note', 'AmoCRM\Models\Note'], - ['pipelines', 'AmoCRM\Models\Pipelines'], - ['task', 'AmoCRM\Models\Task'], - ['transaction', 'AmoCRM\Models\Transaction'], - ['unsorted', 'AmoCRM\Models\Unsorted'], - ['webhooks', 'AmoCRM\Models\Webhooks'], - ['widgets', 'AmoCRM\Models\Widgets'], - ]; - } -} diff --git a/tests/Helpers/B2BFamilyExceptionTest.php b/tests/Helpers/B2BFamilyExceptionTest.php deleted file mode 100644 index ae21c2e..0000000 --- a/tests/Helpers/B2BFamilyExceptionTest.php +++ /dev/null @@ -1,14 +0,0 @@ -b2b = new B2BFamilyMock( - $amo, - 'b2b_appkey', - 'b2b_secret', - 'b2b_email', - 'b2b_password' - ); - } - - public function testLogin() - { - $response = $this->b2b->login(); - - $this->assertEquals($response['appkey'], 'b2b_appkey'); - $this->assertEquals($response['email'], 'b2b_email'); - $this->assertEquals($response['password'], 'b2b_password'); - $this->assertEquals($response['hash'], 'fde29cb7e620a15fbf23b75c61725a7e'); - } - - public function testSubscribe() - { - $response = $this->b2b->subscribe(); - - $this->assertArrayHasKey('apikey', $response); - $this->assertEquals($response['path'], 'http://b2bf.cloudapp.net/post/'); - } - - public function testUnsubscribe() - { - $response = $this->b2b->unsubscribe(100); - - $this->assertArrayHasKey('apikey', $response); - $this->assertEquals($response['id'], 100); - } - - public function testMail() - { - $response = $this->b2b->mail(100, ['foobar' => ['foo' => 'bar']]); - - $this->assertArrayHasKey('apikey', $response); - $this->assertArrayHasKey('custom_data', $response); - $this->assertArrayHasKey('notification_settings', $response); - $this->assertEquals($response['foobar']['foo'], 'bar'); - $this->assertEquals($response['custom_data']['userDomainAmo'], 'example.com'); - $this->assertEquals($response['custom_data']['userLoginAmo'], 'login'); - $this->assertEquals($response['custom_data']['userHashAmo'], 'hash'); - $this->assertEquals($response['custom_data']['userTypeAmo'], 2); - $this->assertEquals($response['custom_data']['userIdAmo'], 100); - $this->assertFalse($response['notification_settings']['sms_enable']); - $this->assertFalse($response['notification_settings']['email_enable']); - $this->assertTrue($response['notification_settings']['webhook_enable']); - } -} diff --git a/tests/Helpers/FieldsTest.php b/tests/Helpers/FieldsTest.php deleted file mode 100644 index e32d155..0000000 --- a/tests/Helpers/FieldsTest.php +++ /dev/null @@ -1,67 +0,0 @@ -fields = new \AmoCRM\Helpers\Fields(); - - $this->fields->add('key1', 'value1'); - $this->fields->add('key2', 'value2'); - } - - public function testAdd() - { - $this->fields['key3'] = 'value3'; - $this->fields->key4 = 'value4'; - $this->fields->add('key5', 'value5'); - $this->fields->offsetSet('key6', 'value6'); - - $this->assertEquals(6, count($this->fields)); - } - - public function testGet() - { - $this->assertEquals('value1', $this->fields['key1']); - $this->assertEquals('value1', $this->fields->key1); - $this->assertEquals('value1', $this->fields->get('key1')); - $this->assertEquals('value1', $this->fields->offsetGet('key1')); - $this->assertEquals(null, $this->fields->offsetGet('not exist')); - } - - public function testExists() - { - $this->assertTrue(isset($this->fields['key1'])); - $this->assertTrue($this->fields->offsetExists('key1')); - } - - public function testUnset() - { - unset($this->fields['key1']); - $this->assertFalse(isset($this->fields['key1'])); - - $this->fields->offsetUnset('key2'); - $this->assertFalse(isset($this->fields['key2'])); - } - - public function testIterator() - { - $iterator = $this->fields->getIterator(); - - $this->assertEquals('value1', $iterator->current()); - - $iterator->next(); - $this->assertEquals('value2', $iterator->current()); - } - - public function testCount() - { - $this->assertEquals(2, count($this->fields)); - $this->assertEquals(2, $this->fields->count()); - } -} diff --git a/tests/Models/AbstractModelTest.php b/tests/Models/AbstractModelTest.php deleted file mode 100644 index 19ada93..0000000 --- a/tests/Models/AbstractModelTest.php +++ /dev/null @@ -1,54 +0,0 @@ -model = $this->getMockForAbstractClass('\AmoCRM\Models\AbstractModel', [$paramsBag]); - $this->setProtectedProperty($this->model, 'fields', [ - 'foo', - 'bar', - ]); - } - - public function testOffsetExists() - { - $this->model['foo'] = 'foobar'; - $this->assertTrue(isset($this->model['foo'])); - } - - public function testOffsetGet() - { - $this->model['foo'] = 'foobar'; - $this->assertEquals('foobar', $this->model['foo']); - $this->assertEquals(null, $this->model['bar']); - } - - public function testOffsetSet() - { - $this->model['foo'] = 'foobar'; - $this->assertEquals('foobar', $this->model['foo']); - } - - public function testOffsetUncet() - { - $this->model['foo'] = 'foobar'; - $this->assertEquals('foobar', $this->model['foo']); - unset($this->model['foo']); - $this->assertEquals(null, $this->model['foo']); - } - - public function testGetValues() - { - $this->assertCount(0, $this->model->getValues()); - $this->model['foo'] = 'foobar'; - $this->assertCount(1, $this->model->getValues()); - } -} diff --git a/tests/Models/AccountTest.php b/tests/Models/AccountTest.php deleted file mode 100644 index 353c4ef..0000000 --- a/tests/Models/AccountTest.php +++ /dev/null @@ -1,88 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['account' => [ - 'users' => [ - [ - 'id' => 1, - 'login' => 'mail@example.com', - ], - [ - 'id' => 2, - 'login' => 'foo@bar.com', - ] - ] - ]]; - } -} - -class AccountTest extends TestCase -{ - /** - * @var null|AccountMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $paramsBag->addAuth('login', 'mail@example.com'); - $this->model = new AccountMock($paramsBag); - } - - public function testApiCurrent() - { - $parameters = [ - 'free_users' => 'Y' - ]; - - $result = $this->model->apiCurrent(false, $parameters); - - $this->assertNotEmpty($result); - $this->assertEquals('/private/api/v2/json/accounts/current', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testGetUserByLogin() - { - $actual = $this->model->getUserByLogin(); - $this->assertNotEmpty($actual); - $this->assertArrayHasKey('id', $actual); - $this->assertEquals('mail@example.com', $actual['login']); - - $actual = $this->model->getUserByLogin('foo@bar.com'); - $this->assertNotEmpty($actual); - $this->assertArrayHasKey('id', $actual); - $this->assertEquals('foo@bar.com', $actual['login']); - } - - public function testGetShorted() - { - $keys = ['id' => [], 'name' => [], 'login' => [], 'type_id' => [], 'enums' => []]; - - $expected = [ - 'users' => $keys, - 'leads_statuses' => $keys, - 'note_types' => $keys, - 'task_types' => $keys, - 'custom_fields' => [$keys], - 'pipelines' => $keys, - ]; - - $actual = $this->invokeMethod($this->model, 'getShorted', [$expected]); - - $this->assertEquals($expected, $actual); - } -} diff --git a/tests/Models/CallTest.php b/tests/Models/CallTest.php deleted file mode 100644 index 0a8d1bd..0000000 --- a/tests/Models/CallTest.php +++ /dev/null @@ -1,116 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['calls' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'calls' => [ - 'add' => [ - 'success' => [ - "b7095fb33b368c7103626d3943d9e61c14697", - "b8b49f89f4c9b6fde609ad741e640dd946234", - ], - 'errors' => [], - ] - ] - ]; - } -} - -class CallTest extends TestCase -{ - /** - * @var null|CallMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new CallMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiAdd() - { - $expected = [ - 'add' => [ - [ - 'account_id' => 1111111, - 'uuid' => '947669bc-ec58-450e-83e8-828a3e6fc354', - ] - ] - ]; - - $this->model['account_id'] = 1111111; - $this->model['uuid'] = '947669bc-ec58-450e-83e8-828a3e6fc354'; - - $this->assertEquals( - 'b7095fb33b368c7103626d3943d9e61c14697', - $this->model->apiAdd('code', 'key') - ); - $this->assertEquals('/api/calls/add/', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'add' => [ - [ - 'account_id' => 1111111, - 'uuid' => '947669bc-ec58-450e-83e8-828a3e6fc354', - ], - [ - 'account_id' => 1111111, - 'uuid' => '947669bc-ec58-450e-83e8-828a3e6fc354', - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd('code', 'key', [$this->model, $this->model])); - $this->assertEquals('/api/calls/add/', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['account_id', 100, 100], - ['uuid', '947669bc-ec58-450e-83e8-828a3e6fc354', '947669bc-ec58-450e-83e8-828a3e6fc354'], - ['caller', '88001000000', '88001000000'], - ['to', '88002000000', '88002000000'], - ['date', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['type', 'inbound', 'inbound'], - ['billsec', 10, 10], - ['link', 'http://example.com/audio.mp3', 'http://example.com/audio.mp3'], - ]; - } -} diff --git a/tests/Models/CatalogElementTest.php b/tests/Models/CatalogElementTest.php deleted file mode 100644 index e0c4ae4..0000000 --- a/tests/Models/CatalogElementTest.php +++ /dev/null @@ -1,158 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['catalog_elements' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'catalog_elements' => [ - 'add' => [ - 'catalog_elements' => [ - ['id' => 100], - ['id' => 100], - ], - 'errors' => [], - ], - 'update' => [ - 'catalog_elements' => [ - ['id' => 100] - ], - 'errors' => [], - ], - 'delete' => [ - 'catalog_elements' => [ - ['id' => 100] - ], - 'errors' => [], - ] - ] - ]; - } -} - -class CatalogElementTest extends TestCase -{ - /** - * @var null|CatalogElementMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new CatalogElementMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $result = $this->model->apiList(); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/catalog_elements/list', $this->model->mockUrl); - $this->assertEquals([], $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $expected = [ - 'catalog_elements' => [ - 'add' => [ - [ - 'catalog_id' => 100, - 'name' => 'Product', - ] - ] - ] - ]; - - $this->model['catalog_id'] = 100; - $this->model['name'] = 'Product'; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/catalog_elements/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'catalog_elements' => [ - 'add' => [ - [ - 'catalog_id' => 100, - 'name' => 'Product', - ], - [ - 'catalog_id' => 100, - 'name' => 'Product', - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/catalog_elements/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUpdate() - { - $this->model['name'] = 'Product'; - - $this->assertTrue($this->model->apiUpdate(1)); - $this->assertEquals('/private/api/v2/json/catalog_elements/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['catalog_elements']['update'][0]['id']); - $this->assertEquals('Product', $this->model->mockParameters['catalog_elements']['update'][0]['name']); - } - - public function testApiDelete() - { - $this->assertTrue($this->model->apiDelete(1)); - $this->assertEquals('/private/api/v2/json/catalog_elements/set', $this->model->mockUrl); - $this->assertEquals([1], $this->model->mockParameters['catalog_elements']['delete']); - } - - public function testApiDeleteBatch() - { - $this->assertTrue($this->model->apiDeleteBatch([1,2,3])); - $this->assertEquals('/private/api/v2/json/catalog_elements/set', $this->model->mockUrl); - $this->assertEquals([1,2,3], $this->model->mockParameters['catalog_elements']['delete']); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['catalog_id', 100, 100], - ['name', 'Products', 'Products'], - ['request_id', 200, 200], - ]; - } -} diff --git a/tests/Models/CatalogTest.php b/tests/Models/CatalogTest.php deleted file mode 100644 index fb5cab9..0000000 --- a/tests/Models/CatalogTest.php +++ /dev/null @@ -1,154 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['contacts' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'catalogs' => [ - 'add' => [ - 'catalogs' => [ - ['id' => 100], - ['id' => 100], - ], - 'errors' => [], - ], - 'update' => [ - 'catalogs' => [ - ['id' => 100] - ], - 'errors' => [], - ], - 'delete' => [ - 'catalogs' => [ - ['id' => 100] - ], - 'errors' => [], - ] - ] - ]; - } -} - -class CatalogTest extends TestCase -{ - /** - * @var null|CatalogMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new CatalogMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $result = $this->model->apiList(); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/catalogs/list', $this->model->mockUrl); - $this->assertEquals([], $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $result = $this->model->apiList(100); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/catalogs/list', $this->model->mockUrl); - $this->assertEquals(['id' => 100], $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $expected = [ - 'catalogs' => [ - 'add' => [ - [ - 'name' => 'Products', - ] - ] - ] - ]; - - $this->model['name'] = 'Products'; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/catalogs/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'catalogs' => [ - 'add' => [ - [ - 'name' => 'Products', - ], - [ - 'name' => 'Products', - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/catalogs/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUpdate() - { - $this->model['name'] = 'Products'; - $this->model['company_name'] = 'ООО Тестовая компания'; - - $this->assertTrue($this->model->apiUpdate(1)); - $this->assertEquals('/private/api/v2/json/catalogs/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['catalogs']['update'][0]['id']); - $this->assertEquals('Products', $this->model->mockParameters['catalogs']['update'][0]['name']); - } - - public function testApiDelete() - { - $this->assertTrue($this->model->apiDelete(1)); - $this->assertEquals('/private/api/v2/json/catalogs/set', $this->model->mockUrl); - $this->assertEquals([1], $this->model->mockParameters['catalogs']['delete']); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['name', 'Products', 'Products'], - ['request_id', 100, 100], - ]; - } -} diff --git a/tests/Models/CompanyTest.php b/tests/Models/CompanyTest.php deleted file mode 100644 index 805af22..0000000 --- a/tests/Models/CompanyTest.php +++ /dev/null @@ -1,180 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['contacts' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'contacts' => [ - 'add' => [ - ['id' => 100], - ['id' => 200] - ], - 'update' => [ - ['id' => 100], - ['id' => 200] - ] - ] - ]; - } -} - -class CompanyTest extends TestCase -{ - /** - * @var null|CompanyMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new CompanyMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testCustomFields() - { - $this->model->addCustomField(100, 'Custom text'); - $this->model->addCustomField(200, 'test@mail.com', 'WORK'); - $this->model->addCustomField(300, [ - ['415.874.3275', 'MOB'], - ['415.374.3278', 'OTHER'], - ['415.374.3279', 'FAX'], - ]); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][0]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][0]); - $this->assertArrayHasKey('value', $this->model['custom_fields'][0]['values'][0]); - $this->assertEquals('Custom text', $this->model['custom_fields'][0]['values'][0]['value']); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][1]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][1]); - $this->assertArrayHasKey('value', $this->model['custom_fields'][1]['values'][0]); - $this->assertEquals('test@mail.com', $this->model['custom_fields'][1]['values'][0]['value']); - $this->assertEquals('WORK', $this->model['custom_fields'][1]['values'][0]['enum']); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][2]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][2]); - $this->assertCount(3, $this->model['custom_fields'][2]['values']); - } - - public function testApiList() - { - $parameters = [ - 'query' => 'test', - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/company/list', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $expected = [ - 'contacts' => [ - 'add' => [ - [ - 'name' => 'ООО Тестовая компания', - ] - ] - ] - ]; - - $this->model['name'] = 'ООО Тестовая компания'; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/company/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'contacts' => [ - 'add' => [ - [ - 'name' => 'ООО Тестовая компания', - ], - [ - 'name' => 'ООО Тестовая компания', - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/company/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUpdate() - { - $this->model['name'] = 'ООО Тестовая компания'; - - $this->assertTrue($this->model->apiUpdate(1)); - $this->assertEquals('/private/api/v2/json/company/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['contacts']['update'][0]['id']); - $this->assertEquals('ООО Тестовая компания', $this->model->mockParameters['contacts']['update'][0]['name']); - - $this->assertTrue($this->model->apiUpdate(1, 'now')); - $this->assertEquals('/private/api/v2/json/company/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['contacts']['update'][0]['id']); - $this->assertEquals('ООО Тестовая компания', $this->model->mockParameters['contacts']['update'][0]['name']); - } - - /** - * @expectedException \AmoCRM\Exception - */ - public function testApiUpdateBad() - { - $this->model->apiUpdate('foo', 'bar'); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['name', 'Компания', 'Компания'], - ['request_id', 100, 100], - ['date_create', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['last_modified', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['responsible_user_id', 100, 100], - ['created_user_id', 100, 100], - ['linked_leads_id', 100, [100]], - ['linked_leads_id', [100, 200], [100, 200]], - ['tags', 'Tag', 'Tag'], - ['tags', ['Tag 1', 'Tag 2'], 'Tag 1,Tag 2'], - ['modified_user_id', 100, 100], - ]; - } -} diff --git a/tests/Models/ContactTest.php b/tests/Models/ContactTest.php deleted file mode 100644 index a64d291..0000000 --- a/tests/Models/ContactTest.php +++ /dev/null @@ -1,219 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['contacts' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'contacts' => [ - 'add' => [ - ['id' => 100], - ['id' => 200] - ], - 'update' => [ - ['id' => 100], - ['id' => 200] - ] - ] - ]; - } -} - -class ContactTest extends TestCase -{ - /** - * @var null|ContactMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new ContactMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testCustomFields() - { - $this->model->addCustomField(100, 'Custom text'); - $this->model->addCustomField(200, 'test@mail.com', 'WORK'); - $this->model->addCustomField(300, [ - ['415.874.3275', 'MOB'], - ['415.374.3278', 'OTHER'], - ['415.374.3279', 'FAX'], - ]); - $this->model->addCustomMultiField(400, 100); - $this->model->addCustomMultiField(500, [200, 300]); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][0]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][0]); - $this->assertArrayHasKey('value', $this->model['custom_fields'][0]['values'][0]); - $this->assertEquals('Custom text', $this->model['custom_fields'][0]['values'][0]['value']); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][1]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][1]); - $this->assertArrayHasKey('value', $this->model['custom_fields'][1]['values'][0]); - $this->assertEquals('test@mail.com', $this->model['custom_fields'][1]['values'][0]['value']); - $this->assertEquals('WORK', $this->model['custom_fields'][1]['values'][0]['enum']); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][2]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][2]); - $this->assertCount(3, $this->model['custom_fields'][2]['values']); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][3]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][3]); - $this->assertCount(1, $this->model['custom_fields'][3]['values']); - $this->assertContains(100, $this->model['custom_fields'][3]['values']); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][4]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][4]); - $this->assertCount(2, $this->model['custom_fields'][4]['values']); - $this->assertContains(200, $this->model['custom_fields'][4]['values']); - $this->assertContains(300, $this->model['custom_fields'][4]['values']); - } - - public function testSetNotes() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $note = new \AmoCRM\Models\Note($paramsBag); - $note['text'] = 'foobar'; - - $this->model['notes'] = $note; - $this->assertEquals($this->model['notes'], [$note->getValues()]); - - $this->model['notes'] = [$note, $note]; - $this->assertEquals($this->model['notes'], [$note->getValues(), $note->getValues()]); - } - - public function testApiList() - { - $parameters = [ - 'query' => 'test', - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/contacts/list', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $expected = [ - 'contacts' => [ - 'add' => [ - [ - 'name' => 'ФИО', - 'company_name' => 'ООО Тестовая компания', - ] - ] - ] - ]; - - $this->model['name'] = 'ФИО'; - $this->model['company_name'] = 'ООО Тестовая компания'; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/contacts/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'contacts' => [ - 'add' => [ - [ - 'name' => 'ФИО', - 'company_name' => 'ООО Тестовая компания', - ], - [ - 'name' => 'ФИО', - 'company_name' => 'ООО Тестовая компания', - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/contacts/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUpdate() - { - $this->model['name'] = 'ФИО'; - $this->model['company_name'] = 'ООО Тестовая компания'; - - $this->assertTrue($this->model->apiUpdate(1)); - $this->assertEquals('/private/api/v2/json/contacts/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['contacts']['update'][0]['id']); - $this->assertEquals('ФИО', $this->model->mockParameters['contacts']['update'][0]['name']); - - $this->assertTrue($this->model->apiUpdate(1, 'now')); - $this->assertEquals('/private/api/v2/json/contacts/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['contacts']['update'][0]['id']); - $this->assertEquals('ФИО', $this->model->mockParameters['contacts']['update'][0]['name']); - } - - public function testApiLinks() - { - $parameters = [ - 'limit_rows' => 4 - ]; - - $result = $this->model->apiLinks($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/contacts/links', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['name', 'ФИО', 'ФИО'], - ['request_id', 100, 100], - ['date_create', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['last_modified', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['responsible_user_id', 100, 100], - ['created_user_id', 100, 100], - ['linked_leads_id', 100, [100]], - ['linked_leads_id', [100, 200], [100, 200]], - ['company_name', 'Компания', 'Компания'], - ['linked_company_id', 100, 100], - ['tags', 'Tag', 'Tag'], - ['tags', ['Tag 1', 'Tag 2'], 'Tag 1,Tag 2'], - ['modified_user_id', 100, 100], - ]; - } -} diff --git a/tests/Models/CustomFieldTest.php b/tests/Models/CustomFieldTest.php deleted file mode 100644 index 7e575e6..0000000 --- a/tests/Models/CustomFieldTest.php +++ /dev/null @@ -1,136 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'fields' => [ - 'add' => [ - ['id' => 100], - ['id' => 200] - ], - 'delete' => [ - ['id' => 100] - ] - ] - ]; - } -} - -class CustomFieldTest extends TestCase -{ - /** - * @var null|CustomFieldMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new CustomFieldMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertSame($this->model[$field], $expected); - } - - public function testApiAdd() - { - $expected = [ - 'fields' => [ - 'add' => [ - [ - 'name' => 'Tracking ID', - 'type' => 1, - 'element_type' => 3, - 'origin' => '528d0285c1f9180911159a9dc6f759b3_zendesk_widget', - ] - ] - ] - ]; - - $this->model['name'] = 'Tracking ID'; - $this->model['type'] = \AmoCRM\Models\CustomField::TYPE_TEXT; - $this->model['element_type'] = \AmoCRM\Models\CustomField::ENTITY_COMPANY; - $this->model['origin'] = '528d0285c1f9180911159a9dc6f759b3_zendesk_widget'; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/fields/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'fields' => [ - 'add' => [ - [ - 'name' => 'Tracking ID', - 'type' => 1, - 'element_type' => 3, - 'origin' => '528d0285c1f9180911159a9dc6f759b3_zendesk_widget', - ], - [ - 'name' => 'Tracking ID', - 'type' => 1, - 'element_type' => 3, - 'origin' => '528d0285c1f9180911159a9dc6f759b3_zendesk_widget', - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/fields/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiDelete() - { - $expected = [ - 'fields' => [ - 'delete' => [ - [ - 'id' => 100, - 'origin' => '528d0285c1f9180911159a9dc6f759b3_zendesk_widget', - ], - ] - ] - ]; - - $this->assertTrue($this->model->apiDelete(100, '528d0285c1f9180911159a9dc6f759b3_zendesk_widget')); - $this->assertEquals('/private/api/v2/json/fields/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['name', 'Field', 'Field'], - ['request_id', 100, 100], - ['disabled', 1, 1], - ['disabled', true, 1], - ['disabled', 0, 0], - ['disabled', false, 0], - ['type', 1, 1], - ['element_type', 1, 1], - ['origin', 'api', 'api'], - ['enums', [['one'], ['two']], [['one'], ['two']]], - ]; - } -} diff --git a/tests/Models/CustomerTest.php b/tests/Models/CustomerTest.php deleted file mode 100644 index 1b23950..0000000 --- a/tests/Models/CustomerTest.php +++ /dev/null @@ -1,161 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['customers' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'customers' => [ - 'add' => [ - 'customers' => [ - ['id' => 100], - ['id' => 200] - ] - ], - 'update' => [ - 'customers' => [ - ['id' => 100], - ['id' => 200] - ] - ] - ] - ]; - } -} - -class CustomerTest extends TestCase -{ - /** - * @var null|CustomerMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new CustomerMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $parameters = [ - 'limit_rows' => 100, - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/customers/list', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $expected = [ - 'customers' => [ - 'add' => [ - [ - 'name' => 'ФИО', - 'request_id' => 100, - 'main_user_id' => 200, - 'next_price' => 300, - 'periodicity' => 7, - ] - ] - ] - ]; - - $this->model['name'] = 'ФИО'; - $this->model['request_id'] = 100; - $this->model['main_user_id'] = 200; - $this->model['next_price'] = 300; - $this->model['periodicity'] = 7; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/customers/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'customers' => [ - 'add' => [ - [ - 'name' => 'ФИО', - 'request_id' => 100, - 'main_user_id' => 200, - 'next_price' => 300, - 'periodicity' => 7, - ], - [ - 'name' => 'ФИО', - 'request_id' => 100, - 'main_user_id' => 200, - 'next_price' => 300, - 'periodicity' => 7, - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/customers/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUpdate() - { - $this->model['name'] = 'ФИО'; - $this->model['main_user_id'] = 200; - - $this->assertTrue($this->model->apiUpdate(1)); - $this->assertEquals('/private/api/v2/json/customers/set', $this->model->mockUrl); - - $this->assertEquals(1, $this->model->mockParameters['customers']['update'][0]['id']); - $this->assertEquals('ФИО', $this->model->mockParameters['customers']['update'][0]['name']); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['name', 'ФИО', 'ФИО'], - ['main_user_id', 100, 100], - ['created_by', 100, 100], - ['next_price', 100, 100], - ['periodicity', 100, 100], - ['tags', 'Tag', 'Tag'], - ['tags', ['Tag 1', 'Tag 2'], 'Tag 1,Tag 2'], - ['next_date', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['request_id', 100, 100], - ]; - } -} diff --git a/tests/Models/CustomersPeriodsTest.php b/tests/Models/CustomersPeriodsTest.php deleted file mode 100644 index 5769bea..0000000 --- a/tests/Models/CustomersPeriodsTest.php +++ /dev/null @@ -1,130 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['customers_periods' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'customers_periods' => [ - 'set' => [ - ['id' => 100], - ['id' => 200], - ] - ] - ]; - } -} - -class CustomersPeriodsTest extends TestCase -{ - /** - * @var null|CustomersPeriodsMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new CustomersPeriodsMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $result = $this->model->apiList(); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/customers_periods/list', $this->model->mockUrl); - $this->assertEquals([], $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiSet() - { - $expected = [ - 'customers_periods' => [ - 'update' => [ - [ - 'period' => 7, - 'sort' => 1, - 'color' => '#ffdc7f', - ] - ] - ] - ]; - - $this->model['period'] = 7; - $this->model['sort'] = 1; - $this->model['color'] = '#ffdc7f'; - - $this->assertEquals(100, $this->model->apiSet()); - $this->assertEquals('/private/api/v2/json/customers_periods/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'customers_periods' => [ - 'update' => [ - [ - 'period' => 7, - 'sort' => 1, - 'color' => '#ffdc7f', - ], - [ - 'period' => 60, - 'sort' => 3, - 'color' => '#ccc8f9', - ] - ] - ] - ]; - - $period = [ - 'period' => 60, - 'sort' => 3, - 'color' => '#ccc8f9', - ]; - - $this->assertCount(2, $this->model->apiSet([$this->model, $period])); - $this->assertEquals('/private/api/v2/json/customers_periods/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['id', 100, 100], - ['period', 60, 60], - ['sort', 1, 1], - ['color', '#ffdc7f', '#ffdc7f'], - ]; - } -} diff --git a/tests/Models/LeadTest.php b/tests/Models/LeadTest.php deleted file mode 100644 index 0510238..0000000 --- a/tests/Models/LeadTest.php +++ /dev/null @@ -1,189 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['leads' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'leads' => [ - 'add' => [ - ['id' => 100], - ['id' => 200] - ], - 'update' => [ - ['id' => 100], - ['id' => 200] - ] - ] - ]; - } -} - -class LeadTest extends TestCase -{ - /** - * @var null|LeadMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new LeadMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testCustomFields() - { - $this->model->addCustomField(100, 'Custom text'); - $this->model->addCustomField(200, 'test@mail.com', 'WORK'); - $this->model->addCustomField(300, [ - ['415.874.3275', 'MOB'], - ['415.374.3278', 'OTHER'], - ['415.374.3279', 'FAX'], - ]); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][0]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][0]); - $this->assertArrayHasKey('value', $this->model['custom_fields'][0]['values'][0]); - $this->assertEquals('Custom text', $this->model['custom_fields'][0]['values'][0]['value']); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][1]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][1]); - $this->assertArrayHasKey('value', $this->model['custom_fields'][1]['values'][0]); - $this->assertEquals('test@mail.com', $this->model['custom_fields'][1]['values'][0]['value']); - $this->assertEquals('WORK', $this->model['custom_fields'][1]['values'][0]['enum']); - - $this->assertArrayHasKey('id', $this->model['custom_fields'][2]); - $this->assertArrayHasKey('values', $this->model['custom_fields'][2]); - $this->assertCount(3, $this->model['custom_fields'][2]['values']); - } - - public function testSetNotes() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $note = new \AmoCRM\Models\Note($paramsBag); - $note['text'] = 'foobar'; - - $this->model['notes'] = $note; - $this->assertEquals($this->model['notes'], [$note->getValues()]); - - $this->model['notes'] = [$note, $note]; - $this->assertEquals($this->model['notes'], [$note->getValues(), $note->getValues()]); - } - - public function testApiList() - { - $parameters = [ - 'query' => 'test', - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/leads/list', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $expected = [ - 'leads' => [ - 'add' => [ - [ - 'name' => 'Тестовая сделка', - ] - ] - ] - ]; - - $this->model['name'] = 'Тестовая сделка'; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/leads/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'leads' => [ - 'add' => [ - [ - 'name' => 'Тестовая сделка', - ], - [ - 'name' => 'Тестовая сделка', - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/leads/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUpdate() - { - $this->model['name'] = 'Тестовая сделка'; - - $this->assertTrue($this->model->apiUpdate(1)); - $this->assertEquals('/private/api/v2/json/leads/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['leads']['update'][0]['id']); - $this->assertEquals('Тестовая сделка', $this->model->mockParameters['leads']['update'][0]['name']); - - $this->assertTrue($this->model->apiUpdate(1, 'now')); - $this->assertEquals('/private/api/v2/json/leads/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['leads']['update'][0]['id']); - $this->assertEquals('Тестовая сделка', $this->model->mockParameters['leads']['update'][0]['name']); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['name', 'Сделка', 'Сделка'], - ['date_create', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['last_modified', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['status_id', 100, 100], - ['pipeline_id', 100, 100], - ['price', 300000, 300000], - ['responsible_user_id', 100, 100], - ['created_user_id', 100, 100], - ['request_id', 100, 100], - ['linked_company_id', 100, 100], - ['tags', 'Tag', 'Tag'], - ['tags', ['Tag 1', 'Tag 2'], 'Tag 1,Tag 2'], - ['visitor_uid', '12345678-52d2-44c2-9e16-ba0052d9f6d6', '12345678-52d2-44c2-9e16-ba0052d9f6d6'], - ['modified_user_id', 100, 100], - ['loss_reason_id', 100, 100], - ]; - } -} diff --git a/tests/Models/LinksTest.php b/tests/Models/LinksTest.php deleted file mode 100644 index cc7573a..0000000 --- a/tests/Models/LinksTest.php +++ /dev/null @@ -1,192 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['links' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'links' => [ - 'link' => [ - 'links' => [], - 'errors' => [], - ], - 'unlink' => [ - 'links' => [], - 'errors' => [], - ] - ] - ]; - } -} - -class LinksTest extends TestCase -{ - /** - * @var null|LinksMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new LinksMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $parameters = [ - 'from' => 'leads', - 'from_id' => 1125199, - 'to' => 'contacts', - 'to_id' => 3673249 - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/links/list', $this->model->mockUrl); - $this->assertEquals(['links' => [$parameters]], $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiLink() - { - $expected = [ - 'links' => [ - 'link' => [ - [ - 'from' => 'leads', - 'from_id' => 1125199, - 'to' => 'contacts', - 'to_id' => 3673249, - ] - ] - ] - ]; - - $this->model['from'] = 'leads'; - $this->model['from_id'] = 1125199; - $this->model['to'] = 'contacts'; - $this->model['to_id'] = 3673249; - - $this->assertTrue($this->model->apiLink()); - $this->assertEquals('/private/api/v2/json/links/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'links' => [ - 'link' => [ - [ - 'from' => 'leads', - 'from_id' => 1125199, - 'to' => 'contacts', - 'to_id' => 3673249, - ], - [ - 'from' => 'leads', - 'from_id' => 1125199, - 'to' => 'contacts', - 'to_id' => 3673249, - ] - ] - ] - ]; - - $this->assertTrue($this->model->apiLink([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/links/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUnlink() - { - $expected = [ - 'links' => [ - 'unlink' => [ - [ - 'from' => 'leads', - 'from_id' => 1125199, - 'to' => 'contacts', - 'to_id' => 3673249, - ] - ] - ] - ]; - - $this->model['from'] = 'leads'; - $this->model['from_id'] = 1125199; - $this->model['to'] = 'contacts'; - $this->model['to_id'] = 3673249; - - $this->assertTrue($this->model->apiUnlink()); - $this->assertEquals('/private/api/v2/json/links/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'links' => [ - 'unlink' => [ - [ - 'from' => 'leads', - 'from_id' => 1125199, - 'to' => 'contacts', - 'to_id' => 3673249, - ], - [ - 'from' => 'leads', - 'from_id' => 1125199, - 'to' => 'contacts', - 'to_id' => 3673249, - ] - ] - ] - ]; - - $this->assertTrue($this->model->apiUnlink([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/links/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['from', 'leads', 'leads'], - ['from_id', 100, 100], - ['to', 'contacts', 'contacts'], - ['to_id', 200, 200], - ['from_catalog_id', 300, 300], - ['to_catalog_id', 400, 400], - ['quantity', 500, 500], - ]; - } -} diff --git a/tests/Models/NoteTest.php b/tests/Models/NoteTest.php deleted file mode 100644 index 4d79327..0000000 --- a/tests/Models/NoteTest.php +++ /dev/null @@ -1,123 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['notes' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'notes' => [ - 'add' => [ - ['id' => 100], - ['id' => 200] - ], - 'update' => [ - ['id' => 100], - ['id' => 200] - ] - ] - ]; - } -} - -class NoteTest extends TestCase -{ - /** - * @var null|NoteMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new NoteMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $parameters = [ - 'query' => 'test', - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/notes/list', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $this->model['element_id'] = 100; - $this->model['element_type'] = 1; - $this->model['note_type'] = 4; - $this->model['text'] = 'Текст примечания'; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/notes/set', $this->model->mockUrl); - $this->assertNull($this->model->mockModified); - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/notes/set', $this->model->mockUrl); - $this->assertNull($this->model->mockModified); - } - - public function testApiUpdate() - { - $this->model['element_id'] = 100; - $this->model['element_type'] = 1; - $this->model['note_type'] = 4; - $this->model['text'] = 'Текст примечания'; - - $this->assertTrue($this->model->apiUpdate(1)); - $this->assertEquals('/private/api/v2/json/notes/set', $this->model->mockUrl); - $this->assertNull($this->model->mockModified); - - $this->assertTrue($this->model->apiUpdate(1, 'now')); - $this->assertEquals('/private/api/v2/json/notes/set', $this->model->mockUrl); - $this->assertNull($this->model->mockModified); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['element_id', 100, 100], - ['element_type', 100, 100], - ['note_type', 100, 100], - ['date_create', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['last_modified', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['request_id', 100, 100], - ['text', "Line 1\nLine 2", "Line 1\nLine 2"], - ['responsible_user_id', 100, 100], - ['created_user_id', 100, 100], - ]; - } -} diff --git a/tests/Models/PipelinesTest.php b/tests/Models/PipelinesTest.php deleted file mode 100644 index 6613c60..0000000 --- a/tests/Models/PipelinesTest.php +++ /dev/null @@ -1,206 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['pipelines' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'pipelines' => [ - 'add' => [ - 'pipelines' => [ - ['id' => 100], - ['id' => 200] - ] - ], - 'update' => [ - 'pipelines' => [ - ['id' => 100], - ['id' => 200] - ] - ] - ] - ]; - } -} - -class PipelinesTest extends TestCase -{ - /** - * @var null|PipelinesMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new PipelinesMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $result = $this->model->apiList(); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/pipelines/list', $this->model->mockUrl); - $this->assertEmpty($this->model->mockParameters); - - $result = $this->model->apiList(100); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/pipelines/list', $this->model->mockUrl); - $this->assertEquals(['id' => 100], $this->model->mockParameters); - } - - public function testApiAdd() - { - $expected = [ - 'pipelines' => [ - 'add' => [ - [ - 'name' => 'Воронка 1', - 'sort' => 1, - 'is_main' => 'on', - 'statuses' => [ - 0 => [ - 'name' => 'Pending', - 'sort' => 10, - 'color' => '#fffeb2', - ], - 12345 => [ - 'id' => 12345, - 'name' => 'Done', - 'sort' => 20, - 'color' => '#f3beff', - ], - ] - ] - ] - ] - ]; - - $this->model['name'] = 'Воронка 1'; - $this->model['sort'] = 1; - $this->model['is_main'] = 1; - $this->model->addStatusField([ - 'name' => 'Pending', - 'sort' => 10, - 'color' => '#fffeb2', - ]); - $this->model->addStatusField([ - 'name' => 'Done', - 'sort' => 20, - 'color' => '#f3beff', - ], 12345); - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/pipelines/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - - $expected = [ - 'pipelines' => [ - 'add' => [ - [ - 'name' => 'Воронка 1', - 'sort' => 1, - 'is_main' => 'on', - 'statuses' => [ - 0 => [ - 'name' => 'Pending', - 'sort' => 10, - 'color' => '#fffeb2', - ], - 12345 => [ - 'id' => 12345, - 'name' => 'Done', - 'sort' => 20, - 'color' => '#f3beff', - ], - ] - ], - [ - 'name' => 'Воронка 1', - 'sort' => 1, - 'is_main' => 'on', - 'statuses' => [ - 0 => [ - 'name' => 'Pending', - 'sort' => 10, - 'color' => '#fffeb2', - ], - 12345 => [ - 'id' => 12345, - 'name' => 'Done', - 'sort' => 20, - 'color' => '#f3beff', - ], - ] - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/pipelines/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - } - - public function testApiUpdate() - { - $this->model['name'] = 'Воронка'; - - $this->assertTrue($this->model->apiUpdate(1)); - $this->assertEquals('/private/api/v2/json/pipelines/set', $this->model->mockUrl); - $this->assertEquals(1, $this->model->mockParameters['pipelines']['update'][1]['id']); - $this->assertEquals('Воронка', $this->model->mockParameters['pipelines']['update'][1]['name']); - } - - public function testApiDelete() - { - $this->model->apiDelete(1); - $this->assertEquals('/private/api/v2/json/pipelines/delete', $this->model->mockUrl); - $this->assertEquals(['id' => 1], $this->model->mockParameters); - - $this->model->apiDelete([1, 2]); - $this->assertEquals('/private/api/v2/json/pipelines/delete', $this->model->mockUrl); - $this->assertEquals(['id' => [1, 2]], $this->model->mockParameters); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['name', 'ФИО', 'ФИО'], - ['sort', 100, 100], - ['is_main', 'on', 'on'], - ['is_main', 1, 'on'], - ['is_main', true, 'on'], - ]; - } -} diff --git a/tests/Models/TaskTest.php b/tests/Models/TaskTest.php deleted file mode 100644 index 3617c57..0000000 --- a/tests/Models/TaskTest.php +++ /dev/null @@ -1,133 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['tasks' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'tasks' => [ - 'add' => [ - ['id' => 100], - ['id' => 200] - ], - 'update' => [ - ['id' => 100], - ['id' => 200] - ] - ] - ]; - } -} - -class TaskTest extends TestCase -{ - /** - * @var null|TaskMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new TaskMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $parameters = [ - 'query' => 'test', - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/tasks/list', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $this->model['element_id'] = 11029224; - $this->model['element_type'] = 1; - $this->model['date_create'] = '-2 DAYS'; - $this->model['task_type'] = 1; - $this->model['text'] = "Текст\nзадачи"; - $this->model['responsible_user_id'] = 798027; - $this->model['complete_till'] = '+1 DAY'; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/tasks/set', $this->model->mockUrl); - $this->assertNull($this->model->mockModified); - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/tasks/set', $this->model->mockUrl); - $this->assertNull($this->model->mockModified); - } - - public function testApiUpdate() - { - $this->model['text'] = "Текст\nзадачи"; - - $this->assertTrue($this->model->apiUpdate(1, 'апдейт')); - $this->assertEquals('/private/api/v2/json/tasks/set', $this->model->mockUrl); - $this->assertNull($this->model->mockModified); - - $this->assertTrue($this->model->apiUpdate(1, 'апдейт', 'now')); - $this->assertEquals('/private/api/v2/json/tasks/set', $this->model->mockUrl); - $this->assertNull($this->model->mockModified); - } - - /** - * @expectedException \AmoCRM\Exception - */ - public function testApiUpdateBad() - { - $this->model->apiUpdate('foo', 'bar'); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['element_id', 100, 100], - ['element_type', 100, 100], - ['date_create', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['last_modified', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['status', 100, 100], - ['request_id', 100, 100], - ['task_type', 100, 100], - ['text', "Line 1\nLine 2", "Line 1\nLine 2"], - ['responsible_user_id', 100, 100], - ['complete_till', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['created_user_id', 100, 100], - ]; - } -} diff --git a/tests/Models/TransactionTest.php b/tests/Models/TransactionTest.php deleted file mode 100644 index 6cad9d6..0000000 --- a/tests/Models/TransactionTest.php +++ /dev/null @@ -1,139 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['transactions' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'transactions' => [ - 'add' => [ - 'transactions' => [ - ['id' => 100], - ['id' => 100], - ], - 'errors' => [], - ], - 'delete' => [ - 'transactions' => [ - ['id' => 100] - ], - 'errors' => [], - ] - ] - ]; - } -} - -class TransactionTest extends TestCase -{ - /** - * @var null|TransactionMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new TransactionMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $result = $this->model->apiList(); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/transactions/list', $this->model->mockUrl); - $this->assertEquals([], $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $expected = [ - 'transactions' => [ - 'add' => [ - [ - 'customer_id' => 29729, - 'price' => 3500, - ] - ] - ] - ]; - - $this->model['customer_id'] = 29729; - $this->model['price'] = 3500; - - $this->assertEquals(100, $this->model->apiAdd()); - $this->assertEquals('/private/api/v2/json/transactions/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - - $expected = [ - 'transactions' => [ - 'add' => [ - [ - 'customer_id' => 29729, - 'price' => 3500, - ], - [ - 'customer_id' => 29729, - 'price' => 3500, - ] - ] - ] - ]; - - $this->assertCount(2, $this->model->apiAdd([$this->model, $this->model])); - $this->assertEquals('/private/api/v2/json/transactions/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiDelete() - { - $this->assertTrue($this->model->apiDelete(1)); - $this->assertEquals('/private/api/v2/json/transactions/set', $this->model->mockUrl); - $this->assertEquals([1], $this->model->mockParameters['transactions']['delete']); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['customer_id', 100, 100], - ['date', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['price', 200, 200], - ['comment', 'Comment for transaction', 'Comment for transaction'], - ['request_id', 300, 300], - ['next_price', 400, 400], - ['next_date', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ]; - } -} diff --git a/tests/Models/UnsortedTest.php b/tests/Models/UnsortedTest.php deleted file mode 100644 index b98d531..0000000 --- a/tests/Models/UnsortedTest.php +++ /dev/null @@ -1,236 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['unsorted' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'unsorted' => [ - 'add' => [ - 'data' => [ - '100' => 100, - ] - ], - ] - ]; - } -} - -class UnsortedTest extends TestCase -{ - /** - * @var null|UnsortedMock - */ - private $model = null; - - /** - * @var null|\AmoCRM\Request\ParamsBag - */ - private $paramsBag = null; - - public function setUp() - { - $this->paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new UnsortedMock($this->paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $parameters = [ - 'page_size' => 10, - 'PAGEN_1' => 1, - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/api/unsorted/list/', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiGetAllSummary() - { - $result = $this->model->apiGetAllSummary(); - - $this->assertEquals([], $result); - $this->assertEquals('/api/unsorted/get_all_summary/', $this->model->mockUrl); - $this->assertEmpty($this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAdd() - { - $expected = [ - 'unsorted' => [ - 'category' => 'mail', - 'add' => [ - [ - 'source' => 'some@mail.from', - 'source_uid' => '06ea27be-b26e-4ce4-8c20-cb4261a65752', - 'source_data' => [ - 'from' => [ - 'email' => 'info@site.hh.ru', - 'name' => 'HeadHunter', - ], - 'date' => 1446544372, - 'subject' => 'Did you like me?', - 'thread_id' => 11774, - 'message_id' => 23698, - ], - ] - ] - ] - ]; - - $this->model['source'] = 'some@mail.from'; - $this->model['source_uid'] = '06ea27be-b26e-4ce4-8c20-cb4261a65752'; - $this->model['source_data'] = [ - 'from' => [ - 'email' => 'info@site.hh.ru', - 'name' => 'HeadHunter', - ], - 'date' => 1446544372, - 'subject' => 'Did you like me?', - 'thread_id' => 11774, - 'message_id' => 23698, - ]; - - $this->assertEquals(100, $this->model->apiAdd(UnsortedMock::TYPE_MAIL)); - $this->assertEquals('/api/unsorted/add/', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiAddSip() - { - $this->assertEquals(100, $this->model->apiAddSip()); - $this->assertEquals('/api/unsorted/add/', $this->model->mockUrl); - $this->assertEquals('sip', $this->model->mockParameters['unsorted']['category']); - $this->assertNull($this->model->mockModified); - } - - public function testApiAddMail() - { - $this->assertEquals(100, $this->model->apiAddMail()); - $this->assertEquals('/api/unsorted/add/', $this->model->mockUrl); - $this->assertEquals('mail', $this->model->mockParameters['unsorted']['category']); - $this->assertNull($this->model->mockModified); - } - - public function testApiAddForms() - { - $this->assertEquals(100, $this->model->apiAddForms()); - $this->assertEquals('/api/unsorted/add/', $this->model->mockUrl); - $this->assertEquals('forms', $this->model->mockParameters['unsorted']['category']); - $this->assertNull($this->model->mockModified); - } - - public function testApiAccept() - { - $expected = [ - 'unsorted' => [ - 'accept' => [ - 100 - ], - 'user_id' => 200, - 'status_id' => 300, - ], - ]; - - $this->model->apiAccept(100, 200, 300); - $this->assertEquals('/api/unsorted/accept/', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiDecline() - { - $expected = [ - 'unsorted' => [ - 'decline' => [ - 100 - ], - 'user_id' => 200, - ], - ]; - - $this->model->apiDecline(100, 200); - $this->assertEquals('/api/unsorted/decline/', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testAddDataLead() - { - $lead = new \AmoCRM\Models\Lead($this->paramsBag); - $lead['name'] = 'New lead from this form'; - - $this->model->addDataLead($lead); - $this->assertArrayHasKey('leads', $this->model['data']); - $this->assertCount(1, $this->model['data']['leads']); - $this->assertEquals($lead->getValues(), $this->model['data']['leads'][0]); - - $this->model->addDataLead([$lead, $lead]); - $this->assertArrayHasKey('leads', $this->model['data']); - $this->assertCount(3, $this->model['data']['leads']); - $this->assertEquals($lead->getValues(), $this->model['data']['leads'][1]); - $this->assertEquals($lead->getValues(), $this->model['data']['leads'][2]); - } - - public function testAddDataContact() - { - $contact = new \AmoCRM\Models\Contact($this->paramsBag); - $contact['name'] = 'New contact from this form'; - - $this->model->addDataContact($contact); - $this->assertArrayHasKey('contacts', $this->model['data']); - $this->assertCount(1, $this->model['data']['contacts']); - $this->assertEquals($contact->getValues(), $this->model['data']['contacts'][0]); - - $this->model->addDataContact([$contact, $contact]); - $this->assertArrayHasKey('contacts', $this->model['data']); - $this->assertCount(3, $this->model['data']['contacts']); - $this->assertEquals($contact->getValues(), $this->model['data']['contacts'][1]); - $this->assertEquals($contact->getValues(), $this->model['data']['contacts'][2]); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['source', 'www.my-awesome-site.com', 'www.my-awesome-site.com'], - ['source_uid', null, null], - ['source_uid', '06ea27be-b26e-4ce4-8c20-cb4261a65752', '06ea27be-b26e-4ce4-8c20-cb4261a65752'], - ['date_create', '2016-04-01 00:00:00', strtotime('2016-04-01 00:00:00')], - ['pipeline_id', 100, 100], - ]; - } -} diff --git a/tests/Models/WebhooksTest.php b/tests/Models/WebhooksTest.php deleted file mode 100644 index 680ef52..0000000 --- a/tests/Models/WebhooksTest.php +++ /dev/null @@ -1,280 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return [ - 'webhooks' => [ - [ - 'url' => 'http://example.com/', - 'result' => 1, - ] - ] - ]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return [ - 'webhooks' => [ - 'subscribe' => [ - [ - 'url' => 'http://example.com/', - 'result' => 1, - ] - ], - 'unsubscribe' => [ - [ - 'url' => 'http://example.com/', - 'result' => 1, - ] - ], - ] - ]; - } -} - -class WebhooksTest extends TestCase -{ - /** - * @var null|WebhooksMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new WebhooksMock($paramsBag); - } - - /** - * @dataProvider fieldsProvider - */ - public function testFields($field, $value, $expected) - { - $this->model[$field] = $value; - - $this->assertEquals($this->model[$field], $expected); - } - - public function testApiList() - { - $result = $this->model->apiList(); - - $this->assertEquals(1, count($result)); - $this->assertEquals(1, $result[0]['result']); - $this->assertEquals('/private/api/v2/json/webhooks/list', $this->model->mockUrl); - $this->assertEquals([], $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiSubscribe() - { - $expected = [ - 'webhooks' => [ - 'subscribe' => [ - [ - 'url' => 'http://example.com/', - 'events' => ['status_lead'], - ] - ] - ] - ]; - - $result = $this->model->apiSubscribe('http://example.com/', 'status_lead'); - - $this->assertEquals(1, $result); - $this->assertEquals('/private/api/v2/json/webhooks/subscribe', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiSubscribeModel() - { - $expected = [ - 'webhooks' => [ - 'subscribe' => [ - [ - 'url' => 'http://example.com/', - 'events' => ['status_lead'], - ] - ] - ] - ]; - - $this->model['url'] = 'http://example.com/'; - $this->model['events'] = 'status_lead'; - - $result = $this->model->apiSubscribe(); - - $this->assertEquals(1, $result); - $this->assertEquals('/private/api/v2/json/webhooks/subscribe', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiSubscribeMulti() - { - $expected = [ - 'webhooks' => [ - 'subscribe' => [ - [ - 'url' => 'http://example.com/', - 'events' => [ - 'add_contact', - 'update_contact', - 'delete_contact' - ], - ] - ] - ] - ]; - - $result = $this->model->apiSubscribe('http://example.com/', [ - 'add_contact', - 'update_contact', - 'delete_contact' - ]); - - $this->assertEquals(1, $result); - $this->assertEquals('/private/api/v2/json/webhooks/subscribe', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUnsubscribe() - { - $expected = [ - 'webhooks' => [ - 'unsubscribe' => [ - [ - 'url' => 'http://example.com/', - 'events' => ['status_lead'], - ] - ] - ] - ]; - - $result = $this->model->apiUnsubscribe('http://example.com/', 'status_lead'); - - $this->assertEquals(1, $result); - $this->assertEquals('/private/api/v2/json/webhooks/unsubscribe', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUnsubscribeModel() - { - $expected = [ - 'webhooks' => [ - 'unsubscribe' => [ - [ - 'url' => 'http://example.com/', - 'events' => ['status_lead'], - ] - ] - ] - ]; - - $this->model['url'] = 'http://example.com/'; - $this->model['events'] = 'status_lead'; - - $result = $this->model->apiUnsubscribe(); - - $this->assertEquals(1, $result); - $this->assertEquals('/private/api/v2/json/webhooks/unsubscribe', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUnsubscribeMulti() - { - $expected = [ - 'webhooks' => [ - 'unsubscribe' => [ - [ - 'url' => 'http://example.com/', - 'events' => [ - 'add_contact', - 'update_contact', - 'delete_contact' - ], - ] - ] - ] - ]; - - $result = $this->model->apiUnsubscribe('http://example.com/', [ - 'add_contact', - 'update_contact', - 'delete_contact' - ]); - - $this->assertEquals(1, $result); - $this->assertEquals('/private/api/v2/json/webhooks/unsubscribe', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function fieldsProvider() - { - return [ - // field, value, expected - ['url', 'http://example.com/', 'http://example.com/'], - ['events', 'status_lead', ['status_lead']], - [ - 'events', - [ - 'add_contact', - 'update_contact', - 'delete_contact' - ], - [ - 'add_contact', - 'update_contact', - 'delete_contact' - ] - ], - [ - 'events', - null, - [ - 'add_lead', // Добавить сделку - 'add_contact', // Добавить контакт - 'add_company', // Добавить компанию - 'add_customer', // Добавить покупателя - 'update_lead', // Изменить сделку - 'update_contact', // Изменить контакт - 'update_company', // Изменить компанию - 'update_customer', // Изменить покупателя - 'delete_lead', // Удалить сделку - 'delete_contact', // Удалить контакт - 'delete_company', // Удалить компанию - 'delete_customer', // Удалить покупателя - 'status_lead', // Смена статуса сделки - 'responsible_lead', // Смена отв-го сделки - 'restore_contact', // Восстановить контакт - 'restore_company', // Восстановить компанию - 'restore_lead', // Восстановить сделку - 'note_lead', // Примечание в сделке - 'note_contact', // Примечание в контакте - 'note_company', // Примечание в компании - 'note_customer', // Примечание в покупателе - ] - ], - ]; - } -} diff --git a/tests/Models/WidgetsTest.php b/tests/Models/WidgetsTest.php deleted file mode 100644 index cd9fe59..0000000 --- a/tests/Models/WidgetsTest.php +++ /dev/null @@ -1,94 +0,0 @@ -mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = $modified; - - return ['widgets' => []]; - } - - protected function postRequest($url, $parameters = []) - { - $this->mockUrl = $url; - $this->mockParameters = $parameters; - $this->mockModified = null; - - return ['widgets' => []]; - } -} - -class WidgetsTest extends TestCase -{ - /** - * @var null|WidgetsMock - */ - private $model = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $this->model = new WidgetsMock($paramsBag); - } - - public function testApiList() - { - $parameters = [ - 'widget_id' => 62121 - ]; - - $result = $this->model->apiList($parameters); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/widgets/list', $this->model->mockUrl); - $this->assertEquals($parameters, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiInstall() - { - $expected = [ - 'widgets' => [ - 'install' => [ - 'widget_id' => 62121 - ] - ] - ]; - - $result = $this->model->apiInstall([ - 'widget_id' => 62121 - ]); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/widgets/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } - - public function testApiUninstall() - { - $expected = [ - 'widgets' => [ - 'uninstall' => [ - 'widget_id' => 62121 - ] - ] - ]; - - $result = $this->model->apiUninstall([ - 'widget_id' => 62121 - ]); - - $this->assertEquals([], $result); - $this->assertEquals('/private/api/v2/json/widgets/set', $this->model->mockUrl); - $this->assertEquals($expected, $this->model->mockParameters); - $this->assertNull($this->model->mockModified); - } -} diff --git a/tests/Request/CurlHandleTest.php b/tests/Request/CurlHandleTest.php deleted file mode 100644 index 2118ea4..0000000 --- a/tests/Request/CurlHandleTest.php +++ /dev/null @@ -1,37 +0,0 @@ -handle = new \AmoCRM\Request\CurlHandle(); - } - - public function testOpen() - { - $ch = $this->handle->open(); - $this->assertInternalType('resource', $ch); - } - - public function testOpenWillReturnTheSameHandle() - { - $ch1 = $this->handle->open(); - $this->handle->close(); - $ch2 = $this->handle->open(); - $this->assertEquals($ch1, $ch2); - } - - public function testClose() - { - $ch = $this->handle->open(); - $this->handle->close(); - } - - public function testCloseWithoutOpen() - { - $this->handle->close(); - } -} diff --git a/tests/Request/ParamsBagTest.php b/tests/Request/ParamsBagTest.php deleted file mode 100644 index b488414..0000000 --- a/tests/Request/ParamsBagTest.php +++ /dev/null @@ -1,54 +0,0 @@ -params = new \AmoCRM\Request\ParamsBag(); - } - - public function testAuth() - { - $this->params->addAuth('key', 'value')->addAuth('foo', 'bar'); - - $this->assertEquals('value', $this->params->getAuth('key')); - $this->assertEquals('bar', $this->params->getAuth('foo')); - $this->assertCount(2, $this->params->getAuth()); - } - - public function testProxy() - { - $this->params->addProxy('http://proxy.url'); - - $this->assertTrue($this->params->hasProxy()); - $this->assertEquals('http://proxy.url', $this->params->getProxy()); - } - - public function testGet() - { - $this->params->addGet('key', 'value')->addGet(['foo' => 'bar']); - - $this->assertEquals('value', $this->params->getGet('key')); - $this->assertEquals('bar', $this->params->getGet('foo')); - $this->assertCount(2, $this->params->getGet()); - $this->assertTrue($this->params->hasGet()); - - $this->params->clearGet(); - $this->assertFalse($this->params->hasGet()); - } - - public function testPost() - { - $this->params->addPost('key', 'value')->addPost(['foo' => 'bar']); - - $this->assertEquals('value', $this->params->getPost('key')); - $this->assertEquals('bar', $this->params->getPost('foo')); - $this->assertCount(2, $this->params->getPost()); - $this->assertTrue($this->params->hasPost()); - - $this->params->clearPost(); - $this->assertFalse($this->params->hasPost()); - } -} diff --git a/tests/Request/RequestTest.php b/tests/Request/RequestTest.php deleted file mode 100644 index 44b55d1..0000000 --- a/tests/Request/RequestTest.php +++ /dev/null @@ -1,260 +0,0 @@ -v1 = $value; - } - - protected function request($url, $modified = null) - { - return []; - } -} - -class RequestTest extends TestCase -{ - /** - * @var null|RequestMock - */ - private $request = null; - - public function setUp() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $paramsBag->addAuth('domain', 'example.amocrm.ru'); - $paramsBag->addAuth('login', 'login@domain'); - $paramsBag->addAuth('apikey', 'hash'); - $this->request = new RequestMock($paramsBag); - } - - public function testDebug() - { - $this->assertAttributeEquals(false, 'debug', $this->request); - $this->request->debug(true); - $this->assertAttributeEquals(true, 'debug', $this->request); - } - - public function testGetLastHttpCode() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $request = new \AmoCRM\Request\Request($paramsBag); - - $this->setProtectedProperty($request, 'lastHttpCode', 200); - $this->assertEquals(200, $request->getLastHttpCode()); - } - - public function testGetLastHttpResponse() - { - $paramsBag = new \AmoCRM\Request\ParamsBag(); - $request = new \AmoCRM\Request\Request($paramsBag); - - $this->setProtectedProperty($request, 'lastHttpResponse', 'foobar'); - $this->assertEquals('foobar', $request->getLastHttpResponse()); - } - - public function testGetParameters() - { - $actual = $this->invokeMethod($this->request, 'getParameters'); - $this->assertInstanceOf('\AmoCRM\Request\ParamsBag', $actual); - } - - public function testGetRequest() - { - $actual = $this->invokeMethod($this->request, 'getRequest', [ - '/foobar', - ['foo' => 'bar'], - 'now' - ]); - - $this->assertEquals([], $actual); - } - - public function testPostRequest() - { - $actual = $this->invokeMethod($this->request, 'postRequest', [ - '/foobar', - ['foo' => 'bar'] - ]); - - $this->assertEquals([], $actual); - } - - public function testPrepareHeaders() - { - date_default_timezone_set('UTC'); - - $dt = '2017-01-02 12:30:00'; - $ts = strtotime($dt); - - $actual = $this->invokeMethod($this->request, 'prepareHeaders'); - - $this->assertCount(2, $actual); - $this->assertContains('Content-Type: application/json', $actual); - - $actual = $this->invokeMethod($this->request, 'prepareHeaders', [$dt]); - $this->assertCount(3, $actual); - $this->assertContains('IF-MODIFIED-SINCE: Mon, 02 Jan 2017 12:30:00 +0000', $actual); - - $actual = $this->invokeMethod($this->request, 'prepareHeaders', [$ts]); - $this->assertCount(3, $actual); - $this->assertContains('IF-MODIFIED-SINCE: 1483360200', $actual); - } - - /** - * @expectedException \Exception - */ - public function testIncorrectPrepareHeaders() - { - $this->invokeMethod($this->request, 'prepareHeaders', ['foobar']); - } - - public function testPrepareEndpointV1() - { - $this->request->v1(true); - $expected = 'https://example.amocrm.ru/foo/?login=login%40domain&api_key=hash'; - $actual = $this->invokeMethod($this->request, 'prepareEndpoint', ['/foo/']); - - $this->assertEquals($expected, $actual); - } - - public function testPrepareEndpointV2() - { - $expected = 'https://example.amocrm.ru/foo/?USER_LOGIN=login%40domain&USER_HASH=hash'; - $actual = $this->invokeMethod($this->request, 'prepareEndpoint', ['/foo/']); - - $this->assertEquals($expected, $actual); - } - - public function testParseResponse() - { - $response = json_encode([ - 'response' => [ - 'foo' => 'bar', - ] - ]); - $info = [ - 'http_code' => 200 - ]; - - $actual = $this->invokeMethod($this->request, 'parseResponse', [$response, $info]); - $this->assertArrayHasKey('foo', $actual); - $this->assertEquals('bar', $actual['foo']); - } - - public function testParseResponseEmpty() - { - $actual = $this->invokeMethod($this->request, 'parseResponse', [null, null]); - $this->assertFalse($actual); - } - - /** - * @expectedException \AmoCRM\Exception - * @expectedExceptionCode 101 - * @expectedExceptionMessage Аккаунт не найден - */ - public function testParseResponseWithError() - { - $response = json_encode([ - 'response' => [ - 'error_code' => '101', - 'error' => 'Аккаунт не найден', - ] - ]); - $info = [ - 'http_code' => 400 - ]; - - $this->invokeMethod($this->request, 'parseResponse', [$response, $info]); - } - - /** - * @expectedException \AmoCRM\Exception - * @expectedExceptionCode 0 - * @expectedExceptionMessage Аккаунт не найден - */ - public function testParseResponseWithoutCode() - { - $response = json_encode([ - 'response' => [ - 'error' => 'Аккаунт не найден', - ] - ]); - $info = [ - 'http_code' => 400 - ]; - - $this->invokeMethod($this->request, 'parseResponse', [$response, $info]); - } - - /** - * @expectedException \AmoCRM\Exception - * @expectedExceptionCode 0 - * @expectedExceptionMessage {"foo":"bar"} - */ - public function testParseResponseWithErrorV1() - { - $response = json_encode([ - 'response' => [ - 'foo' => 'bar', - ] - ]); - $info = [ - 'http_code' => 400 - ]; - - $this->request->v1(true); - $this->invokeMethod($this->request, 'parseResponse', [$response, $info]); - } - - /** - * @expectedException \AmoCRM\Exception - * @expectedExceptionCode 403 - * @expectedExceptionMessage Аккаунт заблокирован, за неоднократное превышение количества запросов в секунду - */ - public function testParseInvalidResponseWithHttpError() - { - $response = '~~GO AWAY~~'; - $info = [ - 'http_code' => 403, - ]; - - $this->invokeMethod($this->request, 'parseResponse', [$response, $info]); - } - - /** - * @expectedException \AmoCRM\Exception - * @expectedExceptionCode 502 - * @expectedExceptionMessage Invalid response body. - */ - public function testParseInvalidResponseWithHttpError502() - { - $response = 'Bad Gateway'; - $info = [ - 'http_code' => 502, - ]; - - $this->invokeMethod($this->request, 'parseResponse', [$response, $info]); - } - - public function testPrintDebug() - { - $this->request->debug(true); - - $actual = $this->invokeMethod($this->request, 'printDebug', ['foo', 'bar', true]); - $this->assertStringStartsWith('[DEBUG]', $actual); - $this->assertRegExp('/foo: bar/u', $actual); - - $actual = $this->invokeMethod($this->request, 'printDebug', ['foo', [100 => 200], true]); - $this->assertStringStartsWith('[DEBUG]', $actual); - $this->assertRegExp('/Array/u', $actual); - $this->assertRegExp('/\[100\] => 200/u', $actual); - } - - public function testPrintDebugOff() - { - $actual = $this->invokeMethod($this->request, 'printDebug'); - $this->assertFalse($actual); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index 0a76409..0000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,37 +0,0 @@ -getProperty($property); - $reflection_property->setAccessible(true); - $reflection_property->setValue($object, $value); - } - - /** - * Call protected/private method of a class. - * - * @param object &$object Instantiated object that we will run method on. - * @param string $methodName Method name to call - * @param array $parameters Array of parameters to pass into method. - * @return mixed Method return. - */ - public function invokeMethod(&$object, $methodName, array $parameters = []) - { - $reflection = new ReflectionClass(get_class($object)); - $method = $reflection->getMethod($methodName); - $method->setAccessible(true); - - return $method->invokeArgs($object, $parameters); - } -} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/add_company.json b/tests/Webhooks/Fixtures/add_company.json deleted file mode 100644 index bee530f..0000000 --- a/tests/Webhooks/Fixtures/add_company.json +++ /dev/null @@ -1 +0,0 @@ -{"contacts":{"add":[{"id":"22000020","name":"New company","responsible_user_id":"151516","date_create":"1483965006","last_modified":"1483965006","created_user_id":"151516","modified_user_id":"151516","type":"company"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/add_contact.json b/tests/Webhooks/Fixtures/add_contact.json deleted file mode 100644 index 7adafd5..0000000 --- a/tests/Webhooks/Fixtures/add_contact.json +++ /dev/null @@ -1 +0,0 @@ -{"contacts":{"add":[{"id":"21999540","name":"New contact","responsible_user_id":"151516","date_create":"1483964916","last_modified":"1483964916","created_user_id":"151516","modified_user_id":"151516","type":"contact"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/add_lead.json b/tests/Webhooks/Fixtures/add_lead.json deleted file mode 100644 index 2604587..0000000 --- a/tests/Webhooks/Fixtures/add_lead.json +++ /dev/null @@ -1 +0,0 @@ -{"leads":{"add":[{"id":"8000832","name":"New lead","status_id":"13352724","price":"1000","responsible_user_id":"151516","last_modified":"1483964859","modified_user_id":"151516","created_user_id":"151516","date_create":"1483964859","pipeline_id":"416904","account_id":"13352718"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/delete_company.json b/tests/Webhooks/Fixtures/delete_company.json deleted file mode 100644 index 28ef944..0000000 --- a/tests/Webhooks/Fixtures/delete_company.json +++ /dev/null @@ -1 +0,0 @@ -{"contacts":{"delete":[{"id":"22000020","type":"company"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/delete_contact.json b/tests/Webhooks/Fixtures/delete_contact.json deleted file mode 100644 index 8373da8..0000000 --- a/tests/Webhooks/Fixtures/delete_contact.json +++ /dev/null @@ -1 +0,0 @@ -{"contacts":{"delete":[{"id":"21999540","type":"contact"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/delete_lead.json b/tests/Webhooks/Fixtures/delete_lead.json deleted file mode 100644 index 25d6ce3..0000000 --- a/tests/Webhooks/Fixtures/delete_lead.json +++ /dev/null @@ -1 +0,0 @@ -{"leads":{"delete":[{"id":"8000832"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/note_company.json b/tests/Webhooks/Fixtures/note_company.json deleted file mode 100644 index b10212d..0000000 --- a/tests/Webhooks/Fixtures/note_company.json +++ /dev/null @@ -1 +0,0 @@ -{"contacts":{"note":[{"note":{"text":"'Note to company'","attachement":"''","date_create":"'2017-01-09 13:01:34'","note_type":"4","element_type":"3","group_id":"0","element_id":"21992826","timestamp_x":"'2017-01-09 13:01:35'","account_id":"13352718","created_by":"151516","modified_by":"151516","main_user_id":"151516"},"type":"company"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/note_contact.json b/tests/Webhooks/Fixtures/note_contact.json deleted file mode 100644 index 2770590..0000000 --- a/tests/Webhooks/Fixtures/note_contact.json +++ /dev/null @@ -1 +0,0 @@ -{"contacts":{"note":[{"note":{"text":"'Note to contact'","attachement":"''","date_create":"'2017-01-09 12:36:16'","note_type":"4","element_type":"1","group_id":"0","element_id":"21999476","timestamp_x":"'2017-01-09 12:36:16'","account_id":"13352718","created_by":"151516","modified_by":"151516","main_user_id":"151516"},"type":"contact"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/note_lead.json b/tests/Webhooks/Fixtures/note_lead.json deleted file mode 100644 index fb9276e..0000000 --- a/tests/Webhooks/Fixtures/note_lead.json +++ /dev/null @@ -1 +0,0 @@ -{"leads":{"note":[{"note":{"text":"'Note to lead'","attachement":"''","date_create":"'2017-01-09 13:01:18'","note_type":"4","element_type":"2","group_id":"0","element_id":"8001508","timestamp_x":"'2017-01-09 13:01:19'","account_id":"13352718","created_by":"151516","modified_by":"151516","main_user_id":"1234095"}}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/status_lead.json b/tests/Webhooks/Fixtures/status_lead.json deleted file mode 100644 index b373525..0000000 --- a/tests/Webhooks/Fixtures/status_lead.json +++ /dev/null @@ -1 +0,0 @@ -{"leads":{"status":[{"id":"8001508","name":"Lead","status_id":"13352730","price":"0","responsible_user_id":"1234095","last_modified":"1483966700","modified_user_id":"151516","created_user_id":"151516","date_create":"1483965216","pipeline_id":"416904","account_id":"13352718","old_status_id":"13352727"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/update_company.json b/tests/Webhooks/Fixtures/update_company.json deleted file mode 100644 index 728d1e8..0000000 --- a/tests/Webhooks/Fixtures/update_company.json +++ /dev/null @@ -1 +0,0 @@ -{"contacts":{"update":[{"id":"22000020","name":"Update company","responsible_user_id":"151516","date_create":"1483965006","last_modified":"1483965063","created_user_id":"151516","modified_user_id":"151516","type":"company"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/update_contact.json b/tests/Webhooks/Fixtures/update_contact.json deleted file mode 100644 index 7d7d34c..0000000 --- a/tests/Webhooks/Fixtures/update_contact.json +++ /dev/null @@ -1 +0,0 @@ -{"contacts":{"update":[{"id":"21999540","name":"Update contact","responsible_user_id":"151516","date_create":"1483964916","last_modified":"1483965051","created_user_id":"151516","modified_user_id":"151516","type":"contact"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/Fixtures/update_lead.json b/tests/Webhooks/Fixtures/update_lead.json deleted file mode 100644 index 2910850..0000000 --- a/tests/Webhooks/Fixtures/update_lead.json +++ /dev/null @@ -1 +0,0 @@ -{"leads":{"update":[{"id":"8000832","name":"Update lead","status_id":"13352724","price":"1000","responsible_user_id":"151516","last_modified":"1483965034","modified_user_id":"151516","created_user_id":"151516","date_create":"1483964859","pipeline_id":"416904","account_id":"13352718"}]},"account":{"subdomain":"example"}} \ No newline at end of file diff --git a/tests/Webhooks/ListenerTest.php b/tests/Webhooks/ListenerTest.php deleted file mode 100644 index 9373db9..0000000 --- a/tests/Webhooks/ListenerTest.php +++ /dev/null @@ -1,52 +0,0 @@ -fired = false; - $this->listener = new \AmoCRM\Webhooks\Listener(); - } - - /** - * @dataProvider eventsProvider - */ - public function testOn($event, $data) - { - $_POST = $data; - - $this->assertFalse($this->fired); - $this->listener->clean()->on($event, [$this, 'mockCallback'])->listen(); - $this->assertTrue($this->fired); - } - - public function eventsProvider() - { - $events = []; - - foreach (glob(__DIR__ . '/Fixtures/*.json') as $filename) { - $event = pathinfo($filename, PATHINFO_FILENAME); - $data = json_decode(file_get_contents($filename), true); - $events[] = [$event, $data]; - } - - return $events; - } - - public function mockCallback($domain, $id, $data) - { - $this->fired = true; - $this->assertEquals('example', $domain); - $this->assertNotEmpty($data); - } -} \ No newline at end of file