Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100264 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 36802 invoked from network); 20 Aug 2017 12:32:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Aug 2017 12:32:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=danack@basereality.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=danack@basereality.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain basereality.com from 74.125.83.47 cause and error) X-PHP-List-Original-Sender: danack@basereality.com X-Host-Fingerprint: 74.125.83.47 mail-pg0-f47.google.com Received: from [74.125.83.47] ([74.125.83.47:36153] helo=mail-pg0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 47/BC-34801-95189995 for ; Sun, 20 Aug 2017 08:32:25 -0400 Received: by mail-pg0-f47.google.com with SMTP id i12so84908870pgr.3 for ; Sun, 20 Aug 2017 05:32:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=/lrdJusSMpRh1MixTLzveNdmuqh8HM+W6WmMz04oXp0=; b=N86+Mz/z4PEZS+H3MdRq3rNe0kPaAr9ATIMxUYSQUl69xIe3G+LQeThTdr+P545MGg iu5c3cmLEwjyjbu2KARcJN2FcfiMGgIJtM0FN5+Q1phEaVegs9J5RUpDtDPaMrF8q2U4 HKkYJgSuCn8u9vUXFwXnD4n72OykCK6G4WYdHgDptE6KIdfc4vT/BOv5zbdjCjNtYHWG 4ojDm60qkz6ls0lWAd/+YSTnK09oI2/CEMkO/sKoJgvJKZ1dvj7PX069cA5QURlJTJlk VxP/E5JLIulpQ+PQCvMMBDa0CZdnw5fMLo72DezoAGSXPI1CeiQHENOp3mrpVF9CV3X+ ctng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=/lrdJusSMpRh1MixTLzveNdmuqh8HM+W6WmMz04oXp0=; b=fUIBoMm2fSxe07qscns/naPs86uAXfUYxNjxFjg3d4BhBczH1OhNqv0BgDGr8t0RB3 uF05+yu5YyuEBtvp/p1cEeK1S6HLjDJOz6kXOoedXHdOFXg+uEdc3pnYrUb1UgcHv8aX FbTRcG4f4vKeSEqhGDJzioW3IhkwQizmpqMQYPDTJyivedCQAz6omcOJ8vNmIU2UymZz 6bTCRXqnQ27JmrJUzHBcVkgWCZrMvE+GKgxeJdnSCJP1hOU5KWioqJfuREjX/iHIsQZz cIxedRkyl6fv5Jl72L+p7ftlnpnYs0mTzmapdOFY8bKCp2Tw4mHFWSrgrcumNngdBc1L E3Fg== X-Gm-Message-State: AHYfb5gMWwL75ZiegZXEf9LLTESMe9pOdtWYyQ0r8j2nvo5lqAZ79N+V tgx9OaJj786VRND5+gO9Aj5pNix6U3tc X-Received: by 10.84.171.132 with SMTP id l4mr15600454plb.183.1503232342163; Sun, 20 Aug 2017 05:32:22 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.180.133 with HTTP; Sun, 20 Aug 2017 05:32:21 -0700 (PDT) X-Originating-IP: [77.99.17.151] In-Reply-To: References: Date: Sun, 20 Aug 2017 13:32:21 +0100 Message-ID: To: Ryan Jentzsch Cc: Internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Easy method of overriding built-in php functions. From: danack@basereality.com (Dan Ackroyd) On 16 August 2017 at 21:02, Ryan Jentzsch wrote: > > Is there an RFC that I didn't find that allows for easily overriding >built-in functions? No, but there is an extension: https://pecl.php.net/package/uopz However, I believe the maintainer of it recommends avoiding using it if at all possible because: > However in the constructor of this class is the following line to > get the expected JSON string: > > $result = file_get_contents('php://input'); > > I can not override the file_get_contents() function. The work around is to > monkey patch the class as a mock and override the constructor Your workaround is bad solution to the wrong problem. Trying to unit test things that interact with the real world is fundamentally broken thing to try to do. A much better thing to do would be to isolate the internal layers of your application from the 'horrible outside world', by extracting the bit that interacts with the outside world with an interface to be injected: interface InputReader { public function getString(); } class PhpInputReader implements InputReader { public function getString() { return file_get_contents('php://input'); } } And then inject a mock version of InputReader when you are doing a unit test for the class that now depends on an InputReader. And just to be clear; it is fundamentally impossible to write a unit test for the class PhpInputReader, as it interacts with the system and so can't be unit tested. Trying to force a unit-test for something that should have an integration test is just the wrong thing to do. There's a talk by a guy called J B Rainsberger that helped me understand the exact nature of unit tests and integrated tests: https://www.youtube.com/watch?v=VDfX44fZoMc or http://vimeo.com/80533536 I really strongly recommend watching it to anyone who is thinking that replacing functions like file_get_contents() with test version is an appropriate thing to do. cheers Dan Ack