Whenever referencing dependency code ask if it is available in all environments

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the dumb-mistakes-and-gotchas category.

Last Updated: 2024-03-27

I had the following code in my ServiceProvider.php file

<?php
public function register()

  // Add `fillHidden` to `$browser` methods in Dusk tests
  Browser::macro('fillHidden', function ($name , $value) {
      $this->script("document.getElementsByName('$name')[0].value = '$value'");
      return $this;
  });
}

This addition, however, broke staging since the Browser object was only available locally (since it was part of the Dusk testing framework - a dependency which was not installed on staging or production).

The fix was to condition the macro definition on the current env:

<?php
public function register()
{
  if (config('app.env') == "local") {

    // Add `fillHidden` to $browser methods in Dusk tests
    Browser::macro('fillHidden', function ($name , $value) {
        $this->script("document.getElementsByName('$name')[0].value = '$value'");
        return $this;
    });
  }
}

Lesson

Whenever you refence an object in code, ask yourself: "is this dependency available everywhere or just in some envs?"