<commit_before><?php

namespace common\tests\unit\lead;

use common\fixtures\helpers\LeadFixtureHelper;
use common\modules\lead\models\LeadMultipleSmsForm;
use common\tests\_support\UnitModelTrait;
use common\tests\unit\Unit;
use console\jobs\LeadSmsSendJob;
use Yii;

class LeadMultipleSmsFormTest extends Unit {

	use UnitModelTrait;

	private const DEFAULT_OWNER_ID = 1;

	private LeadMultipleSmsForm $model;

	public function _fixtures(): array {
		return array_merge(
			LeadFixtureHelper::lead(),
			LeadFixtureHelper::status(),
			LeadFixtureHelper::reports()
		);
	}

	public function testEmpty(): void {
		$this->giveModel();
		$this->model->owner_id = null;
		$this->thenUnsuccessValidate();
		$this->thenSeeError('Message cannot be blank.', 'message');
		$this->thenSeeError('Owner cannot be blank.', 'owner_id');
		$this->thenSeeError('Status cannot be blank.', 'status_id');
		$this->thenSeeError('Ids cannot be blank when Models are empty.', 'ids');
		$this->thenSeeError('Models cannot be blank when Ids are empty.', 'models');
	}

	public function testInvalidStatus(): void {
		$this->giveModel([
			'status_id' => -2,
		]);

		$this->thenUnsuccessValidate();
		$this->thenSeeError('Status is invalid.', 'status_id');
	}

	public function testFromIds(): void {
		$this->giveModel([
			'ids' => [1, 3, 5],
			'status_id' => 2,
			'message' => 'Message for #1 and #2',
		]);

		$this->thenSuccessValidate();
		$jobs = $this->model->pushJobs();
		$this->tester->assertCount(2, $jobs);
		foreach ($this->model->getMessages() as $message) {
			$this->tester->assertTrue(
				$message->getDst() === '48777222122' ||
				$message->getDst() === '48555222111');
		}
		$job = $this->tester->grabLastPushedJob();

		$this->tester->assertInstanceOf(LeadSmsSendJob::class, $job);
		$this->tester->assertSame($job->lead_id, 5);
	}

	public function testFromModels(): void {
		$leadWithPhone = $this->tester->grabFixture(LeadFixtureHelper::LEAD, 'new-wordpress-accident');
		$leadWithoutPhone = $this->tester->grabFixture(LeadFixtureHelper::LEAD, 'new-without-phone-and-owner');
		$leads = [];
		$leads[] = $leadWithoutPhone;
		$leads[] = $leadWithPhone;
		$this->giveModel([
			'status_id' => 2,
			'message' => 'Message for #1 and #2',
		]);
		$this->model->models = $leads;
		$this->thenSuccessValidate();
		$jobs = $this->model->pushJobs();
		$this->tester->assertCount(1, $jobs);
		$job = $this->tester->grabLastPushedJob();
		$this->tester->assertInstanceOf(LeadSmsSendJob::class, $job);
		$this->tester->assertSame($job->lead_id, 1);
	}

	private function giveModel(array $config = []): void {
		if (!isset($config['owner_id'])) {
			$config['owner_id'] = static::DEFAULT_OWNER_ID;
		}
		$this->model = new LeadMultipleSmsForm($config);
	}

	public function getModel(): LeadMultipleSmsForm {
		return $this->model;
	}
}
<commit_msg>test: fix lead fixture index for Lead without phone.<commit_after><?php

namespace common\tests\unit\lead;

use common\fixtures\helpers\LeadFixtureHelper;
use common\modules\lead\models\LeadMultipleSmsForm;
use common\tests\_support\UnitModelTrait;
use common\tests\unit\Unit;
use console\jobs\LeadSmsSendJob;
use Yii;

class LeadMultipleSmsFormTest extends Unit {

	use UnitModelTrait;

	private const DEFAULT_OWNER_ID = 1;

	private LeadMultipleSmsForm $model;

	public function _fixtures(): array {
		return array_merge(
			LeadFixtureHelper::lead(),
			LeadFixtureHelper::status(),
			LeadFixtureHelper::reports()
		);
	}

	public function testEmpty(): void {
		$this->giveModel();
		$this->model->owner_id = null;
		$this->thenUnsuccessValidate();
		$this->thenSeeError('Message cannot be blank.', 'message');
		$this->thenSeeError('Owner cannot be blank.', 'owner_id');
		$this->thenSeeError('Status cannot be blank.', 'status_id');
		$this->thenSeeError('Ids cannot be blank when Models are empty.', 'ids');
		$this->thenSeeError('Models cannot be blank when Ids are empty.', 'models');
	}

	public function testInvalidStatus(): void {
		$this->giveModel([
			'status_id' => -2,
		]);

		$this->thenUnsuccessValidate();
		$this->thenSeeError('Status is invalid.', 'status_id');
	}

	public function testFromIds(): void {
		$this->giveModel([
			'ids' => [1, 3, 5],
			'status_id' => 2,
			'message' => 'Message for #1 and #2',
		]);

		$this->thenSuccessValidate();
		$jobs = $this->model->pushJobs();
		$this->tester->assertCount(2, $jobs);
		foreach ($this->model->getMessages() as $message) {
			$this->tester->assertTrue(
				$message->getDst() === '48777222122' ||
				$message->getDst() === '48555222111');
		}
		$job = $this->tester->grabLastPushedJob();

		$this->tester->assertInstanceOf(LeadSmsSendJob::class, $job);
		$this->tester->assertSame($job->lead_id, 5);
	}

	public function testFromModels(): void {
		$leadWithPhone = $this->tester->grabFixture(LeadFixtureHelper::LEAD, 'new-wordpress-accident');
		$leadWithoutPhone = $this->tester->grabFixture(LeadFixtureHelper::LEAD, 'new-without-phone-and-owner_and_source-without-phone');
		$leads = [];
		$leads[] = $leadWithoutPhone;
		$leads[] = $leadWithPhone;
		$this->giveModel([
			'status_id' => 2,
			'message' => 'Message for #1 and #2',
		]);
		$this->model->models = $leads;
		$this->thenSuccessValidate();
		$jobs = $this->model->pushJobs();
		$this->tester->assertCount(1, $jobs);
		$job = $this->tester->grabLastPushedJob();
		$this->tester->assertInstanceOf(LeadSmsSendJob::class, $job);
		$this->tester->assertSame($job->lead_id, 1);
	}

	private function giveModel(array $config = []): void {
		if (!isset($config['owner_id'])) {
			$config['owner_id'] = static::DEFAULT_OWNER_ID;
		}
		$this->model = new LeadMultipleSmsForm($config);
	}

	public function getModel(): LeadMultipleSmsForm {
		return $this->model;
	}
}

{'fraction_non_alphanumeric': 0.17931754874651812, 'fraction_numerical': 0.01288300835654596, 'mean_word_length': 4.996868475991649, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2006962', 'n_tokens_mistral': 2214, 'n_tokens_neox': 1983, 'n_words': 382}