An introduction and step by step example of using Behat and PHPSpec2 with Magento to develop a sample feature. Using the two Open Source extensions BehatMage and MageSpec to make the tools aware of the the Magento domain.
Client Request Hi
John, Does
Magento
have
a
feature
to
add
reviews
to
products?
If
not,
how
much
would
cost
to
implement
it? Regards, Susan e-‐Commerce
Director Acme
Furniture,
Ltd.
Let the client know Hi
Susan, We
have
deployed
the
review
funcGonality
to
the
UAT
server
for
your
acceptance.
I
trust
this
meets
your
requirement. John Project
Manager MageWoders
Development
Client response Hi
John, Thanks
for
the
update.
I
also
wanted
to
be
able
to
turn
this
feature
off
or
on
for
products.
I
don’t
seem
to
be
able
to
do
this? Can
we
have
a
conference
call
with
the
enGre
team
to
resolve
this?
This
is
URGENT
we
can’t
launch
a
system
like
this! Regards, Susan e-‐Commerce
Director Acme
Furniture,
Ltd.
Refactor email Feature:
Admin
User
can
manage
review
visibility
So
that
our
Customers
are
not
influenced
by
a
product
with
bad
review
history,
as
an
Admin
User
I
want
to
disable
reviews
of
those
specific
products
Add a scenario
Scenario:
Turn
off
reviews
per
product
Given
the
following
products
exist:
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
And
"Ottoman1"
has
existing
reviews
When
I
turn
this
off
for
"Ottoman1"
product
Then
no
review
should
be
displayed
Add another for clarity
Scenario:
Turning
off
reviews
will
not
affect
other
products
Given
the
following
products
exist:
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
|
Chair1
|
Chair
|
1
|
And
"Ottoman1"
has
existing
reviews
And
"Chair"
has
existing
reviews
When
I
turn
this
off
for
"Ottoman1"
product
Then
"Chair"
can
still
accept
reviews
Feature Feature:
Admin
User
can
manage
review
visibility
So
that
our
Customers
are
not
influenced
by
a
product
with
bad
review
history,
as
an
Admin
User
I
want
to
disable
reviews
of
those
specific
products
Scenario:
Turn
off
reviews
per
product
Given
the
following
products
exist:
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
And
"Ottoman1"
has
existing
reviews
When
I
turn
this
off
for
"Ottoman1"
product
Then
no
review
should
be
displayed
Scenario:
Turning
off
reviews
will
not
affect
other
products
Given
the
following
products
exist:
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
|
Chair1
|
Chair
|
1
|
And
"Ottoman1"
has
existing
reviews
And
"Chair"
has
existing
reviews
When
I
turn
this
off
for
"Ottoman1"
product
Then
"Chair"
can
still
accept
reviews
Initialize Behat $
bin/behat
-‐-‐init +d
features
-‐
place
your
*.feature
files
here +d
features/bootstrap
-‐
place
bootstrap
scripts
and
static
files
here +f
features/bootstrap/FeatureContext.php
-‐
place
your
feature
related
code
here
Run Behat $
bin/behat Feature:
Admin
User
can
manage
review
visibility
So
that
our
Customers
are
not
influenced
by
a
product
with
bad
review
history,
as
an
Admin
User
I
want
to
disable
reviews
of
those
specific
products
Scenario:
Turn
off
reviews
per
product
#
features/reviews/admin_user_manages_review_visibility
Given
the
following
products
exist:
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
And
"Ottoman1"
has
existing
reviews
When
I
turn
reviews
off
for
"Ottoman1"
product
Then
no
review
should
be
displayed
for
"Ottoman1"
Scenario:
Turning
off
reviews
will
not
affect
other
products
#
features/reviews/admin_user_manages_review_v
Given
the
following
products
exist:
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
|
Chair1
|
Chair
|
1
|
And
"Ottoman1"
has
existing
reviews
And
"Chair1"
has
existing
reviews
When
I
turn
reviews
off
for
"Ottoman1"
product
Then
"Chair1"
can
still
accept
reviews
Run Behat You
can
implement
step
definitions
for
undefined
steps
with
these
snippets:
/**
*
@Given
/^the
following
products
exist:$/
*/
public
function
theFollowingProductsExist(TableNode
$table)
{
throw
new
PendingException();
}
/**
*
@Given
/^"([^"]*)"
has
existing
reviews$/
*/
public
function
hasExistingReviews($arg1)
{
throw
new
PendingException();
}
/**
*
@When
/^I
turn
reviews
off
for
"([^"]*)"
product$/
*/
public
function
iTurnReviewsOffForProduct($arg1)
{
throw
new
PendingException();
}
/**
Add an Admin Context use
Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Context\BehatContext,
Behat\Behat\Exception\PendingException; use
Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode; use
MageTest\MagentoExtension\Context\MagentoContext; /**
*
Features
context.
*/ class
FeatureContext
extends
BehatContext {
public
function
__construct($paramters)
{
$this-‐>useContext('admin_user',
new
AdminUserContext($parameters));
} }
Add an Admin Context use
Behat\Behat\Exception\PendingException; use
Behat\Gherkin\Node\TableNode; use
MageTest\MagentoExtension\Context\MagentoContext; class
AdminUserContext
extends
MagentoContext {
//
paste
snippets
here }
With Magento Context $
bin/behat Feature:
Admin
User
can
manage
review
visibility
So
that
our
Customers
are
not
influenced
by
a
product
with
bad
review
history,
as
an
Admin
User
I
want
to
disable
reviews
of
those
specific
products
Scenario:
Turn
off
reviews
per
product
#
features/reviews/admin_user_manages_review_visibi
Given
the
following
products
exist:
#
AdminUserContext::theProductsExist()
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
accepts_reviews
is
not
yet
defined
as
an
attribute
of
Product
And
"Ottoman1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()
When
I
turn
reviews
off
for
"Ottoman1"
product
#
AdminUserContext::iTurnReviewsOffForProduct()
Then
no
review
should
be
displayed
for
"Ottoman1"
#
AdminUserContext::noReviewShouldBeDisplayedFor()
Scenario:
Turning
off
reviews
will
not
affect
other
products
#
features/reviews/admin_user_manages_revi
Given
the
following
products
exist:
#
AdminUserContext::theProductsExist()
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
|
Chair1
|
Chair
|
1
|
accepts_reviews
is
not
yet
defined
as
an
attribute
of
Product
And
"Ottoman1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()
And
"Chair1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()
After attribute exists $
bin/behat Feature:
Admin
User
can
manage
review
visibility
So
that
our
Customers
are
not
influenced
by
a
product
with
bad
review
history,
as
an
Admin
User
I
want
to
disable
reviews
of
those
specific
products
Scenario:
Turn
off
reviews
per
product
#
features/reviews/admin_user_manages_review_visibi
Given
the
following
products
exist:
#
AdminUserContext::theProductsExist()
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
And
"Ottoman1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()
When
I
turn
reviews
off
for
"Ottoman1"
product
#
AdminUserContext::iTurnReviewsOffForProduct()
Then
no
review
should
be
displayed
for
"Ottoman1"
#
AdminUserContext::noReviewShouldBeDisplayedFor()
Scenario:
Turning
off
reviews
will
not
affect
other
products
#
features/reviews/admin_user_manages_revi
Given
the
following
products
exist:
#
AdminUserContext::theProductsExist()
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
|
Chair1
|
Chair
|
1
|
accepts_reviews
is
not
yet
defined
as
an
attribute
of
Product
And
"Ottoman1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()
And
"Chair1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()
Repeat until it’s all green $
bin/behat Feature:
Admin
User
can
manage
review
visibility
So
that
our
Customers
are
not
influenced
by
a
product
with
bad
review
history,
as
an
Admin
User
I
want
to
disable
reviews
of
those
specific
products
Scenario:
Turn
off
reviews
per
product
#
features/reviews/admin_user_manages_review_visibi
Given
the
following
products
exist:
#
AdminUserContext::theProductsExist()
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
And
"Ottoman1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()
When
I
turn
reviews
off
for
"Ottoman1"
product
#
AdminUserContext::iTurnReviewsOffForProduct()
Then
no
review
should
be
displayed
for
"Ottoman1"
#
AdminUserContext::noReviewShouldBeDisplayedFor()
Scenario:
Turning
off
reviews
will
not
affect
other
products
#
features/reviews/admin_user_manages_revi
Given
the
following
products
exist:
#
AdminUserContext::theProductsExist()
|
sku
|
name
|
accepts_reviews
|
|
Ottoman1
|
Ottoman
|
1
|
|
Chair1
|
Chair
|
1
|
accepts_reviews
is
not
yet
defined
as
an
attribute
of
Product
And
"Ottoman1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()
And
"Chair1"
has
existing
reviews
#
AdminUserContext::hasExistingReviews()