-
Notifications
You must be signed in to change notification settings - Fork 120
[ADD] price with minor units param #620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+232
−12
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,7 +111,7 @@ class LeadModel extends BaseApiModel implements | |
| protected $closestTaskAt; | ||
|
|
||
| /** | ||
| * @var int | ||
| * @var int|float|null | ||
| */ | ||
| protected $price; | ||
|
|
||
|
|
@@ -252,21 +252,28 @@ public function setName(string $name): self | |
| return $this; | ||
| } | ||
|
|
||
| public function getPrice(): ?int | ||
| { | ||
| return isset($this->price) ? (int)$this->price : null; | ||
| } | ||
|
|
||
| /** | ||
| * @return null|int | ||
| * На данный момент поддержка минорных единиц валют доступна только в Kommo | ||
| */ | ||
| public function getPrice(): ?int | ||
| public function getPriceWithMinorUnits(): ?float | ||
| { | ||
| return $this->price; | ||
| return isset($this->price) ? (float)$this->price : null; | ||
| } | ||
|
|
||
| /** | ||
| * @param null|int $price | ||
| * | ||
| * @return self | ||
| * @param int|float|null $price | ||
| */ | ||
| public function setPrice(?int $price): self | ||
| public function setPrice($price): self | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нужно добавить проверку на типы внутри метода, если убираем тип |
||
| { | ||
| if (!is_int($price) && !is_float($price) && !is_null($price)) { | ||
| throw new InvalidArgumentException('Lead price must be an integer, float or null.'); | ||
| } | ||
|
|
||
| $this->price = $price; | ||
|
|
||
| return $this; | ||
|
|
@@ -744,7 +751,9 @@ public static function fromArray(array $lead): self | |
| $leadModel->setName($lead['name']); | ||
| } | ||
|
|
||
| if (array_key_exists('price', $lead) && !is_null($lead['price'])) { | ||
| if (array_key_exists('price_with_minor_units', $lead) && !is_null($lead['price_with_minor_units'])) { | ||
| $leadModel->setPrice((float)$lead['price_with_minor_units']); | ||
| } elseif (array_key_exists('price', $lead) && !is_null($lead['price'])) { | ||
| $leadModel->setPrice((int)$lead['price']); | ||
| } | ||
|
|
||
|
|
@@ -861,6 +870,7 @@ public function toArray(): array | |
| $result = [ | ||
| 'name' => $this->getName(), | ||
| 'price' => $this->getPrice(), | ||
| 'price_with_minor_units' => $this->getPriceWithMinorUnits(), | ||
| 'responsible_user_id' => $this->getResponsibleUserId(), | ||
| 'group_id' => $this->getGroupId(), | ||
| 'status_id' => $this->getStatusId(), | ||
|
|
@@ -932,8 +942,8 @@ public function toApi(?string $requestId = "0"): array | |
| $result['name'] = $this->getName(); | ||
| } | ||
|
|
||
| if (!is_null($this->getPrice())) { | ||
| $result['price'] = $this->getPrice(); | ||
| if (!is_null($this->getPriceWithMinorUnits())) { | ||
| $result['price'] = $this->getPriceWithMinorUnits(); | ||
| } | ||
|
|
||
| if (!is_null($this->getResponsibleUserId())) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cases\Price; | ||
|
|
||
| use AmoCRM\Exceptions\InvalidArgumentException; | ||
| use AmoCRM\Models\LeadModel; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class LeadPriceTest extends TestCase | ||
| { | ||
| public function testSetPriceWithIntegerPreservesLegacyBehaviour(): void | ||
| { | ||
| $lead = (new LeadModel())->setPrice(100); | ||
|
|
||
| $this->assertPriceState($lead, 100, 100.0); | ||
| } | ||
|
|
||
| public function testSetPriceWithFloatExposesMinorUnitsAndTruncatesLegacyGetter(): void | ||
| { | ||
| $lead = (new LeadModel())->setPrice(100.5); | ||
|
|
||
| $this->assertPriceState($lead, 100, 100.5); | ||
| } | ||
|
|
||
| public function testSetPriceWithSmallFractionKeepsLegacyPriceAtZero(): void | ||
| { | ||
| $lead = (new LeadModel())->setPrice(0.1); | ||
|
|
||
| $this->assertPriceState($lead, 0, 0.1); | ||
| $this->assertSame(0, $lead->toArray()['price']); | ||
| $this->assertSame(0.1, $lead->toArray()['price_with_minor_units']); | ||
| $this->assertSame(0.1, $lead->toApi()['price']); | ||
| } | ||
|
|
||
| public function testSetPriceWithLargeFractionDoesNotRoundLegacyPriceUp(): void | ||
| { | ||
| $lead = (new LeadModel())->setPrice(99999999.999); | ||
|
|
||
| $this->assertPriceState($lead, 99999999, 99999999.999); | ||
| $this->assertNotSame(100000000, $lead->getPrice()); | ||
| $this->assertSame(99999999, $lead->toArray()['price']); | ||
| $this->assertSame(99999999.999, $lead->toArray()['price_with_minor_units']); | ||
| $this->assertSame(99999999.999, $lead->toApi()['price']); | ||
| } | ||
|
|
||
| public function testSetPriceWithNullLeavesBothRepresentationsNull(): void | ||
| { | ||
| $lead = (new LeadModel())->setPrice(null); | ||
|
|
||
| $this->assertPriceState($lead, null, null); | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidArgumentException | ||
| */ | ||
| public function testFromArrayUsesLegacyPriceWhenOnlyPriceIsProvided(): void | ||
| { | ||
| $this->assertPriceState( | ||
| LeadModel::fromArray(['id' => 1, 'price' => 200]), | ||
| 200, | ||
| 200.0 | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidArgumentException | ||
| */ | ||
| public function testFromArrayUsesMinorUnitsPriceWhenProvided(): void | ||
| { | ||
| $this->assertPriceState( | ||
| LeadModel::fromArray(['id' => 1, 'price_with_minor_units' => 200.75]), | ||
| 200, | ||
| 200.75 | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidArgumentException | ||
| */ | ||
| public function testFromArrayWithSmallFractionKeepsLegacyPriceAtZero(): void | ||
| { | ||
| $this->assertPriceState( | ||
| LeadModel::fromArray(['id' => 1, 'price_with_minor_units' => 0.1]), | ||
| 0, | ||
| 0.1 | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidArgumentException | ||
| */ | ||
| public function testFromArrayWithLargeFractionDoesNotRoundLegacyPriceUp(): void | ||
| { | ||
| $lead = LeadModel::fromArray([ | ||
| 'id' => 1, | ||
| 'price_with_minor_units' => 99999999.999, | ||
| ]); | ||
|
|
||
| $this->assertPriceState($lead, 99999999, 99999999.999); | ||
| $this->assertNotSame(100000000, $lead->getPrice()); | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidArgumentException | ||
| */ | ||
| public function testFromArrayPrefersMinorUnitsOverLegacyPrice(): void | ||
| { | ||
| $lead = LeadModel::fromArray([ | ||
| 'id' => 1, | ||
| 'price' => 300, | ||
| 'price_with_minor_units' => 300.99, | ||
| ]); | ||
|
|
||
| $this->assertPriceState($lead, 300, 300.99); | ||
| } | ||
|
|
||
| /** | ||
| * @throws InvalidArgumentException | ||
| */ | ||
| public function testFromArrayFallsBackToLegacyPriceWhenMinorUnitsIsNull(): void | ||
| { | ||
| $lead = LeadModel::fromArray([ | ||
| 'id' => 1, | ||
| 'price' => 400, | ||
| 'price_with_minor_units' => null, | ||
| ]); | ||
|
|
||
| $this->assertPriceState($lead, 400, 400.0); | ||
| } | ||
|
|
||
| public function testToArrayIncludesBothPriceRepresentations(): void | ||
| { | ||
| $this->assertSame( | ||
| [ | ||
| 'name' => null, | ||
| 'price' => 123, | ||
| 'price_with_minor_units' => 123.45, | ||
| 'responsible_user_id' => null, | ||
| 'group_id' => null, | ||
| 'status_id' => null, | ||
| 'pipeline_id' => null, | ||
| 'loss_reason_id' => null, | ||
| 'source_id' => null, | ||
| 'created_by' => null, | ||
| 'updated_by' => null, | ||
| 'created_at' => null, | ||
| 'updated_at' => null, | ||
| 'closed_at' => null, | ||
| 'closest_task_at' => null, | ||
| 'is_deleted' => null, | ||
| 'custom_fields_values' => null, | ||
| 'score' => null, | ||
| 'account_id' => null, | ||
| 'id' => 1, | ||
| ], | ||
| (new LeadModel()) | ||
| ->setId(1) | ||
| ->setPrice(123.45) | ||
| ->toArray() | ||
| ); | ||
| } | ||
|
|
||
| public function testToApiUsesMinorUnitsInPriceFieldForFloatInput(): void | ||
| { | ||
| $this->assertSame( | ||
| ['price' => 100.5, 'request_id' => '0'], | ||
| (new LeadModel())->setPrice(100.5)->toApi() | ||
| ); | ||
| } | ||
|
|
||
| public function testToApiUsesNumericPriceFieldForIntegerInput(): void | ||
| { | ||
| $this->assertSame( | ||
| ['price' => 100.0, 'request_id' => '0'], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. добавь тесты, если строку передадут, массив и тд) |
||
| (new LeadModel())->setPrice(100)->toApi() | ||
| ); | ||
| } | ||
|
|
||
| public function testToApiOmitsPriceWhenItWasNotSet(): void | ||
| { | ||
| $this->assertSame(['request_id' => '0'], (new LeadModel())->toApi()); | ||
| } | ||
|
|
||
| public function testSetPriceWithStringThrowsException(): void | ||
| { | ||
| $this->expectException(InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Lead price must be an integer, float or null.'); | ||
|
|
||
| (new LeadModel())->setPrice('100.0'); | ||
| } | ||
|
|
||
| public function testSetPriceWithArrayThrowsException(): void | ||
| { | ||
| $this->expectException(InvalidArgumentException::class); | ||
| $this->expectExceptionMessage('Lead price must be an integer, float or null.'); | ||
|
|
||
| (new LeadModel())->setPrice([100]); | ||
| } | ||
|
|
||
| private function assertPriceState(LeadModel $lead, ?int $legacyPrice, ?float $priceWithMinorUnits): void | ||
| { | ||
| $this->assertSame($legacyPrice, $lead->getPrice()); | ||
|
|
||
| is_null($priceWithMinorUnits) | ||
| ? $this->assertNull($lead->getPriceWithMinorUnits()) | ||
| : $this->assertSame($priceWithMinorUnits, $lead->getPriceWithMinorUnits()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нужен коммент, что в данный момент это только для kommo
а также поднять версию либы, там была константа
1.16.0 поставить