olimorris / neotest-phpunit

🧪 Neotest adapter for PHPUnit
MIT License
29 stars 22 forks source link

Add `dap` strategy #17

Closed przepompownia closed 8 months ago

przepompownia commented 10 months ago

I looked into https://github.com/nvim-neotest/neotest-python to see how dap strategy can be implemented and tried to apply something similar here in its simplest form to start with. With

require('neotest').setup({
  adapters = {
    require('neotest-phpunit') {
      dap = require('dap').configurations.php[1], -- details at the bottom
    },
  }
})

and :lua require("neotest").run.run({strategy = 'dap'}) I see (after setting some breakpoint) that the adapter starts as expected (with correct parameters) but it seems that the test does not run (at all or when the adapter is ready).

Running then :lua require("neotest").run.run({}) causes stopping the test at the breakpoint.

Can anyone help me?

{
  type = 'php',
  request = 'launch',
  name = 'Listen for XDebug',
  port = 9003,
  stopOnEntry = false,
  xdebugSettings = {
    max_children = 512,
    max_data = 1024,
    max_depth = 4,
  },
  breakpoints = {
    exception = {
      Notice = false,
      Warning = false,
      Error = false,
      Exception = false,
      ['*'] = false,
    },
  },
}
olimorris commented 10 months ago

How do you debug currently with PHP and Neovim?

przepompownia commented 10 months ago

Hi @olimorris

I use nvim-dap for two years. I have no problem with running DAP adapter separately and run some test both from CLI and by neotest and then stop on a breakpoint.

By default I have disabled XDebug. If I need, I append -dzend_extension=xdebug.so. To avoid complicating this PR, I assume here that XDebug is enabled permanently. Ultimately I would like to be able to use a separate phpunit_cmd per strategy if possible.

It's only for convenience. If it would require a lot of work to complete, we may give up and work as before.

Do you need some standalone reproduction containing neotest with this change, nvim-dap, phpunit and simple test? I can prepare it if it would be helpful.

olimorris commented 10 months ago

I haven't looked into this in any detail however I would have thought that we could take your existing config for running PHPUnit tests and triggering a debugger and use it in this adapter?

przepompownia commented 10 months ago

https://github.com/przepompownia/neotest-phpunit-example - please look at the demo. make start should be enough to install dependencies and run, assuming that you have composer and php with xdebug.

Only five user mappings are defined here

  vim.keymap.set('n', '<Esc>', vim.cmd.fclose)
  vim.keymap.set({'n'}, ',dr', dap.continue, {})
  vim.keymap.set({'n'}, ',dc', dap.close, {})
  vim.keymap.set({'n'}, ',nr', require('neotest').run.run, {})
  vim.keymap.set({'n'}, ',nd', function () require('neotest').run.run({strategy = 'dap'}) end, {})

and the only user command PhpUnitWithXdebug to run the test manually.

Notice that on ,nd (i.e. :lua require('neotest').run.run({strategy = 'dap'})) no test is run.

przepompownia commented 10 months ago

@olimorris my English is still poor. I didn't fully understand the intent of your question. It was easier for me to create a demo :wink: It helped me use the adapter in a way I hadn't used before (program, runtimeArgs etc. - see https://github.com/xdebug/vscode-php-debug#supported-launchjson-settings).

olimorris commented 10 months ago

This looks great. I'll try and look at this in the next couple of weeks; I'm rather time poor at the moment, unfortunately.

przepompownia commented 10 months ago

One of the disadvantages is that phpunit_cmd cannot be table. The program field in the adapter must be string if I know. At the moment I added validation for that.

olimorris commented 8 months ago

Sorry I haven't got a round to looking at this.

I'm happy to merge this if you think it's finished?

przepompownia commented 8 months ago

I have tested it on https://github.com/phpactor/phpactor/pull/2476 without any problem. From my perspective it can be merged.

olimorris commented 8 months ago

Thanks for your efforts on this!

przepompownia commented 8 months ago

Thanks for merging! I hope it will be useful to someone else.

eerison commented 4 months ago

hey @przepompownia do you have any example how to use this on lazyvim?

for example: https://github.com/eerison/lazyvim/blob/main/lua/plugins/php.lua#L34

it is related with: https://github.com/olimorris/neotest-phpunit/issues/25