-
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
Open
rasa04
wants to merge
1
commit into
amocrm:master
Choose a base branch
from
rasa04:cents
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| <?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()); | ||
| } | ||
|
|
||
| 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.
нужно добавить проверку на типы внутри метода, если убираем тип