I have the following logic hook
<?php if (!defined('sugarEntry') || !sugarEntry) { die('Not A Valid Entry Point'); } require_once('modules/Order/OrderSearchHelper.php'); /** * Class OrderHooks */ class OrderHooks { public $bean; public $event; public $arguments; public $logger; public function init($bean, $event, $arguments, $logger) { $this->bean = $bean; $this->event = $event; $this->arguments = $arguments; $this->logger = $logger; } /** * @param SugarBean $bean */ private function populateSiteFields($bean, $db): void { if ($bean->load_relationship('accounts_Order_1')) { $accountBeans = $bean->accounts_Order_1->getBeans(); /* @var $account CustomAccount */ foreach ($accountBeans as $account) { $bean->mpan = $account->mpan_c; $bean->postcode = $account->site_address_post_code_c; $bean->site_address_line_1 = $account->site_address_line_1_c; $bean->site_type_c = $account->site_type_c; $bean->account_id = $account->id; $bean->account_name = $account->name; $bean->site_id = $account->id; } } } /** * @param SugarBean $bean * @param $event * @param $arguments */ public function afterRetrieveLogic(SugarBean $bean, $event, $arguments) { $db = DBManagerFactory::getInstance(); $this->populateSiteFields($bean, $db); } /** * Logichook - before_save - Order * * @param $bean * @param $event * @param $arguments * * @noinspection PhpUnused */ public function OrderUpdated($bean, $event, $arguments): void { $_logger = $GLOBALS['log']; $this->init($bean, $event, $arguments, $_logger); $this->logger->debug('In OrderUpdated...bean->id: ' . $this->bean->id); //Find Jobs related to linked Order that are in Open or Planned status $booking_array = $this->fetchBookings(); if (count($booking_array) === 0) { //we dont have any bookings or jobs or return. return; } } /** * * fetchBookings - Fetch bookings which relate indirectly to a Order. * * @return array */ public function fetchBookings(): array { $this->bean->load_relationship('Order_meetings_1'); //$this->load_relationship('Order_meetings_1'); $relatedBookings = $this->bean->Order_meetings_1->getBeans(); //$relatedBookings = $this->Order_meetings_1->getBeans(); $bookingArray = []; foreach ($relatedBookings as $booking) { //Build the "bookings" part of the return array //Need to collect only related "Planned" Bookings for this if ($booking->status === 'Planned') { $bookingArray[] = [ 'bookingId' => $booking->booking_id_c ]; } } return $bookingArray; } /** * @param Order $bean * @param String $event * @param array $arguments * * @noinspection PhpUnused */ public function returnSupplier(Order $bean, String $event, array $arguments): void { if (!empty($bean->account_id)) { $customAccount = BeanFactory::getBean('CustomAccount', $bean->account_id); if (!empty($customAccount)) { $supplier = $customAccount->getCurrentSupplier(); //$bean->current_supplier_c = $supplier->name; //@TODO create non-db col } } } public function updateStatus(SugarBean $bean, $event, $arguments) { if (empty($bean->fetched_row)) { return true; } global $current_user; switch ($bean->status) { case 'In_Progress': $bean->assigned_user_id = $current_user->id; break; case 'Open': $bean->assigned_user_id = null; break; } } /** * @throws SugarQueryException */ public function beforeFilter($bean, $event, $args) { if ( empty($_REQUEST['filter']) ) { return; } $filter = $_REQUEST['filter']; $applyFilter = false; $accountSearchFields = []; if (strpos(json_encode($filter), 'site_type_c') > 0) { $applyFilter = true; $accountSearchFields = ['site_type_c']; } elseif (strpos(json_encode($filter), 'mpan') > 0) { $applyFilter = true; $accountSearchFields = ['mpan']; } elseif (strpos(json_encode($filter), 'site_address_line_1') > 0) { $applyFilter = true; $accountSearchFields = ['site_address_line_1']; } elseif (strpos(json_encode($filter), 'postcode') > 0) { $applyFilter = true; $accountSearchFields = ['postcode']; } if ($applyFilter) { OrderSearchHelper::addCustomFieldSearchToQuery($args[1]['id_query'], $accountSearchFields, $accountSearchFields, 'accounts_Order_1'); } } }
I have been trying to test some of this code using phpunit and mocking objects, But I am having trouble understanding how it works and what to do
<?php use PHPUnit\Framework\TestCase; require_once 'custom/modules/Order/OrderHooks.php'; /** * Class OrderHooksTest * @coversDefaultClass OrderHooks */ class OrderHooksTest extends TestCase { private $hooks; private $booking; private $Order; protected function setUp() { parent::setUp(); $this->logger = $this->createMock(SugarLogger::class); $this->booking = $this->createMock(CustomMeeting::class); $this->Order = $this->createMock(Order::class); $this->hooks = new OrderHooks(); $this->hooks->init($this->Order, null, null, $this->logger); //SugarTestHelper::setUp("beanList"); } /** * @covers OrderHooks::fetchBookings */ public function testFetchBookingsSuccess() { $this->booking->status = 'Planned'; $this->booking->booking_id_c = 'x'; $this->Order->load_relationship = function () { return true; }; //->will($this->returnValue('foo')); $this->Order->Order_meetings_1 = true;//$this->createMock(CustomMeeting::class); $this->Order->Order_meetings_1->getBeans = function () { return [$this->createMock(CustomMeeting::class)]; }; //print_r($this->Order->Order_meetings_1->getBeans);die; //$this->Order->Order_meetings_1->method('getBeans') //->willReturn($this->booking); //$this->logger->shouldReceive('info')->once()->with('relateOppToForecast success!'); $this->hooks->fetchBookings(); } }
I get error with `Attempt to assign property 'getBeans' of non-object`