PHP code example of adriansuter / php-autoload-override
1. Go to this page and download the library: Download adriansuter/php-autoload-override library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
adriansuter / php-autoload-override example snippets
namespace My\App;
class Probability
{
public function pick(int $probability, string $color1, string $color2): string
{
if (\rand(1, 100) <= $probability) {
return $color1;
} else {
return $color2;
}
}
}
// tests/bootstrap.php
use AdrianSuter\Autoload\Override\OverrideFactory;
use My\App\Probability;
/** @var \Composer\Autoload\ClassLoader $classLoader */
$classLoader =
namespace My\App\Tests;
use AdrianSuter\Autoload\Override\MockRegistry;
use My\App\Probability;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class ProbabilityTest extends TestCase
{
protected function tearDown(): void
{
MockRegistry::reset(Probability::class);
}
#[Test]
public function pickReturnsSecondColorWhenRandExceedsProbability(): void
{
MockRegistry::set(Probability::class, 'rand', 35);
$p = new Probability();
$this->assertSame('blue', $p->pick(34, 'red', 'blue'));
}
#[Test]
public function pickReturnsFirstColorWhenRandMeetsProbability(): void
{
MockRegistry::set(Probability::class, 'rand', 35);
$p = new Probability();
$this->assertSame('red', $p->pick(35, 'red', 'blue'));
}
}
MockRegistry::setGlobal('time', 1574333284);
Override::apply($classLoader, [
Probability::class => [
'rand' => function (int $min, int $max): int {
return MockRegistry::get(Probability::class, 'rand', \rand($min, $max));
}
]
]);
'rand' => function (int $min, int $max): int {
if (MockRegistry::has(Probability::class, 'rand')) {
return MockRegistry::get(Probability::class, 'rand');
}
return \rand($min, $max);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.