Sonata\CustomerBundle\Entity\AddressManager::setCurrent PHP Method

setCurrent() public method

public setCurrent ( Sonata\Component\Customer\AddressInterface $address )
$address Sonata\Component\Customer\AddressInterface
    public function setCurrent(AddressInterface $address)
    {
        foreach ($address->getCustomer()->getAddressesByType($address->getType()) as $custAddress) {
            if ($custAddress->getCurrent()) {
                $custAddress->setCurrent(false);
                $this->save($custAddress);
                break;
            }
        }
        $address->setCurrent(true);
        $this->save($address);
    }

Usage Example

 public function testSetCurrent()
 {
     $currentAddress = $this->getMock('Sonata\\Component\\Customer\\AddressInterface');
     $currentAddress->expects($this->once())->method('getCurrent')->will($this->returnValue(true));
     $currentAddress->expects($this->once())->method('setCurrent');
     $custAddresses = array($currentAddress);
     $customer = $this->getMock('Sonata\\Component\\Customer\\CustomerInterface');
     $customer->expects($this->once())->method('getAddressesByType')->will($this->returnValue($custAddresses));
     $address = $this->getMock('Sonata\\Component\\Customer\\AddressInterface');
     $address->expects($this->once())->method('setCurrent');
     $address->expects($this->once())->method('getCustomer')->will($this->returnValue($customer));
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $em->expects($this->any())->method('persist');
     $registry = $this->getMock('Doctrine\\Common\\Persistence\\ManagerRegistry');
     $registry->expects($this->any())->method('getManagerForClass')->will($this->returnValue($em));
     $addressManager = new AddressManager('Sonata\\Component\\Customer\\AddressInterface', $registry);
     $addressManager->setCurrent($address);
 }