{"id":447243,"date":"2024-02-23T12:04:11","date_gmt":"2024-02-23T11:04:11","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=phpstorm&#038;p=447243"},"modified":"2025-09-17T11:34:09","modified_gmt":"2025-09-17T10:34:09","slug":"ai-for-php-how-to-make-ai-assistant-generate-test-implementations","status":"publish","type":"phpstorm","link":"https:\/\/blog.jetbrains.com\/fr\/phpstorm\/2024\/02\/ai-for-php-how-to-make-ai-assistant-generate-test-implementations","title":{"rendered":"AI for PHP: How to Make AI Assistant Generate Test Implementations"},"content":{"rendered":"\n<p>We started <a href=\"https:\/\/blog.jetbrains.com\/phpstorm\/2024\/02\/how-to-generate-php-tests-with-jetbrains-ai-assistant\/\">this series<\/a> by looking at how <a href=\"https:\/\/www.jetbrains.com\/ai\/\" target=\"_blank\" rel=\"noopener\">JetBrains AI Assistant<\/a> 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?<\/p>\n\n\n\n<p>Let\u2019s take a look at how we can do that.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Generate code with AI Assistant chat<\/h2>\n\n\n\n<p>The first step to generating an implementation is to have a test in place. Let\u2019s say we\u2019re building a command bus and have the following test:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/OcgTucNO41d-FPqUFaEmmmHT8e7ClqFk9eTgj3cuoIchYBjdAgfAOXoOzvplGJAsUHH1mJLJpUP5qLS7B3pKYDiQcRQG6OcKcZAHm1FJeUtOifJm7xGOi60cTWswDGyQHS9RdsgI77QAxzru76P56BQ-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>We haven\u2019t written anything else \u2013 there\u2019s no <code>CommandBus<\/code> class yet, let alone an implementation for the <code>handle<\/code> and <code>getHistory<\/code> methods. Let\u2019s use AI Assistant to generate the implementation for us. To start, open the <em>AI Actions<\/em> menu by selecting the text and pressing <em>Alt+Enter \/ \u2325Enter<\/em>. Next, choose <em>New Chat Using Selection<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/vqtpv5ftmzrAWfGN7Yjf0iYVOf_aoY0DC17qrsstEoLqWA6yIgGHB_h8R5YnjwPUE67sTMPvkwvA0cSMeJWK9sGXui_4UfXX4MRcjck_3KDHizKYuChDcnaSBCmN1iZJslpEgn7LwvlR-hOF_e8ijOg-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.jetbrains.com\/phpstorm\/\" target=\"_blank\" rel=\"noopener\">PhpStorm<\/a> will open the <em>AI Assistant<\/em> chat window. Here, you can chat with AI Assistant and ask it anything you\u2019d like. The code snippet you\u2019ve selected will already be present in the chat window, so let\u2019s give it a simple prompt \u2013 \u201cwrite an implementation for this test\u201d:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/MjBTRSFs5eyMWCCnyxARN4YQJ5Zwz7sD1acH6YEB0C7ctohFgrO-HLqufD_EU3k4GpBYNCM8wIliX7f3JAIHNlsFk58s5suZ0setdi4WeIdHnaLJFij9wOU-iagGkma90escovdaMjzcsaR5rS5_QGA-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>In a couple of seconds, AI Assistant will have the implementation ready:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/6GwpIdGYJQ0G6AOr3ifqvSkMuM9unAwAPinxKSxpItLqn1BMhEmdRczoQ4Sfh4fgAcLYlymcg-xeuB_pXUQvl9VDvxFo5M0c0VWRTgh136bw0WqOF1XVRC7s0yl6epkTqh9EGxQqhCSZzzOVBKuVUSg-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let\u2019s copy this code into a file and run our test to see what happens:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/R_fCNsUtGcXwolmLNq-Ik58seRh7wD9UXPwvF7L4qoYrFKmSxY9oZiiW4ddPx7Ewq2ZXAD45e9O7f2KoQP10PA-X1IOqMUBPMDw8T6g9VoaxyniWI3Qt-CD_gijD_CM3oQ7N_otkULHnhTEzjc2yR-Y-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>It works! AI Assistant has written a working implementation for our test. Scrolling through the code, it looks like a solid implementation, although we\u2019ll 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,<strong> <\/strong>saving valuable time. That\u2019s exactly what AI Assistant did!<\/p>\n\n\n\n<p>What if we want to use this prompt multiple times? Is there a way to reuse it?<\/p>\n\n\n\n<p>Of course!<\/p>\n\n\n    <div class=\"buttons\">\n        <div class=\"buttons__row\">\n                                                <a href=\"https:\/\/www.jetbrains.com\/ai\/#plans-and-pricing\" class=\"btn\" target=\"\" rel=\"noopener\">Try AI Assistant<\/a>\n                                                    <\/div>\n    <\/div>\n\n\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Saving custom prompts<\/h2>\n\n\n\n<p>Open the <em>AI Actions<\/em> menu by pressing <em>Alt+Enter \/ \u2325Enter<\/em><strong> <\/strong>somewhere within your code. Instead of choosing an existing menu item, click the <em>Add Your Prompts<\/em> link at the bottom:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/reaoxLgYJmqMTsjMmAL6kSqcU7zsQhSeT5pkRuf-yny6dfo-bYYApsiVGMiHaE7SDE3TXeGpNauISkUUW7gBk2phYbw338Vk6iMzHHio5btnZQ604VvZuNOoSk_2EXW18kLJhNL0yzWL2JRDRKUalMQ-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>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 <code>$SELECTION<\/code> variable, which will contain whatever code you selected when invoking your custom prompt.<\/p>\n\n\n\n<p>Let\u2019s write a prompt for our use case \u2013 creating the implementation for a selected test:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/ePkUUeMLIwXZpXkISTVlkCHLcFxQQZdj7wuijXrgLYkFvgT1J9cZdo5xUJ_VsoRyznIL7-dBf0HUdaEEPPbIdKYr-INs4EDmkDY3p2e2GbGFfkN5fSyzKHEVPBwNUEuUH_IKY9wT1SZvOd7fJEObVrs-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Let\u2019s try it out! Select the test again, press <em>Alt+Enter \/ \u2325Enter<\/em> to open the <em>AI Actions <\/em>menu, and you\u2019ll see how the custom <em>Write Implementation<\/em> action is now available!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/mvO8Rs5U7K-JOWWfGyPk4OG1ZITHsrk72CvyCydr6pwt4Kz2rst_zdTZ_sxYYPuQeXjSiWjJ3BaDPwZwG6YAh2iZvRe88PiBAk1E4Sj4pvcoSkEl0cUTmalXkNoVAioL0jg59NSzQOlhiUGGpOj6TZQ-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Select the action, and you\u2019ll see that PhpStorm immediately writes the correct message to AI Assistant, making our AI-assisted action reusable!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2025\/09\/Z-nU9TmmfUVHY8o1B-VQcxe6rnbfGxD7w_5LtbxungwCxgQjiaAOIPUSwg1aDH_j60tHNJ11nkRBllYfXjRqHY5Du-qqyyNk4t4H_pMlFEl5ArtK127hYnY2kzZsjQgmRM0nla1pxt4OkmJvqMqDk_M-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Throughout <a href=\"https:\/\/blog.jetbrains.com\/phpstorm\/2024\/02\/how-to-generate-php-tests-with-jetbrains-ai-assistant\/\">this series<\/a>, we\u2019ve seen several examples of how we can use AI Assistant to our benefit. We don\u2019t expect it to write perfect code, but we do use it to generate boilerplate code that we can build upon. It\u2019s 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.&nbsp;<\/p>\n\n\n\n<p>Personally, I don\u2019t think AI will replace real programmers anytime soon, but I do think it\u2019s 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.&nbsp;<\/p>\n\n\n\n<p>What are your expectations regarding AI assisting you with your job? Share your thoughts in the comments!&nbsp;<\/p>\n\n\n    <div class=\"buttons\">\n        <div class=\"buttons__row\">\n                                                <a href=\"https:\/\/www.jetbrains.com\/ai\/\" class=\"btn\" target=\"\" rel=\"noopener\">Learn more about AI Assistant<\/a>\n                                                    <\/div>\n    <\/div>\n\n\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Useful links<\/h2>\n\n\n\n<p>Did you enjoy reading this blog post? Here are more from this series:<\/p>\n\n\n\n<ul>\n<li><a href=\"https:\/\/blog.jetbrains.com\/fr\/phpstorm\/2024\/02\/ai-for-php-how-to-automate-unit-testing-using-ai-assistant\">AI for PHP: How To Automate Unit Testing Using AI Assistant<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/blog.jetbrains.com\/fr\/phpstorm\/2024\/02\/ai-for-php-how-to-tweak-ai-prompts-to-improve-php-tests\">AI for PHP: How to Tweak AI Prompts to Improve PHP Tests<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Resources:<\/strong><\/p>\n\n\n\n<ul>\n<li><a href=\"https:\/\/www.jetbrains.com\/help\/phpstorm\/ai-assistant.html\" target=\"_blank\" rel=\"noopener\">AI Assistant in PhpStorm<\/a> (documentation)<\/li>\n\n\n\n<li><a href=\"https:\/\/www.jetbrains.com\/ai\/#plans-and-pricing\" target=\"_blank\" rel=\"noopener\">AI Assistant pricing<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Videos:<\/strong><\/p>\n\n\n\n<ul>\n<li><a href=\"https:\/\/youtu.be\/-NnYtfzO7qU?feature=shared\" target=\"_blank\" rel=\"noopener\">JetBrains AI Assistant introduction<\/a><\/li>\n<\/ul>\n","protected":false},"author":1326,"featured_media":458590,"comment_status":"closed","ping_status":"closed","template":"","categories":[2347],"tags":[8168,8337],"cross-post-tag":[8396],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/phpstorm\/447243"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/phpstorm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/types\/phpstorm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/users\/1326"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/comments?post=447243"}],"version-history":[{"count":10,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/phpstorm\/447243\/revisions"}],"predecessor-version":[{"id":619817,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/phpstorm\/447243\/revisions\/619817"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/media\/458590"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/media?parent=447243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/categories?post=447243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/tags?post=447243"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/fr\/wp-json\/wp\/v2\/cross-post-tag?post=447243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}