public function testGetLastRecipient() { $state = $this->prophesize(StateInterface::class); $state->get('my_hugs.last_recipient')->willReturn('Dries'); $tracker = new MyHugTracker($state->reveal()); $this->assertEquals('Dries', $tracker->getLastRecipient()); }
/** * To run this method, use an url such as this: * http://jane.tomhartung.com/my_hug/jane/tom/2 */ public function myHug($to, $from, $count) { $this->myHugTracker->addMyHug($to); if (!$count) { $count = $this->config('my_hugs.settings')->get('default_count'); } return ['#theme' => 'my_hug_page', '#from' => $from, '#to' => $to, '#count' => $count]; }
public function build() { if ($this->configuration['enabled']) { $message = $this->t('@to was the last person my_hugged', ['@to' => $this->myHugTracker->getLastRecipient()]); } else { $message = $this->t('Srsly wtf, no my_hugs :-('); } // -------------------------------------- // (0) We need the "use Drupal\my_hugs\TeachMe;" statement, else we get this error: // "Class 'Drupal\\my_hugs\\Plugin\\Block\\TeachMe' not found" // $teachMe = new TeachMe( 'MyHugStatus::build()' ); // -------------------------------------- // (1a) works with (1a) only // $learningMore = new LearningMore( 'MyHugStatus::build()' ); // // (1) Does NOT work with either (1a) or (1b), it looks for the class inside of this namespace // ie: "Class 'Drupal\\my_hugs\\Plugin\\Block\\Drupal\\my_hugs\\LearningMore\\LearningMore' not found" // $learningMore = new Drupal\my_hugs\LearningMore\LearningMore( 'MyHugStatus::build()' ); // // (1) works with either (1a) OR (1b) but NOT BOTH // $learningMore_1 = new \Drupal\my_hugs\LearningMore\LearningMore( 'MyHugStatus::build() - 1' ); // -------------------------------------- // (2a) Autoloading with inheritance can be a little tricky // (2b) Using just the one "use" works just as well as using both (2a) use statements // // (See comment in the MyHugTracker class....) // $baseObject = new \Drupal\my_hugs\Heirarchy\TheBase( 'MyHugStatus::build()' ); // $subObject = new \Drupal\my_hugs\Heirarchy\SubClass( 'MyHugStatus::build()' ); // -------------------------------------- // (3) the idmygadget code should really be in the vendor directory // // Based on my understanding of these pages: // https://www.drupal.org/node/2156625 and especially // https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md - Symfony Example // What I have should work. Fuuuuuck ok I will try another technique // // $jmwsService = new \Jmws\myservice\JmwsService( 'MyHugStatus::build()' ); // $jmwsServiceDrupal = new \Jmws\myservice\JmwsServiceDrupal( 'MyHugStatus::build()' ); // -------------------------------------- // This is the pattern we will use - for the time being // -------------------------------------- // (4) Create a namespaced and global object using the non-namespaced (global) idmygadget code in a separate directory // // $namespacedObject = new \Drupal\my_hugs\GoGlobal\NamespacedService( 'MyHugStatus::build()' ); // $serviceObject = new \Drupal\my_hugs\GoGlobal\GlobalService( 'MyHugStatus::build()' ); // $serviceSubclassObject = new \Drupal\my_hugs\GoGlobal\GlobalServiceDrupal( 'MyHugStatus::build()' ); // $jmwsIdMyGadget = new \Drupal\my_hugs\JmwsIdMyGadget\JmwsIdMyGadgetDrupal(); // $jmwsIdMyGadget = new Drupal\my_hugs\JmwsIdMyGadget\JmwsIdMyGadgetDrupal(); // $jmwsIdMyGadget = new JmwsIdMyGadgetDrupal(); $message .= '<br />Not using our service to log messages right now.'; /* $message .= '<br />Using our service to log messages so we know it can do that.'; $this->service = $this->myHugTracker->getService(); $this->service->logToday( 'Hi from MyHugStatus::build()!!' ); */ return ['#markup' => $message]; }