From c85fe24d9665376ace7888475d4ee82052d03e39 Mon Sep 17 00:00:00 2001 From: Benjamin Perche Date: Thu, 31 Jul 2014 13:52:07 +0200 Subject: [PATCH] =?UTF-8?q?Add=20tests=20on=20OrderExport=20=09modifi?= =?UTF-8?q?=C3=A9:=20=20=20=20=20=20=20=20=20core/lib/Thelia/ImportExport/?= =?UTF-8?q?Export/Type/OrderExport.php=20=09nouveau=20fichier:=20core/lib/?= =?UTF-8?q?Thelia/Tests/ImportExport/Export/OrderExportTest.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ImportExport/Export/Type/OrderExport.php | 151 ++++++++++++++++-- .../ImportExport/Export/OrderExportTest.php | 149 +++++++++++++++++ 2 files changed, 283 insertions(+), 17 deletions(-) create mode 100644 core/lib/Thelia/Tests/ImportExport/Export/OrderExportTest.php diff --git a/core/lib/Thelia/ImportExport/Export/Type/OrderExport.php b/core/lib/Thelia/ImportExport/Export/Type/OrderExport.php index b7762b441..8ba86d74a 100644 --- a/core/lib/Thelia/ImportExport/Export/Type/OrderExport.php +++ b/core/lib/Thelia/ImportExport/Export/Type/OrderExport.php @@ -28,6 +28,7 @@ use Thelia\Model\Map\OrderCouponTableMap; use Thelia\Model\Map\OrderProductTableMap; use Thelia\Model\Map\OrderProductTaxTableMap; use Thelia\Model\Map\OrderStatusI18nTableMap; +use Thelia\Model\Map\OrderStatusTableMap; use Thelia\Model\Map\OrderTableMap; use Thelia\Model\OrderQuery; @@ -61,11 +62,7 @@ class OrderExport extends ExportHandler ); } - /** - * @param Lang $lang - * @return ModelCriteria|array|BaseLoop - */ - public function buildDataSet(Lang $lang) + protected function getQuery(Lang $lang) { $locale = $lang->getLocale(); @@ -85,15 +82,16 @@ class OrderExport extends ExportHandler "SUM(".OrderProductTaxTableMap::AMOUNT.")". ")" ) - ->addAsColumn("tax_TITLE", OrderProductTaxTableMap::TITLE) + ->addAsColumn("tax_TITLE", OrderProductTableMap::TAX_RULE_TITLE) ->endUse() ->addAsColumn("product_TITLE", OrderProductTableMap::TITLE) ->addAsColumn( "product_PRICE", "IF(".OrderProductTableMap::WAS_IN_PROMO.",". OrderProductTableMap::PROMO_PRICE .",". - OrderProductTableMap::PRICE .")" - ) + OrderProductTableMap::PRICE . + ")" + ) ->addAsColumn("product_QUANTITY", OrderProductTableMap::QUANTITY) ->addAsColumn("product_WAS_IN_PROMO", OrderProductTableMap::WAS_IN_PROMO) ->groupById() @@ -120,16 +118,16 @@ class OrderExport extends ExportHandler ->useCountryI18nQuery("delivery_address_country_i18n_join") ->addAsColumn("delivery_address_country_TITLE", "`delivery_address_country_i18n_join`.TITLE") ->endUse() + ->addAsColumn("delivery_address_COMPANY", "`delivery_address_join`.COMPANY") + ->addAsColumn("delivery_address_FIRSTNAME", "`delivery_address_join`.FIRSTNAME") + ->addAsColumn("delivery_address_LASTNAME", "`delivery_address_join`.LASTNAME") + ->addAsColumn("delivery_address_ADDRESS1", "`delivery_address_join`.ADDRESS1") + ->addAsColumn("delivery_address_ADDRESS2", "`delivery_address_join`.ADDRESS2") + ->addAsColumn("delivery_address_ADDRESS3", "`delivery_address_join`.ADDRESS3") + ->addAsColumn("delivery_address_ZIPCODE", "`delivery_address_join`.ZIPCODE") + ->addAsColumn("delivery_address_CITY", "`delivery_address_join`.CITY") + ->addAsColumn("delivery_address_PHONE", "`delivery_address_join`.PHONE") ->endUse() - ->addAsColumn("delivery_address_COMPANY", "`delivery_address_join`.COMPANY") - ->addAsColumn("delivery_address_FIRSTNAME", "`delivery_address_join`.FIRSTNAME") - ->addAsColumn("delivery_address_LASTNAME", "`delivery_address_join`.LASTNAME") - ->addAsColumn("delivery_address_ADDRESS1", "`delivery_address_join`.ADDRESS1") - ->addAsColumn("delivery_address_ADDRESS2", "`delivery_address_join`.ADDRESS2") - ->addAsColumn("delivery_address_ADDRESS3", "`delivery_address_join`.ADDRESS3") - ->addAsColumn("delivery_address_ZIPCODE", "`delivery_address_join`.ZIPCODE") - ->addAsColumn("delivery_address_CITY", "`delivery_address_join`.CITY") - ->addAsColumn("delivery_address_PHONE", "`delivery_address_join`.PHONE") ->endUse() ->useOrderAddressRelatedByInvoiceOrderAddressIdQuery("invoice_address_join") ->useCustomerTitleQuery("invoice_address_customer_title_join") @@ -175,6 +173,7 @@ class OrderExport extends ExportHandler // total ttc +postage "payment_module_TITLE", OrderTableMap::INVOICE_REF, + OrderTableMap::DELIVERY_REF, "delivery_module_TITLE", "delivery_address_TITLE", "delivery_address_COMPANY", @@ -239,6 +238,27 @@ class OrderExport extends ExportHandler $locale ); + $this->addI18nCondition( + $query, + OrderStatusI18nTableMap::TABLE_NAME, + OrderStatusI18nTableMap::ID, + OrderStatusTableMap::ID, + OrderStatusI18nTableMap::LOCALE, + $locale + ); + + return $query; + } + + /** + * @param Lang $lang + * @return ModelCriteria|array|BaseLoop + */ + public function buildDataSet(Lang $lang) + { + + $query = $this->getQuery($lang); + $dataSet = $query ->find() ->toArray() @@ -315,4 +335,101 @@ class OrderExport extends ExportHandler return $dataSet; } + protected function getAliases() + { + return [ + OrderTableMap::REF => "ref", + "customer_REF" => "customer_ref", + "product_TITLE" => "product_title", + "product_PRICE" => "price", + "product_TAX" => "tax_amount", + "tax_TITLE" => "tax_title", + "order_TOTAL_TTC" => "total_including_taxes", + "product_QUANTITY" => "quantity", + "product_WAS_IN_PROMO" => "was_in_promo", + "order_TOTAL_WITH_DISCOUNT" => "total_with_discount", + OrderTableMap::DISCOUNT => "discount", + "coupon_COUPONS" => "coupons", + "order_TOTAL_WITH_DISCOUNT_AND_POSTAGE" => "total_discount_and_postage", + OrderTableMap::POSTAGE => "postage", + "product_TAXED_PRICE" => "taxed_price", + "payment_module_TITLE" => "payment_module", + OrderTableMap::INVOICE_REF => "invoice_ref", + OrderTableMap::DELIVERY_REF => "delivery_ref", + "delivery_module_TITLE" => "delivery_module", + "delivery_address_TITLE" => "delivery_title", + "delivery_address_COMPANY" => "delivery_company", + "delivery_address_FIRSTNAME" => "delivery_first_name", + "delivery_address_LASTNAME" => "delivery_last_name", + "delivery_address_ADDRESS1" => "delivery_address1", + "delivery_address_ADDRESS2" => "delivery_address2", + "delivery_address_ADDRESS3" => "delivery_address3", + "delivery_address_ZIPCODE" => "delivery_zip_code", + "delivery_address_CITY" => "delivery_city", + "delivery_address_country_TITLE" => "delivery_country", + "delivery_address_PHONE" => "delivery_phone", + "invoice_address_TITLE" => "invoice_title", + "invoice_address_COMPANY" => "invoice_company", + "invoice_address_FIRSTNAME" => "invoice_first_name", + "invoice_address_LASTNAME" => "invoice_last_name", + "invoice_address_ADDRESS1" => "invoice_address1", + "invoice_address_ADDRESS2" => "invoice_address2", + "invoice_address_ADDRESS3" => "invoice_address3", + "invoice_address_ZIPCODE" => "invoice_zip_code", + "invoice_address_CITY" => "invoice_city", + "invoice_address_country_TITLE" => "invoice_country", + "invoice_address_PHONE" => "invoice_phone", + "order_status_TITLE" => "status", + "currency_CODE" => "currency", + ]; + } + + public function getOrder() + { + return [ + "ref", + "customer_ref", + "discount", + "coupons", + "postage", + "total_including_taxes", + "total_with_discount", + "total_discount_and_postage", + "delivery_module", + "delivery_ref", + "payment_module", + "invoice_ref", + "status", + "delivery_title", + "delivery_company", + "delivery_first_name", + "delivery_last_name", + "delivery_address1", + "delivery_address2", + "delivery_address3", + "delivery_zip_code", + "delivery_city", + "delivery_country", + "delivery_phone", + "invoice_title", + "invoice_company", + "invoice_first_name", + "invoice_last_name", + "invoice_address1", + "invoice_address2", + "invoice_address3", + "invoice_zip_code", + "invoice_city", + "invoice_country", + "invoice_phone", + "product_title", + "price", + "taxed_price", + "currency", + "was_in_promo", + "quantity", + "tax_amount", + "tax_title", + ]; + } } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/ImportExport/Export/OrderExportTest.php b/core/lib/Thelia/Tests/ImportExport/Export/OrderExportTest.php new file mode 100644 index 000000000..921f5d692 --- /dev/null +++ b/core/lib/Thelia/Tests/ImportExport/Export/OrderExportTest.php @@ -0,0 +1,149 @@ + + */ +class OrderExportTest extends \PHPUnit_Framework_TestCase +{ + public function testQuery() + { + $container = new Container(); + new Translator($container); + + $handler = new OrderExport($container); + + $lang = Lang::getDefaultLanguage(); + $locale = $lang->getLocale(); + + $data = $handler->buildData($lang)->getData(); + + $ordersProductQuery = OrderProductQuery::create(); + $orders = OrderQuery::create()->find(); + + $count = $ordersProductQuery->count(); + $this->assertEquals(count($data), $count); + + + /** + * For the rest of the test, 50 orders are much enough + */ + if ($count > 50) { + $count = 50; + } + + $current = 0; + + for ($i = 0; $i < $count; ++$current) { + $row = $data[$i]; + /** @var \Thelia\Model\Order $order */ + $order = $orders->get($current); + + $this->assertEquals( + $ref = $order->getRef(), $row["ref"] + ); + + $this->assertEquals( + $order->getCustomer()->getRef(), $row["customer_ref"] + ); + + $coupons = OrderCouponQuery::create() + ->filterByOrder($order) + ->select(OrderCouponTableMap::TITLE) + ->find() + ->toArray() + ; + $coupons = implode(",", $coupons); + + $this->assertTrue( + empty($coupons) ? + empty($row["coupons"]): + $coupons === $row["coupons"] + ); + + $this->assertEquals($order->getCurrency()->getCode(), $row["currency"]); + $this->assertEquals($order->getCustomer()->getRef(), $row["customer_ref"]); + $this->assertEquals($order->getOrderStatus()->setLocale($locale)->getTitle(), $row["status"]); + + $this->assertEquals($order->getDeliveryRef(), $row["delivery_ref"]); + $this->assertEquals($order->getModuleRelatedByDeliveryModuleId()->getCode(), $row["delivery_module"]); + $this->assertEquals($order->getInvoiceRef(), $row["invoice_ref"]); + $this->assertEquals($order->getModuleRelatedByPaymentModuleId()->getCode(), $row["payment_module"]); + + $this->assertEquals($order->getTotalAmount($tax, false, false), $row["total_including_taxes"]); + $this->assertEquals($order->getTotalAmount($tax, false, true), $row["total_with_discount"]); + $this->assertEquals($order->getTotalAmount($tax, true, true), $row["total_discount_and_postage"]); + + + $invoiceAddress = $order->getOrderAddressRelatedByInvoiceOrderAddressId(); + $deliveryAddress = $order->getOrderAddressRelatedByDeliveryOrderAddressId(); + + $addresses = [ + "delivery" => $deliveryAddress, + "invoice" => $invoiceAddress + ]; + + /** @var \Thelia\Model\OrderAddress $address */ + foreach ($addresses as $prefix => $address) { + $this->assertEquals($address->getCustomerTitle()->setLocale($locale)->getShort(), $row[$prefix."_title"]); + $this->assertEquals($address->getAddress1(), $row[$prefix."_address1"]); + $this->assertEquals($address->getAddress2(), $row[$prefix."_address2"]); + $this->assertEquals($address->getAddress3(), $row[$prefix."_address3"]); + $this->assertEquals($address->getCity(), $row[$prefix."_city"]); + $this->assertEquals($address->getZipcode(), $row[$prefix."_zip_code"]); + $this->assertEquals($address->getCompany(), $row[$prefix."_company"]); + $this->assertEquals($address->getFirstname(), $row[$prefix."_first_name"]); + $this->assertEquals($address->getLastname(), $row[$prefix."_last_name"]); + $this->assertEquals($address->getCountry()->setLocale($locale)->getTitle(), $row[$prefix."_country"]); + $this->assertEquals($address->getPhone(), $row[$prefix."_phone"]); + } + + while ($data[$i]["ref"] === $ref) { + /** @var \Thelia\Model\OrderProduct $product */ + $product = OrderProductQuery::create() + ->filterByTitle($data[$i]["product_title"]) + ->filterByTaxRuleTitle($data[$i]["tax_title"]) + ->filterByWasInPromo($data[$i]["was_in_promo"]) + ->_if((bool)((int)$data[$i]["was_in_promo"])) + ->filterByPromoPrice($data[$i]["price"]) + ->_else() + ->filterByPrice($data[$i]["price"]) + ->_endif() + ->filterByQuantity($data[$i]["quantity"]) + ->findOne() + ; + $this->assertNotNull($product); + + $sum = 0; + foreach ($product->getOrderProductTaxes() as $tax) { + $sum += $product->getWasInPromo() ? $tax->getPromoAmount() : $tax->getAmount(); + } + + $this->assertEquals($sum, $data[$i++]["tax_amount"]); + } + } + } +} \ No newline at end of file