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

Remove deprecated setMethods #9

Merged
merged 10 commits into from Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 7 additions & 12 deletions .circleci/config.yml
@@ -1,29 +1,24 @@
version: 2.0
version: 2.1
jobs:
build:
machine:
image: ubuntu-2004:202010-01
docker:
- image: circleci/php:7.3
environment:
CC_TEST_REPORTER_ID: 7b1ac919c90c8ea384b4998b267e0f629185a26ffdf0f96ef4f4a09f53488ed2
XDEBUG_MODE: coverage
working_directory: ~/repo
steps:
- checkout
- run:
name: Setup DDEV
command: |
curl -LO https://raw.githubusercontent.com/drud/ddev/master/scripts/install_ddev.sh && bash install_ddev.sh
- run:
name: Setup Code Climate test-reporter
name: Set up Code Climate test-reporter
command: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
- run:
name: Run tests
command: |
ddev start
ddev xdebug
ddev composer install
composer install
./cc-test-reporter before-build
ddev exec ./vendor/bin/phpunit --testsuite all --coverage-clover clover.xml
./vendor/bin/phpunit --testsuite all --coverage-clover clover.xml
sed -i 's+/var/www/html/+/home/circleci/repo/+g' clover.xml
./cc-test-reporter after-build --coverage-input-type clover --exit-code $?
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -2,3 +2,5 @@
vendor
composer.lock
coverage
.vscode
.phpunit*
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -20,21 +20,21 @@ Creating a double for the body object with plain phpunit might look like this:
```php
$organ = $this->getMockBuilder(Organ::class)
->disableOriginalConstructor()
->setMethods(['getName'])
->onlyMethods(['getName'])
->getMock();

$organ->method('getName')->willReturn('brain');

$system = $this->getMockBuilder(System::class)
->disableOriginalConstructor()
->setMethods(['getOrgan'])
->onlyMethods(['getOrgan'])
->getMock();

$system->method('getOrgan')->willReturn($organ);

$body = $this->getMockBuilder(Body::class)
->disableOriginalConstructor()
->setMethods(['getSystem'])
->onlyMethods(['getSystem'])
->getMock();

$body->method('getSystem')->willReturn($system);
Expand Down
29 changes: 12 additions & 17 deletions phpunit.xml
@@ -1,18 +1,13 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
verbose="false">

<testsuites>
<testsuite name="all">
<directory suffix="Test.php" phpVersion="7.2" phpVersionOperator=">=">test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" verbose="false">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="all">
<directory suffix="Test.php" phpVersion="7.2" phpVersionOperator="&gt;=">test</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/Chain.php
Expand Up @@ -72,7 +72,7 @@ private function build($objectClass)
$builder = $this->getBuilder($objectClass);

if (!empty($methods)) {
$builder->setMethods($methods);
$builder->onlyMethods($methods);
}
$mock = $builder->getMockForAbstractClass();

Expand Down
31 changes: 19 additions & 12 deletions test/ChainTest.php
Expand Up @@ -12,7 +12,8 @@

class ChainTest extends TestCase
{
public function test() {
public function test()
{
$organNames = (new Sequence())->add('mouth')->add('stomach');

$organ = (new Chain($this))
Expand All @@ -33,7 +34,8 @@ public function test() {
$this->assertEquals($body->getSystem('digestive')->getOrgan('mouth')->getName(), 'mouth');
}

public function test2() {
public function test2()
{
$organNames = (new Sequence())->add('mouth')->add('stomach');

$organ = (new Chain($this))
Expand All @@ -56,7 +58,8 @@ public function test2() {
}


public function test3() {
public function test3()
{
$organs = (new Options())
->add('mouth', Organ::class)
->add('stomach', Organ::class);
Expand All @@ -81,7 +84,8 @@ public function test3() {
$this->assertEquals(json_encode(['stomach']), json_encode($chain->getStoredInput('organ')));
}

public function test4() {
public function test4()
{
$this->expectExceptionMessage("blah");

$system = (new Chain($this))
Expand All @@ -94,24 +98,27 @@ public function test4() {
$body->getSystem("blah");
}

public function testNonExistentMethod() {
$this->expectExceptionMessage("method blah does not exist in MockChainTest\Anatomy\Organ");
(new Chain($this))
public function testNonExistentMethod()
{
$this->expectExceptionMessage('Trying to set mock method "blah" with onlyMethods');
(new Chain($this))
->add(Organ::class, 'blah', null)
->getMock();
}

public function testUsingAdddIncorrectly() {
$this->expectExceptionMessage("You should use the add method before using addd.");
(new Chain($this))->addd("hello");
public function testUsingAdddIncorrectly()
{
$this->expectExceptionMessage("You should use the add method before using addd.");
(new Chain($this))->addd("hello");
}

public function testNonExistentOption() {
public function testNonExistentOption()
{
$this->expectExceptionMessage('Option digestive does not exist');
$options = new Options();
$mock = (new Chain($this))
->add(Body::class, 'getSystem', $options)
->getMock();
$mock->getSystem("digestive");
}
}
}
31 changes: 17 additions & 14 deletions test/OptionsTest.php
Expand Up @@ -8,18 +8,21 @@

class OptionsTest extends TestCase
{
public function test() {
$options = new Options();
$options->add("hello", "goodbye");
$options->add("hola", "chao");
$options->add("multi", (new Sequence())->add('adieu')->add('shalom'));
public function test()
{
$options = new Options();
$options->add("hello", "goodbye");
$options->add("hola", "chao");
$options->add("multi", (new Sequence())->add('adieu')->add('shalom'));

$this->assertEquals(json_encode($options->options()), json_encode(["hello", "hola", "multi"]));
$this->assertEquals($options->return("hello"), "goodbye");
$this->assertEquals($options->return("hola"), "chao");
$this->assertEquals($options->return("multi"), "adieu");
$this->assertEquals($options->return("multi"), "shalom");
$this->assertEquals($options->return("multi"), "shalom");
$this->assertNull($options->return('not-an-option'));
}
}
$this->assertEquals(json_encode($options->options()), json_encode(["hello", "hola", "multi"]));
$this->assertEquals($options->return("hello"), "goodbye");
$this->assertEquals($options->return("hola"), "chao");
$this->assertEquals($options->return("multi"), "adieu");
$this->assertEquals($options->return("multi"), "shalom");
$this->assertEquals($options->return("multi"), "shalom");
$this->assertNull($options->return('not-an-option'));
$options->index(2);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a dumb test, and should have an equivalent over on the ChainTest side that actually tests the functionality.

$this->assertEquals(2, $options->getIndex());
}
}
16 changes: 9 additions & 7 deletions test/SequenceTest.php
Expand Up @@ -10,7 +10,8 @@

class SequenceTest extends TestCase
{
public function test() {
public function test()
{
$sequence = new Sequence();
$sequence->add(null);
$sequence->add(1);
Expand All @@ -22,16 +23,17 @@ public function test() {
$this->assertEquals($sequence->return(), 2);
}

public function testSequenceThroughChain() {
$sequence = (new Sequence())
public function testSequenceThroughChain()
{
$sequence = (new Sequence())
->add(null)
->add("hello");

$mock = (new Chain($this))
$mock = (new Chain($this))
->add(Organ::class, 'getName', $sequence)
->getMock();

$this->assertNull($mock->getName());
$this->assertEquals("hello", $mock->getName());
$this->assertNull($mock->getName());
$this->assertEquals("hello", $mock->getName());
}
}
}