Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Telescope installed and enabled #95

Closed
andresayej opened this issue Mar 4, 2020 · 5 comments
Closed

Problem with Telescope installed and enabled #95

andresayej opened this issue Mar 4, 2020 · 5 comments

Comments

@andresayej
Copy link

  • Laravel Version: 7.0.2
  • PHP Version: 7.4.3 (but not relevant)
  • Database Driver & Version: not relevant
  • nunomaduro/collision: 4.1.2
  • laravel/telescope: 3.1.1
  • phpunit/phpunit: 8.5.2

Description:

As requested by @nunomaduro on Twitter I'm describing a strange behavior when running the php artisan test command if Telescope is installed and enabled (Telescope is enabled by default after a fresh installation). This behavior is only visible if you have query execution in your Tests. This behavior is not visible if you run the standard phpunit command

Steps To Reproduce:

  1. Fresh Laravel 7 installation:
laravel new laravel7app
  1. Install Laravel Telescope
2.1
composer require laravel/telescope --dev
2.2
php artisan telescope:install
2.3
php artisan migrate
  1. Create a test with query execution in tests/Feature/ExampleTest.php
<?php

namespace Tests\Feature;

use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    use RefreshDatabase;

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $user = factory(User::class)->create();

        $this->assertDatabaseHas('users', $user->only('email'));
    }
}

Results

When running php artisan test

php artisan test

   PASS  Unit\ExampleTest
  ✓ basic test

   FAIL  Feature\ExampleTest
  ✕ basic test

  Tests:  1 failed, 1 passed

   ErrorException 

  Trying to access array offset on value of type null

  at vendor/laravel/telescope/src/Watchers/QueryWatcher.php:46
    42|             'bindings' => [],
    43|             'sql' => $this->replaceBindings($event),
    44|             'time' => number_format($time, 2),
    45|             'slow' => isset($this->options['slow']) && $time >= $this->options['slow'],
  > 46|             'file' => $caller['file'],
    47|             'line' => $caller['line'],
    48|             'hash' => $this->familyHash($event),
    49|         ])->tags($this->tags($event)));
    50|     }

When running phpunit

phpunit         
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 433 ms, Memory: 24.00 MB

OK (2 tests, 2 assertions)

Quick and easy steps to mitigate the issue

Add TELESCOPE_ENABLED=false in your phpunit.xml file.

<server name="TELESCOPE_ENABLED" value="false"/>

Screenshot 2020-03-04 at 11 34 30

Proposed solutions

  1. Add information note in the Laravel 7 upgrade guide:
    Add TELESCOPE_ENABLED=false in your phpunit.xml file.
<server name="TELESCOPE_ENABLED" value="false"/>
  1. Auto-disable telescope when executing php artisan test by default.
    Probably should be added somewhere here:
    File: collision/src/Adapters/Laravel/Commands/TestCommand.php
/**
     * Gets an array with phpunit envs detected on the phpunit.xml file.
     *
     * @return array
     */
    protected function phpunitEnvs()
    {
        $envs = [];

        if (! file_exists($file = base_path('phpunit.xml'))) {
            $file = base_path('phpunit.xml.dist');
        }

        if (file_exists($file)) {
            /** @var \SimpleXMLElement $xml */
            $xml = simplexml_load_string((string) file_get_contents($file));

            if (is_iterable($xml->php->server)) {
                foreach ($xml->php->server as $env) {
                    if (isset($env['name'])) {
                        $envs[$env['name']->__toString()] = false;
                    }
                }
            }

            if (is_iterable($xml->php->env)) {
                foreach ($xml->php->env as $env) {
                    if (isset($env['name'])) {
                        $envs[$env['name']->__toString()] = false;
                    }
                }
            }
        }

        return $envs;
    }
@nunomaduro
Copy link
Owner

Can you try with the latest dev-master?

@andresayej
Copy link
Author

andresayej commented Mar 4, 2020

The problem is still present with the dev-stable branch

@nunomaduro
Copy link
Owner

nunomaduro commented Mar 4, 2020

@andresayej Can you run ./vendor/bin/phpunit instead of phpunit? I have the problem in both ./vendor/bin/phpunit and php artisan test

Seems that multiple have this problem too:

@andresayej
Copy link
Author

andresayej commented Mar 4, 2020

Yes when running ./vendor/bin/phpunit the problem is there.

@nunomaduro
Copy link
Owner

Means that you have TELESCOPE_ENABLED=false in your global env.

So there is no problem with collision. Thanks for reporting though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants