Skip to content

Commit

Permalink
implement onlyMethods polyfilled
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Feb 23, 2023
1 parent 81b0bdb commit 917f273
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
10 changes: 7 additions & 3 deletions .circleci/config.yml
Expand Up @@ -30,9 +30,13 @@ jobs:
name: PHPUnit
command: |
ENABLE_COVERAGE=false
if [ "<< parameters.php-version >>" == "8" ] && [ "$ENABLE_COVERAGE" == "true" ]; then
XDEBUG_MODE=coverage phpunit --coverage-clover clover.xml
coveralls --coverage_clover=clover.xml -v -o coveralls-upload.json
if [ "<< parameters.php-version >>" == "8" ]; then
if ["$ENABLE_COVERAGE" == "true"]; then
XDEBUG_MODE=coverage phpunit --coverage-clover clover.xml
coveralls --coverage_clover=clover.xml -v -o coveralls-upload.json
else
phpunit --display-deprecations --testdox
fi
else
phpunit
fi
Expand Down
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -58,6 +58,20 @@ class HelloWorldTest extends TestCase
* Real CircleCI example
* https://app.circleci.com/pipelines/github/pmvc/pmvc?branch=master

## PHPUnit Tip
* Show event
```
phpunit --log-events-text php://stdout
```

* show deprecations
```
phpunit --display-deprecations --testdox
```

* trigger PMVC dev dump
* https://github.com/pmvc-plugin/dev/blob/master/tests/DevWithPhpUnitTest.php



## Install with Composer
Expand All @@ -82,3 +96,5 @@ class HelloWorldTest extends TestCase
* or
* composer require pmvc-plugin/unit

## Other Polyfills
* https://github.com/Yoast/PHPUnit-Polyfills
39 changes: 39 additions & 0 deletions src/TestCase.php
Expand Up @@ -17,6 +17,22 @@ public function __construct(
}
}
}
if (!class_exists('PHPUnit_Framework_MockObject_MockBuilder')) {
class PHPUnit_Framework_MockObject_MockBuilder
{
use \PMVC\Alias;
public $caller;
public function __construct(\PMVC\TestCase $testCase, string $type)
{
$this->setDefaultAlias(
new \PHPUnit\Framework\MockObject\MockBuilder(
$testCase,
$type
)
);
}
}
}
if (!class_exists('TypeError')) {
class TypeError extends Exception
{
Expand All @@ -33,6 +49,19 @@ class TypeError extends Exception
l(__DIR__ . '/TestCase-5');
}

class PMVCMockBuilder extends \PHPUnit_Framework_MockObject_MockBuilder
{
use Alias;
public function pmvc_onlyMethods($methods)
{
if ($this->isCallable('onlyMethods')) {
return $this->onlyMethods($methods);
} else {
return $this->setMethods($methods);
}
}
}

class PMVCUnitException extends \Exception
{
public function __construct(
Expand Down Expand Up @@ -90,6 +119,16 @@ private function _init()
}
}

protected function getPMVCMockBuilder($className)
{
return new PMVCMockBuilder($this, $className);
}

protected function dump()
{
fwrite(STDERR, print_r(func_get_args(), true));
}

protected function altSetup()
{
$this->_init();
Expand Down
28 changes: 28 additions & 0 deletions tests/src/PMVCMockBuilderTest.php
@@ -0,0 +1,28 @@
<?php

namespace PMVC\PlugIn\unit;

use PMVC\TestCase;

class PMVCMockBuilderTest extends TestCase
{
public function testGetPMVCMock()
{
$mock = $this->getPMVCMockBuilder(FakeDebugMock::class)
->pmvc_onlyMethods(['a'])
->getMock();
$mock->expects($this->atLeastOnce())->method('a');

$actual = $mock->a();
$this->assertEquals($actual, null);
$this->assertEquals((new FakeDebugMock())->a(), 'foo');
}
}

class FakeDebugMock
{
public function a()
{
return 'foo';
}
}

0 comments on commit 917f273

Please sign in to comment.