Tutorials

AI for PHP: How to Make AI Assistant Generate Test Implementations

Read this post in other languages:

We started this series by looking at how JetBrains AI Assistant can generate tests from an existing implementation. But what if we want to use AI Assistant the other way around? What if we want to take a more test-driven-development approach, writing our tests first, and having AI Assistant generate an implementation for us?

Let’s take a look at how we can do that.

Generate code with AI Assistant chat

The first step to generating an implementation is to have a test in place. Let’s say we’re building a command bus and have the following test:

We haven’t written anything else – there’s no CommandBus class yet, let alone an implementation for the handle and getHistory methods. Let’s use AI Assistant to generate the implementation for us. To start, open the AI Actions menu by selecting the text and pressing Alt+Enter / ⌥Enter. Next, choose New Chat Using Selection:

PhpStorm will open the AI Assistant chat window. Here, you can chat with AI Assistant and ask it anything you’d like. The code snippet you’ve selected will already be present in the chat window, so let’s give it a simple prompt – “write an implementation for this test”:

In a couple of seconds, AI Assistant will have the implementation ready:

Let’s copy this code into a file and run our test to see what happens:

It works! AI Assistant has written a working implementation for our test. Scrolling through the code, it looks like a solid implementation, although we’ll change some stylistic details. Our goal with AI Assistant throughout this series has not been to have AI Assistant write the perfect code for us, but to generate the boring boilerplate that we can use as a starting point, saving valuable time. That’s exactly what AI Assistant did!

What if we want to use this prompt multiple times? Is there a way to reuse it?

Of course!

Saving custom prompts

Open the AI Actions menu by pressing Alt+Enter / ⌥Enter somewhere within your code. Instead of choosing an existing menu item, click the Add Your Prompts link at the bottom:

PhpStorm allows you to write custom prompts and save them as custom AI actions, which is what we want to do in this case. Within these prompts, you can use the $SELECTION variable, which will contain whatever code you selected when invoking your custom prompt.

Let’s write a prompt for our use case – creating the implementation for a selected test:

Let’s try it out! Select the test again, press Alt+Enter / ⌥Enter to open the AI Actions menu, and you’ll see how the custom Write Implementation action is now available!

Select the action, and you’ll see that PhpStorm immediately writes the correct message to AI Assistant, making our AI-assisted action reusable!

Conclusion

Throughout this series, we’ve seen several examples of how we can use AI Assistant to our benefit. We don’t expect it to write perfect code, but we do use it to generate boilerplate code that we can build upon. It’s interesting to see how accurate AI Assistant is in writing code that works, and I am looking forward to it improving even more in the releases to come. 

Personally, I don’t think AI will replace real programmers anytime soon, but I do think it’s a great tool that can help speed up the process of writing code, especially the routine parts, so that we can focus on the parts of our code that matter the most. 

What are your expectations regarding AI assisting you with your job? Share your thoughts in the comments! 

Useful links

Did you enjoy reading this blog post? Here are more from this series:

Resources:

Videos:

image description