Upgrade to Pro — share decks privately, control downloads, hide ads and more …

BDD with Magento

Avatar for Debo Debo
July 18, 2013

BDD with Magento

BehatMage & MageSpec: how the projects have evolved into a real solution since the hackathon.

Avatar for Debo

Debo

July 18, 2013
Tweet

More Decks by Debo

Other Decks in Technology

Transcript

  1. About who Marcello Duarte Head of Training @ Session Digital

    @_md Alistair Stead Technical Assurance Manager @ Session Digital @alistairstead Friday, 20 September 13
  2. About who Šarūnas Valaškevičius Technical Team Leader @ Session Digital

    @swoq Daniel Kidanemariam Software Engineer @ Session Digital @ithinkheisnotontwitter Friday, 20 September 13
  3. • New feature requests • Unclear requirements • Not intuitive

    to Magento domain • Layers of communication add confusion The problem Friday, 20 September 13
  4. 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. Friday, 20 September 13
  5. Intial reaction Hi  Susan, It’s  out  of  the  box  :)

     Should  we  enable  this  funcGon  in  the  next   release? John Project  Manager MageWoders  Development Friday, 20 September 13
  6. • Real simple requirement! • I will get that done

    in no time! Developer thinks... Friday, 20 September 13
  7. Client Confirms Hi  John, That’s  great!  Yes  please,  add  that

     to  the  next  release. Regards, Susan e-­‐Commerce  Director Acme  Furniture,  Ltd. Friday, 20 September 13
  8. 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 Friday, 20 September 13
  9. 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. Friday, 20 September 13
  10. Waterfall process [Royce 1970] 9 months 3 months 2 months

    12 months ... Requirement Analysis Design Code Test Friday, 20 September 13
  11. • Actually talk to the customer • “Automate” the conversation

    • Deliver often and refine Agile 2001 Friday, 20 September 13
  12. Test === Get clear Extreme Programming === Sensible Programming Good

    solution, poor nameing Friday, 20 September 13
  13. • Clearing up some language issues • Make the feedback

    loop even shorter BDD Friday, 20 September 13
  14. Create narative Why? Feature name, Actor, behavior, benefit What do

    you mean? Scenarios (made of steps) Friday, 20 September 13
  15. 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 Friday, 20 September 13
  16. 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 Friday, 20 September 13
  17. 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 Friday, 20 September 13
  18. 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 Friday, 20 September 13
  19. Feature Scenario:  Some  description  of  the  scenario      

     Given  [some  context]        When  [some  event  occurs]        Then  [outcome] Scenario:  Some  description  of  the  scenario        Given  [some  context]                And  [more  context]        When  [some  event  occurs]                And  [another  event  occurs]        Then  [outcome]                And  [another  outcome]                But  [another  outcome] Friday, 20 September 13
  20. composer.json {        "require-­‐dev":  {      

             "magetest/magento-­‐behat-­‐extension":  "dev-­‐develop",        },        "config":  {                "bin-­‐dir":  "bin"        },        "autoload":  {                "psr-­‐0":  {                        "":  [                                "public/app",                                "public/app/code/local",                                "public/app/code/community",                                "public/app/code/core",                                "public/lib"                        ]                }        },        "minimum-­‐stability":  "dev" } Friday, 20 September 13
  21. 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 Friday, 20 September 13
  22. 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 Friday, 20 September 13
  23. 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();        }        /** Friday, 20 September 13
  24. behat.yml default:        extensions:        

           MageTest\MagentoExtension\Extension:                        base_url:  "http://test.magento.loc" Friday, 20 September 13
  25. Add an Admin Context <?php 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));        } } Friday, 20 September 13
  26. Add an Admin Context <?php use  Behat\Behat\Exception\PendingException; use  Behat\Gherkin\Node\TableNode; use

     MageTest\MagentoExtension\Context\MagentoContext; class  AdminUserContext  extends  MagentoContext {          //  paste  snippets  here } Friday, 20 September 13
  27. 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()        When  I  turn  reviews  off  for  "Ottoman1"  product                          #  AdminUserContext::iTurnReviewsOffForProd Friday, 20 September 13
  28. Run Behat You  can  implement  step  definitions  for  undefined  steps

     with  these  snippets:        /**          *  @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();        }        /**          *  @Then  /^no  review  should  be  displayed  for  "([^"]*)"$/          */        public  function  noReviewShouldBeDisplayedFor($arg1)        {                throw  new  PendingException();        }        /** Friday, 20 September 13
  29. Describing code Object Expectation Matcher $this        

                     -­‐>should                    Be()                                    -­‐>shouldNot              Return() Friday, 20 September 13
  30. composer.json {        "require-­‐dev":  {      

             "magetest/magento-­‐behat-­‐extension":  "*",                "magetest/magento-­‐phpspec-­‐extension":  "*"        },        "config":  {                "bin-­‐dir":  "bin"        },        "autoload":  {                "psr-­‐0":  {                        "":  [                                "public/app",                                "public/app/code/local",                                "public/app/code/community",                                "public/app/code/core",                                "public/lib"                        ]                }        },        "minimum-­‐stability":  "dev" } Friday, 20 September 13
  31. phpspec.yml extensions:  [MageTest\PHPSpec2\MagentoExtension\Extension] magento_root:  "./public" mage_locator:        spec_prefix:

     "spec"        src_path:  "public/app/code"        spec_path:  "spec/public/app/code" Friday, 20 September 13
  32. Describe <?php namespace  spec; use  PhpSpec\ObjectBehavior; use  Prophecy\Argument; class  Magespec_Reviews_Model_ProductSpec

     extends  ObjectBehavior {        function  it_is_initializable()        {                $this-­‐>shouldHaveType('Magespec_Reviews_Model_Product');        } } Friday, 20 September 13
  33. Run PHPSpec MageSpec_Reviews_Model_Product    10    !  it  is  initializable

               class  MageSpec_Reviews_Model_Product  does  not  exists. -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐100%-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  1 1  example  (1  broken) 11ms Do  you  want  me  to  create  `MageSpec_Reviews_Model_Product`  for  you?   [Y/n] Friday, 20 September 13