customFieldRepository = $this->diContainer->get(CustomFieldsRepository::class); $this->api = $this->diContainer->get(API::class); } public function testItReturnsDefaultSubscriberFields() { $response = $this->api->getSubscriberFields(); verify($response)->arrayContains([ 'id' => 'email', 'name' => __('Email', 'mailpoet'), 'type' => 'text', 'params' => [ 'required' => '1', ], ]); verify($response)->arrayContains([ 'id' => 'first_name', 'name' => __('First name', 'mailpoet'), 'type' => 'text', 'params' => [ 'required' => '', ], ]); verify($response)->arrayContains([ 'id' => 'last_name', 'name' => __('Last name', 'mailpoet'), 'type' => 'text', 'params' => [ 'required' => '', ], ]); } public function testItReturnsCustomFields() { $customField1 = $this->customFieldRepository->createOrUpdate([ 'name' => 'text custom field', 'type' => CustomFieldEntity::TYPE_TEXT, 'params' => ['required' => '1', 'date_type' => 'year_month_day'], ]); $customField2 = $this->customFieldRepository->createOrUpdate([ 'name' => 'checkbox custom field', 'type' => CustomFieldEntity::TYPE_CHECKBOX, 'params' => ['required' => ''], ]); $response = $this->api->getSubscriberFields(); verify($response)->arrayContains([ 'id' => 'cf_' . $customField1->getId(), 'name' => 'text custom field', 'type' => 'text', 'params' => [ 'required' => '1', 'date_type' => 'year_month_day', 'label' => 'text custom field', ], ]); verify($response)->arrayContains([ 'id' => 'cf_' . $customField2->getId(), 'name' => 'checkbox custom field', 'type' => 'checkbox', 'params' => [ 'required' => '', 'label' => 'checkbox custom field', ], ]); } public function testItCreateNewCustomField() { $response = $this->api->addSubscriberField([ 'name' => 'text custom field', 'type' => 'text', 'params' => [ 'required' => '1', 'label' => 'text custom field', 'date_type' => 'year_month_day', ], ]); verify($response)->isArray(); verify($response)->arrayHasKey('id'); verify($response)->arrayHasKey('name'); verify($response)->arrayHasKey('type'); verify($response)->arrayHasKey('params'); verify($response['params'])->isArray(); } public function testItFailsToCreateNewCustomField() { $this->expectException(APIException::class); $this->api->addSubscriberField([ 'type' => 'text', 'params' => [ 'required' => '1', 'label' => 'text custom field', 'date_type' => 'year_month_day', ], ]); } }