Skip to content

Commit

Permalink
Change owning side of relationship, add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsegura committed Feb 6, 2025
1 parent 3d0a9e5 commit e3ec9aa
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 26 deletions.
43 changes: 43 additions & 0 deletions app/DoctrineMigrations/Version20250206150857.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250206150857 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// TODO Move sylius_order_loopeat_credentials.order_id to sylius_order.loopeat_credentials_id

$this->addSql('ALTER TABLE sylius_order ADD loopeat_credentials_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE sylius_order ADD CONSTRAINT FK_6196A1F9BD145500 FOREIGN KEY (loopeat_credentials_id) REFERENCES sylius_order_loopeat_credentials (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE UNIQUE INDEX UNIQ_6196A1F9BD145500 ON sylius_order (loopeat_credentials_id)');
$this->addSql('ALTER TABLE sylius_order_loopeat_credentials DROP CONSTRAINT fk_dec04c298d9f6d38');
$this->addSql('DROP INDEX uniq_dec04c298d9f6d38');
$this->addSql('ALTER TABLE sylius_order_loopeat_credentials DROP order_id');
}

public function down(Schema $schema): void
{
// TODO Move sylius_order.loopeat_credentials_id to sylius_order_loopeat_credentials.order_id

$this->addSql('ALTER TABLE sylius_order_loopeat_credentials ADD order_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE sylius_order_loopeat_credentials ADD CONSTRAINT fk_dec04c298d9f6d38 FOREIGN KEY (order_id) REFERENCES sylius_order (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE UNIQUE INDEX uniq_dec04c298d9f6d38 ON sylius_order_loopeat_credentials (order_id)');
$this->addSql('ALTER TABLE sylius_order DROP CONSTRAINT FK_6196A1F9BD145500');
$this->addSql('DROP INDEX UNIQ_6196A1F9BD145500');
$this->addSql('ALTER TABLE sylius_order DROP loopeat_credentials_id');
}
}
65 changes: 65 additions & 0 deletions features/carts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2362,3 +2362,68 @@ Feature: Carts
"redirectUrl":"@string@||@null@"
}
"""

@debug
Scenario: Assign cart to guest customer moves Loopeat credentials
Given the fixtures files are loaded:
| sylius_channels.yml |
| payment_methods.yml |
| products.yml |
| restaurants.yml |
And the setting "guest_checkout_enabled" has value "1"
And the restaurant with id "1" has products:
| code |
| PIZZA |
| HAMBURGER |
When I add "Content-Type" header equal to "application/ld+json"
And I add "Accept" header equal to "application/ld+json"
And I send a "POST" request to "/api/carts/session" with body:
"""
{
"restaurant": "/api/restaurants/1"
}
"""
Then print last response
Then the response status code should be 200
And the response should be in JSON
And the JSON should match:
"""
{
"token":@string@,
"cart":{
"@context":"/api/contexts/Order",
"@id":"/api/orders/1",
"@type":"http://schema.org/Order",
"customer":null,
"restaurant":"/api/restaurants/1",
"@*@":"@*@"
}
}
"""
Given the order with id "1" has Loopeat credentials
Given the client is authenticated with last response token
When I add "Content-Type" header equal to "application/ld+json"
And I add "Accept" header equal to "application/ld+json"
And I send an authenticated "PUT" request to "/api/orders/1/assign" with body:
"""
{
"guest": true,
"email": "[email protected]",
"telephone": "+33193166989"
}
"""
Then print last response
Then the response status code should be 200
And the response should be in JSON
And the JSON should match:
"""
{
"@context":"/api/contexts/Order",
"@id":"/api/orders/1",
"@type":"http://schema.org/Order",
"customer":"/api/customers/1",
"restaurant":"/api/restaurants/1",
"@*@":"@*@"
}
"""
And the customer with id "1" should have Loopeat credentials
13 changes: 0 additions & 13 deletions src/Entity/LoopEat/OrderCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,10 @@ class OrderCredentials
use Timestampable;

private $id;
private $order;

public function getId()
{
return $this->id;
}

public function getOrder()
{
return $this->order;
}

public function setOrder($order)
{
$this->order = $order;

return $this;
}
}

6 changes: 0 additions & 6 deletions src/Entity/Sylius/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -1759,9 +1759,7 @@ public function getLoopeatAccessToken()
public function setLoopeatAccessToken($accessToken)
{
if (null === $this->loopeatCredentials) {

$this->loopeatCredentials = new OrderCredentials();
$this->loopeatCredentials->setOrder($this);
}

$this->loopeatCredentials->setLoopeatAccessToken($accessToken);
Expand All @@ -1780,9 +1778,7 @@ public function getLoopeatRefreshToken()
public function setLoopeatRefreshToken($refreshToken)
{
if (null === $this->loopeatCredentials) {

$this->loopeatCredentials = new OrderCredentials();
$this->loopeatCredentials->setOrder($this);
}

$this->loopeatCredentials->setLoopeatRefreshToken($refreshToken);
Expand All @@ -1796,11 +1792,9 @@ public function hasLoopEatCredentials(): bool
public function clearLoopEatCredentials()
{
if (null === $this->loopeatCredentials) {

return;
}

$this->loopeatCredentials->setOrder(null);
$this->loopeatCredentials = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
<field name="updatedAt" type="datetime" column="updated_at">
<gedmo:timestampable on="update"/>
</field>
<one-to-one field="order" target-entity="Sylius\Component\Order\Model\OrderInterface" inversed-by="loopeatCredentials">
<cascade>
<cascade-persist/>
</cascade>
<join-column name="order_id" referenced-column-name="id" />
</one-to-one>
</entity>
</doctrine-mapping>

2 changes: 1 addition & 1 deletion src/Resources/config/doctrine/Sylius.Order.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<cascade-all/>
</cascade>
</one-to-one>
<one-to-one field="loopeatCredentials" target-entity="AppBundle\Entity\LoopEat\OrderCredentials" mapped-by="order" orphan-removal="true">
<one-to-one field="loopeatCredentials" target-entity="AppBundle\Entity\LoopEat\OrderCredentials" orphan-removal="true">
<cascade>
<cascade-all/>
</cascade>
Expand Down
24 changes: 24 additions & 0 deletions tests/Behat/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use AppBundle\Entity\Urbantz\Hub as UrbantzHub;
use AppBundle\Service\SettingsManager;
use AppBundle\Sylius\Order\OrderInterface;
use AppBundle\Entity\Sylius\Customer;
use AppBundle\Entity\Sylius\Order;
use AppBundle\Entity\Sylius\Product;
use AppBundle\Entity\User;
use AppBundle\Entity\Zone;
Expand Down Expand Up @@ -1299,4 +1301,26 @@ public function theStoreWithNameHasADefaultCourierWithUsername($storeName, $user

$this->doctrine->getManagerForClass(Store::class)->flush();
}

/**
* @Given the order with id :orderId has Loopeat credentials
*/
public function theOrderWithIdHasLoopeatCredentials($orderId)
{
$order = $this->doctrine->getRepository(Order::class)->find($orderId);
$order->setLoopeatAccessToken(base64_encode(random_bytes(16)));
$order->setLoopeatRefreshToken(base64_encode(random_bytes(16)));

$this->doctrine->getManagerForClass(Order::class)->flush();
}

/**
* @Then the customer with id :customerId should have Loopeat credentials
*/
public function theCustomerWithIdShouldHaveLoopeatCredentials($customerId)
{
$customer = $this->doctrine->getRepository(Customer::class)->find($customerId);

Assert::assertTrue($customer->hasLoopEatCredentials());
}
}

0 comments on commit e3ec9aa

Please sign in to comment.