This guide will help you set up your PHPStorm environment to solve LeetCode problems using PHPUnit for testing and Ray for debugging.
- PHP installed via Homebrew on your machine.
- Composer installed on your machine.
- PHPStorm installed on your machine.
- Ray installed on your machine.
- Download and install PHPStorm from the PHPStorm website.
- Download and install Composer from the Composer website.
- Open your terminal.
- Install PHP via Homebrew:
brew install php- Open PHPStorm and create a new project by selecting
File > New Project. - Name your project
LeetCodeProblemsand choose a location for the project.
- Create the following directory structure within the project:
LeetCodeProblems/
├── problems/
│ ├── problem_409/
│ │ ├── Solution.php
│ │ └── tests/
│ │ └── TestCases.php
└── ...- Open your terminal and navigate to the root directory of your project.
- Run the following command to install PHPUnit:
composer require --dev phpunit/phpunit- Run the following command to install Ray via Composer:
composer require spatie/ray- In the root directory of your project, create a file named
phpunit.xmlwith the following content:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Project Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>- In the root directory of your project, create a file named
ray.phpwith the following content:
<?php
return [ 'enable' => true, 'host' => 'localhost', 'port' => 23517, ];
- Create
Solution.phpinproblems/problem_409/with the following content:
<?php
class Solution {
/**
-
@param String $s
@return Integer */ public function longestPalindrome($s) { ray('Starting longestPalindrome with input:', $s); // Debug start
$charCount = array_fill(0, 128, 0); // ASCII range for upper and lower case letters // Count the frequency of each character for ($i = 0; $i < strlen($s); $i++) { $charCount[ord($s[$i])]++; } ray('Character counts:', $charCount); // Debug character counts $length = 0; $oddFound = false; // Calculate the length of the longest palindrome for ($i = 0; $i < 128; $i++) { if ($charCount[$i] % 2 == 0) { $length += $charCount[$i]; } else { $length += $charCount[$i] - 1; $oddFound = true; } } // If there was any odd count, we can add one center character if ($oddFound) { $length++; } ray('Final palindrome length:', $length); // Debug final length return $length;} }
- Create
TestCases.phpinproblems/problem_409/tests/with the following content:
<?phpuse PHPUnit\Framework\TestCase; use Spatie\Ray\Ray;
require_once DIR . '/../Solution.php'; // Adjust the path as needed
class TestCases extends TestCase { public function testLongestPalindrome() { $solution = new Solution(); ray()->clearAll(); // Clear previous Ray output
// Example 1 $input = "abccccdd"; $expected = 7; ray("Test case 1 - Input: $input, Expected: $expected, Got: {$solution->longestPalindrome($input)}"); $this->assertEquals($expected, $solution->longestPalindrome($input)); // Example 2 $input = "a"; $expected = 1; ray("Test case 2 - Input: $input, Expected: $expected, Got: {$solution->longestPalindrome($input)}"); $this->assertEquals($expected, $solution->longestPalindrome($input)); }
}
- Configure PHPUnit in PHPStorm:
- Go to
File > Settings > Languages & Frameworks > PHP > Test Frameworks. - Click
+and selectPHPUnit by Composer autoload. - Set the path to the
phpunitexecutable:vendor/bin/phpunit.
- Go to
- Configure Ray in PHPStorm:
- Ensure Ray is properly configured in
ray.phpand that the Ray application is running on your computer.
- Ensure Ray is properly configured in
- Run Tests in PHPStorm:
- Right-click on the
TestCases.phpfile or thetestsdirectory and selectRun 'PHPUnit tests in ...'.
- Right-click on the
- View Ray Output:
- Open the Ray application to see the debug output from your tests.
- Debug with Ray:
- Use
ray()calls within your code to send debug messages to Ray.
- Use
- Run the Tests:
- Running the tests in PHPStorm will execute the test cases and output results in the test runner window.
- View Debug Output in Ray:
- The Ray application will display messages sent from your tests, helping you see what's happening internally.
© 2024 Zemen (00bx). All rights reserved. 🌟 hope ya have a happy coding ^^🚀