-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HasOrderCriteria condition for scenarios
remp/crm#1570
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,34 @@ | ||
<?php | ||
|
||
namespace Crm\ProductsModule\Scenarios; | ||
|
||
use Crm\ApplicationModule\Criteria\Params\BooleanParam; | ||
use Crm\ApplicationModule\Criteria\ScenariosCriteriaInterface; | ||
use Nette\Database\Table\IRow; | ||
use Nette\Database\Table\Selection; | ||
|
||
class HasOrderCriteria implements ScenariosCriteriaInterface | ||
{ | ||
public function params(): array | ||
{ | ||
return [ | ||
new BooleanParam('has_order', $this->label()), | ||
]; | ||
} | ||
|
||
public function addCondition(Selection $selection, $values, IRow $criterionItemRow): bool | ||
{ | ||
if ($values->selection) { | ||
$selection->where(':orders.id IS NOT NULL'); | ||
} else { | ||
$selection->where(':orders.id IS NULL'); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function label(): string | ||
{ | ||
return 'Has shop order'; | ||
} | ||
} |
This file contains 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,100 @@ | ||
<?php | ||
|
||
namespace Crm\ProductsModule\Tests; | ||
|
||
use Crm\ApplicationModule\Tests\DatabaseTestCase; | ||
use Crm\PaymentsModule\PaymentItem\PaymentItemContainer; | ||
use Crm\PaymentsModule\Repository\PaymentGatewaysRepository; | ||
use Crm\PaymentsModule\Repository\PaymentsRepository; | ||
use Crm\ProductsModule\Repository\OrdersRepository; | ||
use Crm\ProductsModule\Scenarios\HasOrderCriteria; | ||
use Crm\UsersModule\Auth\UserManager; | ||
use Crm\UsersModule\Repository\UsersRepository; | ||
|
||
class HasOrderCriteriaTest extends DatabaseTestCase | ||
{ | ||
/** @var PaymentsRepository */ | ||
private $paymentsRepository; | ||
|
||
/** @var OrdersRepository */ | ||
private $ordersRepository; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->paymentsRepository = $this->getRepository(PaymentsRepository::class); | ||
$this->ordersRepository = $this->getRepository(OrdersRepository::class); | ||
} | ||
|
||
protected function requiredRepositories(): array | ||
{ | ||
return [ | ||
PaymentsRepository::class, | ||
UsersRepository::class, | ||
OrdersRepository::class, | ||
PaymentGatewaysRepository::class, | ||
]; | ||
} | ||
|
||
protected function requiredSeeders(): array | ||
{ | ||
return [ | ||
]; | ||
} | ||
|
||
public function testHasOrderWithOrder(): void | ||
{ | ||
[$paymentSelection, $paymentRow] = $this->prepareData(true); | ||
|
||
$hasOrderCriteria = new HasOrderCriteria(); | ||
$hasOrderCriteria->addCondition($paymentSelection, (object)['selection' => true], $paymentRow); | ||
|
||
$this->assertNotEmpty($paymentSelection->fetch()); | ||
} | ||
|
||
public function testHasOrderWithoutOrder(): void | ||
{ | ||
[$paymentSelection, $paymentRow] = $this->prepareData(false); | ||
|
||
$hasOrderCriteria = new HasOrderCriteria(); | ||
$hasOrderCriteria->addCondition($paymentSelection, (object)['selection' => true], $paymentRow); | ||
|
||
$this->assertEmpty($paymentSelection->fetch()); | ||
} | ||
|
||
private function prepareData(bool $withOrder): array | ||
{ | ||
/** @var UserManager $userManager */ | ||
$userManager = $this->inject(UserManager::class); | ||
$userRow = $userManager->addNewUser('[email protected]'); | ||
|
||
$gatewayRepository = $this->getRepository(PaymentGatewaysRepository::class); | ||
$gatewayRow = $gatewayRepository->add('test', 'test', 10, true, true); | ||
|
||
$payment = $this->paymentsRepository->add( | ||
null, | ||
$gatewayRow, | ||
$userRow, | ||
new PaymentItemContainer(), | ||
null, | ||
0.01 // fake amount so we don't have to care about payment items | ||
); | ||
|
||
$selection = $this->paymentsRepository | ||
->getTable() | ||
->where(['payments.id' => $payment->id]); | ||
|
||
if ($withOrder) { | ||
$this->ordersRepository->add( | ||
$payment->id, | ||
null, | ||
null, | ||
null, | ||
null | ||
); | ||
} | ||
|
||
return [$selection, $payment]; | ||
} | ||
} |
File renamed without changes.
This file contains 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