Skip to content

Commit

Permalink
Merge pull request #5086 from pjedrzejewski/admin/dashboard-prototype
Browse files Browse the repository at this point in the history
[Admin] Add single channel dashboard
  • Loading branch information
michalmarcinkowski committed May 24, 2016
2 parents d54afb2 + 2861261 commit 73d66c7
Show file tree
Hide file tree
Showing 38 changed files with 1,095 additions and 108 deletions.
41 changes: 41 additions & 0 deletions features/admin/dashboard.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@admin_dashboard
Feature: Statistics dashboard
In order to have an overview of my sales
As an Administrator
I want to see overall statistics on my admin dashboard

Background:
Given the store operates on a single channel in "France"
And the store ships everywhere for free
And the store allows paying offline
And the store has a product "Sylius T-Shirt"
And this product has "Red XL" variant priced at "€40"
And I am logged in as an administrator

@ui
Scenario: Seeing basic statistics for entire store
Given 3 customers have placed 4 orders for total of "€8566.00"
And then 2 more customers have placed 2 orders for total of "€459.00"
When I open administration dashboard
Then I should see 6 new orders
And I should see 6 new customers
And there should be total sales of "€9,025.00"
And the average order value should be "€1,504.17"

@ui
Scenario: Statistics include only completed orders
Given 4 customers have placed 4 orders for total of "€5241.00"
And 2 customers have added products to the cart for total of "€3450.00"
When I open administration dashboard
Then I should see 4 new orders
And I should see 7 new customers
And there should be total sales of "€5,241.00"
And the average order value should be "€1,310.25"

@ui
Scenario: Seeing recent orders and customers
Given 2 customers have placed 3 orders for total of "€340.00"
And 2 customers have added products to the cart for total of "€424.00"
When I open administration dashboard
Then I should see 5 customers in the list
And I should see 3 new orders in the list
112 changes: 110 additions & 2 deletions src/Sylius/Behat/Context/Setup/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Sylius\Component\Payment\Model\PaymentInterface;
use Sylius\Component\Payment\Model\PaymentMethodInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\User\Model\CustomerInterface;
use Sylius\Component\User\Model\UserInterface;

Expand Down Expand Up @@ -74,6 +75,16 @@ final class OrderContext implements Context
*/
private $itemQuantityModifier;

/**
* @var FactoryInterface
*/
private $customerFactory;

/**
* @var RepositoryInterface
*/
private $customerRepository;

/**
* @var OrderRecalculatorInterface
*/
Expand All @@ -92,7 +103,8 @@ final class OrderContext implements Context
* @param PaymentFactoryInterface $paymentFactory
* @param FactoryInterface $orderItemFactory
* @param OrderItemQuantityModifierInterface $itemQuantityModifier
* @param SharedStorageInterface $sharedStorage
* @param FactoryInterface $customerFactory
* @param RepositoryInterface $customerRepository
* @param OrderRecalculatorInterface $orderRecalculator
* @param ObjectManager $objectManager
*/
Expand All @@ -104,6 +116,8 @@ public function __construct(
PaymentFactoryInterface $paymentFactory,
FactoryInterface $orderItemFactory,
OrderItemQuantityModifierInterface $itemQuantityModifier,
FactoryInterface $customerFactory,
RepositoryInterface $customerRepository,
OrderRecalculatorInterface $orderRecalculator,
ObjectManager $objectManager
) {
Expand All @@ -114,6 +128,8 @@ public function __construct(
$this->paymentFactory = $paymentFactory;
$this->orderItemFactory = $orderItemFactory;
$this->itemQuantityModifier = $itemQuantityModifier;
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
$this->orderRecalculator = $orderRecalculator;
$this->objectManager = $objectManager;
}
Expand Down Expand Up @@ -273,6 +289,65 @@ public function iHaveAlreadyPlacedOrderNthTimes(UserInterface $user, $numberOfOr
}
}

/**
* @Given :numberOfCustomers customers have added products to the cart for total of :total
*/
public function customersHaveAddedProductsToTheCartForTotalOf($numberOfCustomers, $total)
{
$customers = $this->generateCustomers($numberOfCustomers);

$sampleProductVariant = $this->sharedStorage->get('variant');
$total = $this->getPriceFromString($total);

for ($i = 0; $i < $numberOfCustomers; $i++) {
$order = $this->createOrder($customers[rand(0, $numberOfCustomers - 1)]);
$order->setCompletedAt(null);

$price = $i === ($numberOfCustomers - 1) ? $total : rand(1, $total);
$total -= $price;

$item = $this->orderItemFactory->createNew();
$item->setVariant($sampleProductVariant);
$item->setUnitPrice($price);

$this->itemQuantityModifier->modify($item, 1);

$order->addItem($item);

$this->orderRepository->add($order);
}
}

/**
* @Given :numberOfCustomers customers have placed :numberOfOrders orders for total of :total
* @Given then :numberOfCustomers more customers have placed :numberOfOrders orders for total of :total
*/
public function customersHavePlacedOrdersForTotalOf($numberOfCustomers, $numberOfOrders, $total)
{
$customers = $this->generateCustomers($numberOfCustomers);
$sampleProductVariant = $this->sharedStorage->get('variant');
$total = $this->getPriceFromString($total);

for ($i = 0; $i < $numberOfOrders; $i++) {
$order = $this->createOrder($customers[rand(0, $numberOfCustomers - 1)], '#'.uniqid());
$order->setPaymentState(PaymentInterface::STATE_COMPLETED);
$order->setCompletedAt(new \DateTime());

$price = $i === ($numberOfOrders - 1) ? $total : rand(1, $total);
$total -= $price;

$item = $this->orderItemFactory->createNew();
$item->setVariant($sampleProductVariant);
$item->setUnitPrice($price);

$this->itemQuantityModifier->modify($item, 1);

$order->addItem($item);

$this->orderRepository->add($order);
}
}

/**
* @param ProductVariantInterface $productVariant
* @param int $price
Expand Down Expand Up @@ -308,7 +383,7 @@ private function addProductVariantToOrder(ProductVariantInterface $productVarian
*/
private function createOrder(
CustomerInterface $customer,
$number,
$number = null,
ChannelInterface $channel = null,
CurrencyInterface $currency = null
) {
Expand All @@ -322,4 +397,37 @@ private function createOrder(

return $order;
}

/**
* @param $count
*
* @return CustomerInterface[]
*/
private function generateCustomers($count)
{
$customers = [];

for ($i = 0; $i < $count; $i++) {
$customer = $this->customerFactory->createNew();
$customer->setEmail(sprintf('john%s@doe.com', uniqid()));
$customer->setFirstname('John');
$customer->setLastname('Doe'.$i);

$customers[] = $customer;

$this->customerRepository->add($customer);
}

return $customers;
}

/**
* @param string $price
*
* @return int
*/
private function getPriceFromString($price)
{
return (int) round((str_replace(['€', '£', '$'], '', $price) * 100), 2);
}
}
91 changes: 91 additions & 0 deletions src/Sylius/Behat/Context/Ui/Admin/DashboardContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Behat\Context\Ui\Admin;

use Behat\Behat\Context\Context;
use Sylius\Behat\Page\Admin\DashboardPageInterface;
use Webmozart\Assert\Assert;

/**
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
final class DashboardContext implements Context
{
/**
* @var DashboardPageInterface
*/
private $dashboardPage;

/**
* @param DashboardPageInterface $dashboardPage
*/
public function __construct(DashboardPageInterface $dashboardPage)
{
$this->dashboardPage = $dashboardPage;
}

/**
* @When I open administration dashboard
*/
public function iOpenAdministrationDashboard()
{
$this->dashboardPage->open();
}

/**
* @Then I should see :number new orders
*/
public function iShouldSeeNewOrders($number)
{
Assert::same($this->dashboardPage->getNumberOfNewOrders(), $number);
}

/**
* @Then I should see :number new customers
*/
public function iShouldSeeNewCustomers($number)
{
Assert::same($this->dashboardPage->getNumberOfNewCustomers(), $number);
}

/**
* @Then there should be total sales of :total
*/
public function thereShouldBeTotalSalesOf($total)
{
Assert::same($this->dashboardPage->getTotalSales(), $total);
}

/**
* @Then the average order value should be :value
*/
public function myAverageOrderValueShouldBe($value)
{
Assert::same($this->dashboardPage->getAverageOrderValue(), $value);
}

/**
* @Then I should see :number customers in the list
*/
public function iShouldSeeCustomersInTheList2($number)
{
Assert::same($this->dashboardPage->getNumberOfNewCustomersInTheList(), $number);
}

/**
* @Then I should see :number new orders in the list
*/
public function iShouldSeeNewOrdersInTheList($number)
{
Assert::same($this->dashboardPage->getNumberOfNewOrdersInTheList(), $number);
}
}
Loading

0 comments on commit 73d66c7

Please sign in to comment.