/** * Check beforeSave and afterSave callbacks for create and update methods * * @param string $method * @dataProvider methodsDataProvider */ public function testCallback($method) { $customerData = array('firstname' => 'test'); $addressData = null; $callback = $this->getMockBuilder('stdClass')->setMethods(array('beforeSave', 'afterSave'))->getMock(); $callback->expects($this->once())->method('beforeSave')->with($this->_customer, $customerData, $addressData); $callback->expects($this->once())->method('afterSave')->with($this->_customer, $customerData, $addressData); $this->_service->setBeforeSaveCallback(array($callback, 'beforeSave')); $this->_service->setAfterSaveCallback(array($callback, 'afterSave')); if ($method == 'create') { $this->assertInstanceOf('Mage_Customer_Model_Customer', $this->_service->create($customerData, $addressData)); } else { $this->_customer->expects($this->once())->method('getId')->will($this->returnValue(1)); $this->assertInstanceOf('Mage_Customer_Model_Customer', $this->_service->update(1, $customerData, $addressData)); } }
/** * Test beforeSave and afterSave callback * * @magentoDataFixture Mage/Customer/_files/customer.php */ public function testCallback() { $customer = $this->_customerFactory->create()->load(1); $customerData = array('firstname' => 'Updated name'); $customer->addData($customerData); $addressData = array(array('firstname' => 'John', 'lastname' => 'Smith', 'street' => 'Green str, 67', 'country_id' => 'AL', 'city' => 'CityM', 'postcode' => '75477', 'telephone' => '3468676')); $callbackCount = 0; $callback = function ($actualCustomer, $actualData, $actualAddresses) use($customer, $customerData, $addressData, &$callbackCount) { $callbackCount++; // Remove updated_at as in afterSave updated_at may be changed $expectedCustomerData = $customer->getData(); unset($expectedCustomerData['updated_at']); PHPUnit_Framework_Assert::assertEquals($expectedCustomerData, $actualCustomer->toArray(array_keys($expectedCustomerData))); PHPUnit_Framework_Assert::assertEquals($customerData, $actualData); PHPUnit_Framework_Assert::assertEquals($addressData, $actualAddresses); }; $this->_model->setBeforeSaveCallback($callback); $this->_model->setAfterSaveCallback($callback); $this->_model->update(1, $customerData, $addressData); $this->assertEquals(2, $callbackCount, 'Not all expected callbacks were called.'); }