|
| 1 | +# PHPStorm & Docker |
| 2 | + |
| 3 | +The goal for this tutorial is to help containerize an application |
| 4 | +to leverage the tools within PHPStorm (Test Suite, Coverage, etc). |
| 5 | + |
| 6 | +To do this the following things need to happen: |
| 7 | + |
| 8 | +1) Setup Docker for Mac in PHPStorm. |
| 9 | +2) Setup Language (PHP in this example) to use a remote interpreter. |
| 10 | +3) Fix the paths to match our standard (/code for project code) |
| 11 | +4) Setup Tests (PHPUnit or Codeception) to use a special docker-compose file. |
| 12 | + |
| 13 | +We are assuming the following things: |
| 14 | + |
| 15 | +1) Docker compose file exists for the project. |
| 16 | +2) The project is already running via (docker-compose up --build) |
| 17 | +3) Some small knowledge of Docker |
| 18 | + |
| 19 | +### Setup Docker for Mac in PHPStorm |
| 20 | + |
| 21 | +Head to Preferences and go to `Build, Execution, Deployment -> Docker`. Create a |
| 22 | +an item called "Docker" using the daemon option "Docker for Mac". |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +### Creating an isolated docker-compose for testing. |
| 27 | + |
| 28 | +In order for PHPStorm to run tests without affecting containers. You need to |
| 29 | +create a new container that matches whatever container your code works on. |
| 30 | + |
| 31 | +For example, here a random PHP project `docker-compose-phpstorm.yml` file. |
| 32 | + |
| 33 | +``` |
| 34 | +version: '2' |
| 35 | +services: |
| 36 | + project-test: |
| 37 | + container_name: sourcetoad_project_test |
| 38 | + build: . |
| 39 | + volumes: |
| 40 | + - ../:/code:delegated |
| 41 | + networks: |
| 42 | + - project-internal |
| 43 | + - st-mariadb-101 |
| 44 | + - st-redis-32 |
| 45 | +networks: |
| 46 | + project-internal: |
| 47 | + st-redis-32: |
| 48 | + external: |
| 49 | + name: st-redis-32 |
| 50 | + st-mariadb-101: |
| 51 | + external: |
| 52 | + name: st-mariadb-101 |
| 53 | +``` |
| 54 | + |
| 55 | +As you can see, it is a subset of the compose file. It only includes a clone of |
| 56 | +the PHP container, keeping intact the networks, mounts and env file. This allows |
| 57 | +the new container to have access to databases and files which may be needed in |
| 58 | +the test suite. You can use multiple compose files if you need one for an |
| 59 | +acceptance test container. |
| 60 | + |
| 61 | +If your project requires multiple containers in the testing aspect. Recommend |
| 62 | +creating descriptive container names, like `project-codeception` so you could |
| 63 | +additionally have `project-behat` if needed. |
| 64 | + |
| 65 | +### Setup Docker Container for PHP Language |
| 66 | + |
| 67 | +Head to Preferences and go to `Languages & Frameworks > PHP`. From here we are |
| 68 | +going to click the "..." next to the CLI Interpreter option in order to create a |
| 69 | +new "Docker Compose" interpreter. Using the file we created above, we will |
| 70 | +select that one. Once selected, you need to select the container to use. This |
| 71 | +will more than likely be auto-selected for you. |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +### Setup Docker Container for PHP Test Framework |
| 76 | + |
| 77 | +Head to Preferences and go to `Languages & Framework > PHP > Test Frameworks`. |
| 78 | +Once here, click the "+" icon in top right to create a new test environment. You |
| 79 | +will more than likely select one of the following: |
| 80 | + |
| 81 | + * PHPUnit by Remote Interpreter |
| 82 | + * Codeception by Remote Interpreter |
| 83 | + * Behat by Remote Interpreter |
| 84 | + * PHPSpec by Remote Interpreter |
| 85 | + |
| 86 | +Once you select one, you will need to select the container to run this tool at. |
| 87 | +You already created the container and loaded it in the previous steps. So just |
| 88 | +select the container that works. Once loaded, you need to put in the file path |
| 89 | +to that binary. In the screenshot below, we are using Codeception so looking |
| 90 | +for `/code/vendor/bin/codecept`. The `/code` is what we use to mount our code |
| 91 | +into the container. This pattern may be different depending on lots of factors. |
| 92 | + |
| 93 | +Finally, for this step you need to make sure you specify the configuration file |
| 94 | +for the test framework. The default location may be wrong, in the photo below I |
| 95 | +gave the direct path to the configuration like: `/code/codeception.yml`. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +### Setup Test Case to run in IDE |
| 100 | + |
| 101 | +Head "Run -> Edit Configurations". Now you need to select the test framework |
| 102 | +that matches the test framework you attached into the container. Since, we used |
| 103 | +Codeception, we will continue to use Codeception for this demo. |
| 104 | + |
| 105 | +Our goal is to select the "Defined in the Configuration File". No matter the |
| 106 | +tool, if you configured it correctly, we can just use those values and inherit. |
| 107 | + |
| 108 | +This demo has made an additional change and requested Codeception to only run |
| 109 | +the "unit" test suite. |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +### Run the Test Suite |
| 114 | + |
| 115 | +Now we should be able to the launch the test suite. Our docker-compose file will |
| 116 | +execute, spawning a new "test" container based off our main language container. |
| 117 | + |
| 118 | +This will then proceed to execute the test framework we specified, collecting |
| 119 | +the results and parsing them into the UI. |
| 120 | + |
| 121 | +This can be shown below: |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +With that you have successfully hooked the test suites, while keeping Docker |
| 126 | +inside PHPStorm. |
| 127 | + |
| 128 | + |
0 commit comments