0

I have a test class that reports an undefined variable and I cannot seem to understand what the issue is.

Basically the listener below is suppose to listen to an application boot event documented in the class below:

<?php

 namespace Colleen\Core\Event\Application;

final class ApplicationBootedEvents
{
  const APP_BOOTED = 'application.booted';
 }

My event class is as shown below which receives an instance of the application itself.

<?php

 namespace Colleen\Core\Event\Application;

 use Symfony\Component\EventDispatcher\Event;
 use Colleen\Core\Application;

/**
 * The application.booted event is dispatched each time 
 * an application instance is created in the system.
 *
 */
class ApplicationBootedEvent extends Event
{
  protected $app;

  public function __construct(Application $app)
  {
     $this->app = $app;
   }

  public function getApplication()
  {
     return $app;
   }
}

These two classes to me look perfect according to Symfony's documentation on the Event Dispatcher Component. Following is the listener class that is suppose to listen to ApplicationBootedEvents::APP_BOOTED event.

<?php

 namespace Colleen\Core\Event\Application\Listener;

 use Colleen\Core\Event\Application\ApplicationBootedEvent;

 class ApplicationBootedListener
{
   public function onBoot(ApplicationBootedEvent $event)
   {
     $container = $event->getApplication()->getContainer();

     $container->set('class.dispatcher', '\\Symfony\\Component\\EventDispatcher\\EventDispatcher');
    }
 }

The Listener class does nothing at the moment and my test case is to test whether the "class.dispatcher" key exist on my container which simple extends Pimple and is made available through the Application Object.

Below is my test that shows how these will eventually be used in my front controller or any class that stands between them and the front controller.

<?php

 namespace Colleen\Qa\Core\Event\Application\Listener;

 use Colleen\Core\Event\Application\Listener\ApplicationBootedListener;
use Colleen\Core\Event\Application\ApplicationBootedEvents;
use Colleen\Core\Event\Application\ApplicationBootedEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Colleen\Core\Container\Container;
use Colleen\Core\Application;

class AppliocationBootedListenerTest extends \PHPUnit_Framework_TestCase
{
  public function testApplicationBootListener()
  {
    $dispatcher = new EventDispatcher();

    $dispatcher->addListener(
      ApplicationBootedEvents::APP_BOOTED, array(
        new ApplicationBootedListener(), 'onBoot'
      ));

    $app = $dispatcher->dispatch(ApplicationBootedEvents::APP_BOOTED, new ApplicationBootedEvent(new Application(new Container())))->getApplication();

    $expected = '\\Symfony\\Component\\EventDispatcher\\EventDispatcher';
    $actual = $app->getContainer()->get('class.dispatcher');

    $this->assertSame($expected, $actual);
  }
}

The idea is to test whether the Listener gets called and if it is able to feed our application object's container with all the necesary objects we will need to get our web framework to work.

Below is the output I get as a result if running this test case.

enter image description here

1 Answer 1

2

There's an error in your ApplicationBootedEvent.php file, on line 24 as the stack trace suggested..

Change

public function getApplication()
{
    return $app;
}

To

public function getApplication()
{
    return $this->app;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Good Lawd! This is what happens when you are focusing onto so much at the same time. Thanks @LMS94. This indeed solved my issue. The main issue I was having was to really understand my stracktrace although I would admit I checked all of these files one by one and still couldn't spot my mistake. Thanks again.
The stack trace is ordered descending (in this case anyway) so the file at the top is where the error will have been thrown.
I will keep that in mind next time. I have been focusing on my test case thinking that the error is as a result of my logic there but it looked fine to me. You have taught me something today. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.