Skip to content

Commit cb5c81c

Browse files
committed
Add some usage information
1 parent 534d94c commit cb5c81c

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,66 @@ This small PHPSpec extension allows you to test Magento 2.0 modules much more ea
1818
```
1919

2020
## Usage
21-
TBD
21+
22+
Make sure that when you write examples to specify fully qualified class name for auto-generated class.
23+
24+
```php
25+
<?php
26+
27+
namespace spec\Acme\CustomMagentoModule\Model;
28+
29+
use Magento\Catalog\Model\Product;
30+
use PhpSpec\ObjectBehavior;
31+
use Prophecy\Argument;
32+
33+
class ProductManagerSpec extends ObjectBehavior
34+
{
35+
private $productFactory;
36+
37+
function let(ProductFactory $factory) {
38+
$this->productFactory = $factory;
39+
$this->beConstructedWith($factory);
40+
}
41+
42+
function it_creates_items_via_product_factory(Product $product)
43+
{
44+
$this->productFactory->create()->willReturn($product)->shouldBeCalled();
45+
$this->someCreationLogic();
46+
}
47+
}
48+
```
49+
50+
This approach will not get you a desired result, as PHP by default looks for undefined classes within the same namespace.
51+
So instead of `Magento\Catalog\Model\ProductFactory` it will generate a class `spec\Acme\CustomMagentoModule\Model\ProductFactory`, that is definitely not a desired behavior.
52+
53+
In order to fix that make sure to specify fully qualified name in method signature or via `use` operator in the file header.
54+
55+
```php
56+
<?php
57+
58+
namespace spec\Acme\CustomMagentoModule\Model;
59+
60+
use Magento\Catalog\Model\Product;
61+
use Magento\Catalog\Model\ProductFactory; // This class will be automatically generated
62+
use PhpSpec\ObjectBehavior;
63+
use Prophecy\Argument;
64+
65+
class ProductManagerSpec extends ObjectBehavior
66+
{
67+
private $productFactory;
68+
69+
function let(ProductFactory $factory) {
70+
$this->productFactory = $factory;
71+
$this->beConstructedWith($factory);
72+
}
73+
74+
function it_creates_items_via_product_factory(Product $product)
75+
{
76+
$this->productFactory->create()->willReturn($product)->shouldBeCalled();
77+
$this->someCreationLogic();
78+
}
79+
}
80+
```
2281

2382
## Contribution
2483
Make a pull request based on develop branch

0 commit comments

Comments
 (0)