Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THRIFT-5757 Unit tests for php lib #2942

Merged
merged 1 commit into from Mar 12, 2024
Merged

Conversation

sveneld
Copy link
Contributor

@sveneld sveneld commented Mar 2, 2024

After running PHP unit tests for each pull request THRIFT-5756

Next step is writing test for php lib.

It will be several pull request for this task.

  • Did you create an Apache Jira ticket? [ (Request account here, not required for trivial changes)](https://issues.apache.org/jira/browse/THRIFT-5757)
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • If your change does not involve any code, include [skip ci] anywhere in the commit message to free up build resources.

@sveneld
Copy link
Contributor Author

sveneld commented Mar 2, 2024

@pkvach take a look please

if ($this->context_ === null) {
$this->context_ = stream_context_create();
} else {
$this->context_ = $context;
Copy link
Contributor Author

@sveneld sveneld Mar 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

giving null to stream_socket_client as a context generate a warning, so it's better to create an empty context if it does not provided

@@ -87,7 +92,8 @@ public function open()
throw new TTransportException('Socket already connected', TTransportException::ALREADY_OPEN);
}

if (empty($this->host_)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does not work because ssl:// always added to hostname

@@ -253,7 +253,9 @@ public function open()
if (function_exists('socket_import_stream') && function_exists('socket_set_option')) {
// warnings silenced due to bug https://bugs.php.net/bug.php?id=70939
$socket = @socket_import_stream($this->handle_);
@socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1);
if ($socket !== false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previous function can return false, and in php 8.0 it trigger a type error, so i added a check.

In future it will be great to remove all @ and work correctly with all warning and error generated by stream functions

@sveneld
Copy link
Contributor Author

sveneld commented Mar 3, 2024

I found a library https://github.com/php-mock/php-mock-phpunit so I will try to rewrite tests with better coverage and best readability

$transport
->expects($this->exactly(count($lowLevelTransportReadAllParams)))
->method('readAll')
->withConsecutive(...$lowLevelTransportReadAllParams)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withConsecutive() is deprecated and has been removed in PHPUnit 10.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will read about it. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sebastianbergmann/phpunit#4026 i read this long discussion. As I understand it's possible to use something like taht

->with(self::callback(function (string $message) {
                static $i = 0;
                return match (++$i) {
                    1 => $message === 'Removing audio files started.',
                    2 => $message === "Removing audio files that older than '2018-01-01 00:00:00'.",
                };
            }));

But maybe someone will offer solution which will be accepted by phpunit maintainers.
I prefer to leave using of this method till migration to php8, where we can use match, maybe to this moment we will have bettre replacement for withConsecutive

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I agree to leave it as is for now. Thanks.

re2c \
composer

RUN pecl install xdebug-3.1.1 && \
echo "zend_extension=xdebug.so" > /etc/php/7.4/cli/conf.d/20-xdebug.ini
Copy link
Contributor Author

@sveneld sveneld Mar 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some docker builds was missed extensions and xdebug

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there may be an issue here.
The Dockerfile installs PHP without specifying a version, so it will install the latest version available in the package repository. However, the Xdebug configuration is written to a specific PHP 7.4 directory. If the installed PHP version is not 7.4, the Xdebug configuration will not be applied because PHP won't be able to find it.

Same for other Dockerfiles as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was easier than I thought :)

RUN apt-get install -y --no-install-recommends php-xdebug

Copy link
Contributor Author

@sveneld sveneld Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkvach build will be updated here a8fb726

@@ -227,7 +228,6 @@ public function flush()
register_shutdown_function(array('Thrift\\Transport\\TCurlClient', 'closeCurlHandle'));
self::$curlHandle = curl_init();
curl_setopt(self::$curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt(self::$curlHandle, CURLOPT_BINARYTRANSFER, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CURLOPT_BINARYTRANSFER This constant had no effect since PHP 5.1.2

@sveneld sveneld force-pushed the php_unit_test_2 branch 3 times, most recently from 5709382 to 4be2491 Compare March 4, 2024 14:17
@Jens-G Jens-G added the php label Mar 4, 2024
@Jens-G
Copy link
Member

Jens-G commented Mar 5, 2024

Can we squash that bunch of commits please? If it is work in progress consider marking it as "draft". If it is otherwise unrelated PRs that you want to stay separated for some reason or another, do that.

@sveneld sveneld marked this pull request as draft March 6, 2024 05:50
@sveneld
Copy link
Contributor Author

sveneld commented Mar 6, 2024

@pkvach currently I have a trouble when running all tests together

Thrift\Exception\TException : TPhpStream: Could not open php://input
 /opt/project/lib/php/lib/Transport/TPhpStream.php:62
 /opt/project/lib/php/test/Unit/Lib/Transport/TPhpStreamTest.php:67

i added var_dump(error_get_last()); and it showed me such error.

array(4) {
  'type' =>
  int(2)
  'message' =>
  string(89) "Zend OPcache can't be temporary enabled (it may be only disabled till the end of request)"
  'file' =>
  string(19) "Standard input code"
  'line' =>
  int(105)
}

Maybe you have any idea how to fix of how to debug it?

@sveneld
Copy link
Contributor Author

sveneld commented Mar 6, 2024

Can we squash that bunch of commits please? If it is work in progress consider marking it as "draft". If it is otherwise unrelated PRs that you want to stay separated for some reason or another, do that.

Marked as a draft, when i will finish coverage of folder Transport i will squash all commits and mark pull request as ready for review.
Thanks.

@pkvach
Copy link
Contributor

pkvach commented Mar 6, 2024

@sveneld Regarding the falling TPhpStreamTest

Looks like the issue is due to the fact that the fopen file resource created in the data provider is being closed before it can be used in the test.

To fix this, I think we can move the creation of the file resource into the test method itself.

I'm attaching an example solution with changes for testOpen()
TPhpStreamTest.php.zip

Maybe you'll have another option.

@sveneld
Copy link
Contributor Author

sveneld commented Mar 7, 2024

To fix this, I think we can move the creation of the file resource into the test method itself.

You was right, moving creation of file resource into the test method helped to solve the trouble.

One more class left to cover with test in folder Transport and this pull request will be ready :)
But it is not totally finished test coverage, only 38% percent of code is covered with tests..

image

@pkvach
Copy link
Contributor

pkvach commented Mar 7, 2024

Great progress! Thank you for all the good work you do.

@sveneld sveneld marked this pull request as ready for review March 8, 2024 18:23
@sveneld
Copy link
Contributor Author

sveneld commented Mar 8, 2024

@pkvach @Jens-G ready for review

Folder Transport is covered by unit tests

Screenshot from 2024-03-08 19-18-26

* installed, then these null functions will step in and act like cache
* misses.
*/
if (!function_exists('apcu_fetch')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move it inside class as private methods. Global redeclare is not very good.

@sveneld
Copy link
Contributor Author

sveneld commented Mar 8, 2024

Update of docker images will be in #2937

protected function setUp(): void
{
#need to be defined before the TSocketPool class definition
$this->defineFunctionMock('Thrift\Transport', 'function_exists');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->defineFunctionMock('Thrift\Transport', 'function_exists');
self::defineFunctionMock('Thrift\Transport', 'function_exists');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@pkvach
Copy link
Contributor

pkvach commented Mar 9, 2024

LGTM

@sveneld
Copy link
Contributor Author

sveneld commented Mar 9, 2024

@Jens-G let's merge this pull request

@Jens-G Jens-G merged commit 9913004 into apache:master Mar 12, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants