Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126180 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id C30311A00BD for ; Thu, 26 Dec 2024 18:04:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1735236092; bh=S4eF43NKgEItY784cBb61KJRhYrgVDc7MLxy8gHkwNI=; h=From:Subject:Date:References:Cc:In-Reply-To:To:From; b=C2c8rYmRudP5k91Q22+qYIyOSrV2a3LZGmIBHiM0Ki0tqWuuyfdjp38lntexwX5Nc uh3qPEBdN/QlHABj8xOl/3sJhxJriXsTxddLMAKvD26hPpEZ66FoJzRKjHVyKWbLuC xwsKMlawkvpepuRB8cgf+4jYKkCz3LpCjTMNElfnyHwAwY51z+34R1CunvnOC9ERYH 2tIKVvpllt0CcQXk40Dozetf/LbZ/ClnwoQ/DdFf2TxudsBYXUNf1jIuiXYJkxlmIj Y2GZgd7/Raw39YZI4ScxdHXZOwab65tn3ma+tF7ZUXTslTEkiUmDEzfXSV8i62SR5w Gpei+4lJ0BwJQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 90CC3180071 for ; Thu, 26 Dec 2024 18:01:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: * X-Spam-Status: No, score=1.5 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,HTML_MESSAGE, MIME_HTML_ONLY,MIME_HTML_ONLY_MULTI,MIME_QP_LONG_LINE,MPART_ALT_DIFF, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 26 Dec 2024 18:01:31 +0000 (UTC) Received: by mail-yb1-f178.google.com with SMTP id 3f1490d57ef6-e53a5ff2233so5216907276.3 for ; Thu, 26 Dec 2024 10:04:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=benramsey.com; s=google; t=1735236270; x=1735841070; darn=lists.php.net; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=LYtoQIYe/1f0zUGV1hCKZSHhO9ezmpIX/quT+UbkIT4=; b=EozaQ/Q1LEtA5U2xMwoY16CWmZ8U+D4sVViHq/Ed+a9J7RuSYa78aq6VpEZyxWrLds TaVTZdhDj8nCGqnLeoZuKoYr9yn4sYZmM2gP+3uMOO5JiqLUo/QZ8tBMP4tPh2pgf/u3 TKktCD1xMByi0vcXnInnvlU7n8pjy0Cr8rvIR310ysJzxbN0K8htRnZFnWE4+FGEr8wU nf6do2uR26YzrZlc3Puph0uVIugaugz6jPUHM+wbzO0txgR6/rKc6oI6aPoa7DZ5NKav C4TcElDOAON/aDudek6mI2RM6/dCYF84AtrBTR1dL/u7UREv/SGfsgP0IFUpYsL/Q5+8 JwUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735236270; x=1735841070; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LYtoQIYe/1f0zUGV1hCKZSHhO9ezmpIX/quT+UbkIT4=; b=NfP5S2FmIL8dazIlm48W79hc+d6lJNk1WiA2E1+i7oA9FZWsIuesYe/JVEZUHvv+8y aaF3cGWPA/XODVp+hbLhz2oAvC/fs4YhX+gmvHVkpMNje1pLrS4LCJK6GbCru36aCOWE fkPVrpcBnTREEFRIXlkDVAuu1daLWnx6LaEkjTjR7FlJgtHUcXhSdCG7Wb5AI3rAu8id ldd2WWlkOV8gIVAVOUSUMudrrFwNDob5WeqXoqBW5SLQjtSn6IkPhtWMr82PUq0xlFJt fRQygPOAhQtnhFRo2zIOTWHV2tds0xNfSjI0ALp804TPenWBprEFFCf+ZlDD1DJxIXAg 6RTg== X-Gm-Message-State: AOJu0Yy8WFzo5r2XwKqkppeSwPClC15mr8EUg1i5k36UzKEa45rUyXxd yEEWrFNcHbEtsxXtZZH5yx5jGTCWIPiLAdYdjFimZV8G+g/onOX8fIrtIXN2LbMwqmEPBDtP+Ps = X-Gm-Gg: ASbGnctiI1+5cNMhae3ZtczGcfcIgsjNwvujhpcFXJ5WUg0mVIPP7DFfw5asXLxqqJy AyeSJbbDa6fAvxkmwzmEDnvR4Gvu8EStOTwJrpia1eK6FRqPMLSSF738oc4iBxztsqpCZAXwIX2 W9YY8Fbpx5DM/44QoQ+woTjBm/OZpXXD/+1LB1jrXHH1Xo1Xa6ix9yP0N2L5VnUVGeQ/3aYkg0T X7UcsYPrdXBVpS5H6Kpls6r1MPIjxaeYdDKjnHBXtOV9EXQmZ+EBujDoPbZs5HzO4F3TmoEM7ru oXCCdfdip2pPgaZCADDjFNsX11cm879Lqz8pULx708JIwExI X-Google-Smtp-Source: AGHT+IGEwlS3twI284B7ModFon2nLL6BithxqyU0aV7ZD+fHoqj93/1WhKNXesieeRKvh/XVvExkdQ== X-Received: by 2002:a05:690c:ed2:b0:6ee:40f0:dda8 with SMTP id 00721157ae682-6f3f82159admr142362147b3.30.1735236270630; Thu, 26 Dec 2024 10:04:30 -0800 (PST) Received: from smtpclient.apple (h96-61-170-179.lvrgtn.broadband.dynamic.tds.net. [96.61.170.179]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6f3e73e7835sm38055177b3.20.2024.12.26.10.04.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 26 Dec 2024 10:04:30 -0800 (PST) Content-Type: multipart/alternative; boundary=Apple-Mail-ECA41D4D-3234-45FF-BE08-D6527C2992BE Content-Transfer-Encoding: 7bit Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow Mime-Version: 1.0 (1.0) Subject: Re: [PHP-DEV] C Unit testing and mocking Date: Thu, 26 Dec 2024 12:04:19 -0600 Message-ID: <32BBE792-FB6F-46CB-9621-05B311BB5C8B@benramsey.com> References: Cc: PHP internals list In-Reply-To: To: Jakub Zelenka X-Mailer: iPhone Mail (22C152) From: ben@benramsey.com (Ben Ramsey) --Apple-Mail-ECA41D4D-3234-45FF-BE08-D6527C2992BE Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
= On Dec 26, 2024, at 06:19, Jakub Zelenka <bukka@php.net> wrote:
=EF=BB=BF
On Tue, Dec 24, 2024 at 8:22=E2=80=AFPM Ben Ra= msey <ben@benramsey.com> wrot= e:
On Dec 16, 2024, at 07:21, Jakub Zelenka <bukka@php.net> wrote:

=EF=BB=BF
Hi,

I have been looking into how to test some cases w= here integration tests are very difficult or even impossible to create for. T= hose are often found in networking related and system specific code code (ne= twork.c, streams, FPM and more). I was recently fixing one such bug and deci= ded to give a try which resulted in this PR: https://github.com/php/php-src= /pull/16987 .

There was a suggestion of RFC but= that might be a bit too much as it's just an internal change / addition. Bu= t certainly some overview on internals should be done so writing this instea= d.

I decided to use cmocka in that PR because I had= some experience with that. It's quite small and still very powerful and all= ow vast mocking options. It's a bit manual but it gives a bigger control ove= r the mock. It relies on --wrap linking option that replaces original functi= ons with wraps. This is however available only on Linux (or maybe some other= Unix variants) but doesn't work on MacOS or Windows. The developers that wa= nt to use it on those platforms would need to use some Linux Virtualisation o= ption (e.g. Docker). It also requires static library which is supported by e= mbed SAPI that can be compiled statically. That limits number of extensions t= o use but the main use cases don't really have deps so it should be fine.

I did also some research into the other mocking libra= ries in C. There is a Unity with CMock, FFF and some C++ libs like GUnit, Cr= iterion and Trompeloeil that I looked into. I quickly discarded GUnit a= nd Trompeloeil as they relay on C++ virtual methods and require wrappin= g C code to C++ which is very inconvenient. FFF seems too simple and maybe q= uite inflexible for our needs as well. Criterion also optionally uses wrap s= o I didn't see much advantages compare to cmocka. So it left Unity with CMoc= k that allows generating custom mocks using a Ruby script. That seemed initi= ally quite nice but after spending around two hours with trying to make it w= orks for PHP codebase, I just gave up. It gets quite messy for complex scena= rios and I just didn't figure out how to nicely mock libc functions without a= ny modification to php-src.

In terms of CI. It has g= ot its own build which is very simple and it tests just specific parts so we= could just limit it to run only for changed files which might be quite conv= enient.

So the proposed PR is probably the only rea= sonable unit testing that I can come up with. I think it should be completel= y optional initially for people to use - more like an experiment. If it beco= mes used, then good of course. And if it becomes pain, we can just get rid o= f it. Has anyone got any objections to get this merged? If not I plan to mer= ge it early in January.

Cheers

=
Jakub


FWIW, as someone still very new t= o C, I found Criterion quite easy to use, and I was able to quickly grasp it= s concepts and start using it right away. I can=E2=80=99t say the same for o= ther C testing libraries I tried.

I just checked Criterion a bit more and it actually does not have built-in= mocking. It could be used with --wrap but it doesn't have any expectations l= ike cmocka. There's Mimick [1] from the same author which is indendent but i= ts docs still say that it's experimental so probably not a good idea to use a= n experimental tool as its API can change. So all in all it's not probably a= n option for us.


I=E2=80=99ve not done any mocking in C= yet, so I can=E2=80=99t offer much there, but I see Mimick hasn=E2=80=99t h= ad a tagged release since 2017, even though development still appears active= , but I agree it might not be mature enough to standardize on.
It=E2=80=99s interesting to note David Carlier (a.k.a. devnexen)= has made some contributions to Mimick. :-)

Cheers,=
Ben

= --Apple-Mail-ECA41D4D-3234-45FF-BE08-D6527C2992BE--