Skip to content

Instantly share code, notes, and snippets.

@shanethehat
Created February 12, 2014 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanethehat/8951398 to your computer and use it in GitHub Desktop.
Save shanethehat/8951398 to your computer and use it in GitHub Desktop.
The var_dump on line 69 of switch.php returns an instance of Prophecy\Prophecy\MethodProphecy instead of the value '2'. This seems to be caused by the way phpspec unwraps the iterator created in spec.php line 26.
class Foo_Page_Block_Switch extends Mage_Page_Block_Switch
{
/**
* Application
* @var Mage_Core_Model_App
*/
private $_app;
/**
* Staging Collection
* @var Enterprise_Staging_Model_Mysql4_Staging_Collection
*/
private $_stagingCollection;
/**
* Assigning Mage::app as application by default
*/
public function __construct($services = array())
{
if (array_key_exists('app', $services)) {
$this->_app = $services['app'];
}
if (array_key_exists('staging_collection', $services)) {
$this->_stagingCollection = $services['staging_collection'];
}
if (!$this->_app instanceof Mage_Core_Model_App) {
$this->_app = Mage::app();
}
if (!$this->_stagingCollection instanceof Enterprise_Staging_Model_Mysql4_Staging_Collection) {
$this->_stagingCollection = Mage::getModel('enterprise_staging/staging')->getCollection();
}
parent::__construct();
}
/**
* Return all stores from all websites (except staging stores)
* @return array
*/
public function getAllStores()
{
if (!$this->hasData('allStores')) {
$allStores = array();
$stagingWebsites = $this->getStagingWebsiteIds();
foreach ($this->_app->getWebsites() as $website) {
if (array_search($website->getId(), $stagingWebsites) === false) {
$websiteStores = $website->getStores();
if (is_array($websiteStores)) {
$allStores += $website->getStores();
}
}
}
$this->setData('allStores', $allStores);
}
$allStores = (array) $this->getData('allStores');
return $allStores;
}
/**
* Returns list of staging website ids
* @return array
*/
protected function getStagingWebsiteIds()
{
$stagingWebsiteIds = array();
foreach ($this->_stagingCollection as $stagingItem) {
var_dump($stagingItem->getStagingWebsiteId());
$stagingWebsiteIds[] = $stagingItem->getStagingWebsiteId();
}
return $stagingWebsiteIds;
}
}
class Foo_Page_Block_SwitchSpec extends ObjectBehavior
{
function let(
\Mage_Core_Model_App $app, \Enterprise_Staging_Model_Mysql4_Staging_Collection $stagingCollection,
\Mage_Core_Model_Website $website1, \Mage_Core_Model_Website $website2, \Mage_Core_Model_Store $store1,
\Mage_Core_Model_Store $store2, \Mage_Core_Model_Store $store3
) {
$this->beConstructedWith(array(
'app' => $app,
'staging_collection' => $stagingCollection,
));
$website1->getId()->willReturn('1');
$website2->getId()->willReturn('2');
$website1->getStores()->willReturn(array(1 => $store1, 2 => $store2));
$website2->getStores()->willReturn(array(3 => $store3));
$app->getWebsites()->willReturn(array($website1, $website2));
}
function it_does_not_return_stores_from_staging_websites(
$stagingCollection, $store1, $store2, Mock_Staging $staging
) {
$stagingCollection->getIterator()->willReturn(new \ArrayIterator(array($staging)));
$staging->getStagingWebsiteId()->willReturn('2');
$this->getAllStores()->shouldReturn(array(
1 => $store1,
2 => $store2,
));
}
}
class Mock_Staging extends \Enterprise_Staging_Model_Staging
{
public function getStagingWebsiteId()
{
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment