Browse Source

Fix at matcher pt1 (#15467)

Mark Story 5 years ago
parent
commit
931baf8a28

+ 7 - 15
tests/TestCase/Collection/Iterator/FilterIteratorTest.php

@@ -32,21 +32,13 @@ class FilterIteratorTest extends TestCase
     public function testFilter()
     {
         $items = new \ArrayIterator([1, 2, 3]);
-        $callable = $this->getMockBuilder(\stdClass::class)
-            ->addMethods(['__invoke'])
-            ->getMock();
-        $callable->expects($this->at(0))
-            ->method('__invoke')
-            ->with(1, 0, $items)
-            ->will($this->returnValue(false));
-        $callable->expects($this->at(1))
-            ->method('__invoke')
-            ->with(2, 1, $items)
-            ->will($this->returnValue(true));
-        $callable->expects($this->at(2))
-            ->method('__invoke')
-            ->with(3, 2, $items)
-            ->will($this->returnValue(false));
+        $callable = function ($value, $key, $itemArg) use ($items) {
+            $this->assertSame($items, $itemArg);
+            $this->assertContains($value, $items);
+            $this->assertContains($key, [0, 1, 2]);
+
+            return $value === 2;
+        };
 
         $filter = new FilterIterator($items, $callable);
         $this->assertEquals([1 => 2], iterator_to_array($filter));

+ 7 - 15
tests/TestCase/Collection/Iterator/ReplaceIteratorTest.php

@@ -32,21 +32,13 @@ class ReplaceIteratorTest extends TestCase
     public function testReplace()
     {
         $items = new \ArrayIterator([1, 2, 3]);
-        $callable = $this->getMockBuilder(\stdClass::class)
-            ->addMethods(['__invoke'])
-            ->getMock();
-        $callable->expects($this->at(0))
-            ->method('__invoke')
-            ->with(1, 0, $items)
-            ->will($this->returnValue(1));
-        $callable->expects($this->at(1))
-            ->method('__invoke')
-            ->with(2, 1, $items)
-            ->will($this->returnValue(4));
-        $callable->expects($this->at(2))
-            ->method('__invoke')
-            ->with(3, 2, $items)
-            ->will($this->returnValue(9));
+        $callable = function ($value, $key, $itemsArg) use ($items) {
+            $this->assertSame($items, $itemsArg);
+            $this->assertContains($value, $items);
+            $this->assertContains($key, [0, 1, 2]);
+
+            return $value > 1 ? $value * $value : $value;
+        };
 
         $map = new ReplaceIterator($items, $callable);
         $this->assertEquals([1, 4, 9], iterator_to_array($map));

+ 4 - 4
tests/TestCase/Command/SchemaCacheCommandsTest.php

@@ -133,9 +133,9 @@ class SchemaCacheCommandsTest extends TestCase
      */
     public function testBuildNoArgs()
     {
-        $this->cache->expects($this->at(0))
+        $this->cache->expects($this->atLeastOnce())
             ->method('set')
-            ->with('test_articles')
+            ->withConsecutive(['test_articles'])
             ->will($this->returnValue(true));
 
         $this->exec('schema_cache build --connection test');
@@ -211,9 +211,9 @@ class SchemaCacheCommandsTest extends TestCase
      */
     public function testClearNoArgs()
     {
-        $this->cache->expects($this->at(0))
+        $this->cache->expects($this->atLeastOnce())
             ->method('delete')
-            ->with('test_articles')
+            ->withConsecutive(['test_articles'])
             ->will($this->returnValue(true));
 
         $this->exec('schema_cache clear --connection test');

+ 10 - 13
tests/TestCase/Controller/ControllerTest.php

@@ -508,19 +508,16 @@ class ControllerTest extends TestCase
         $controller = new Controller(null, null, null, $eventManager);
 
         $eventManager
-            ->expects($this->at(0))
+            ->expects($this->exactly(2))
             ->method('dispatch')
-            ->with($this->callback(function (EventInterface $event) {
-                return $event->getName() === 'Controller.initialize';
-            }))
-            ->will($this->returnValue(new Event('stub')));
-
-        $eventManager
-            ->expects($this->at(1))
-            ->method('dispatch')
-            ->with($this->callback(function (EventInterface $event) {
-                return $event->getName() === 'Controller.startup';
-            }))
+            ->withConsecutive(
+                [$this->callback(function (EventInterface $event) {
+                    return $event->getName() === 'Controller.initialize';
+                })],
+                [$this->callback(function (EventInterface $event) {
+                    return $event->getName() === 'Controller.startup';
+                })]
+            )
             ->will($this->returnValue(new Event('stub')));
 
         $controller->startupProcess();
@@ -787,7 +784,7 @@ class ControllerTest extends TestCase
         $this->expectException(\UnexpectedValueException::class);
         $this->expectExceptionMessage(
             'Controller actions can only return ResponseInterface instance or null. '
-            . 'Got string instead.'
+                . 'Got string instead.'
         );
 
         $url = new ServerRequest([

+ 3 - 2
tests/TestCase/Database/Log/LoggingStatementTest.php

@@ -102,8 +102,9 @@ class LoggingStatementTest extends TestCase
         $inner->method('execute')->will($this->returnValue(true));
 
         $date = new \DateTime('2013-01-01');
-        $inner->expects($this->at(0))->method('bindValue')->with('a', 1);
-        $inner->expects($this->at(1))->method('bindValue')->with('b', $date);
+        $inner->expects($this->atLeast(2))
+              ->method('bindValue')
+              ->withConsecutive(['a', 1], ['b', $date]);
 
         $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
         $st = new LoggingStatement($inner, $driver);

+ 2 - 2
tests/TestCase/Log/Engine/ConsoleLogTest.php

@@ -32,7 +32,7 @@ class ConsoleLogTest extends TestCase
         $output = $this->getMockBuilder('Cake\Console\ConsoleOutput')->getMock();
 
         $message = ' Error: oh noes</error>';
-        $output->expects($this->at(0))
+        $output->expects($this->once())
             ->method('write')
             ->with($this->stringContains($message));
 
@@ -67,7 +67,7 @@ class ConsoleLogTest extends TestCase
     {
         $output = $this->getMockBuilder(ConsoleOutput::class)->getMock();
 
-        $output->expects($this->at(0))
+        $output->expects($this->once())
             ->method('setOutputAs')
             ->with(ConsoleOutput::RAW);
 

+ 6 - 3
tests/TestCase/Log/Engine/SyslogLogTest.php

@@ -79,9 +79,12 @@ class SyslogLogTest extends TestCase
         $log = $this->getMockBuilder(SyslogLog::class)
             ->onlyMethods(['_open', '_write'])
             ->getMock();
-        $log->expects($this->at(1))->method('_write')->with(LOG_DEBUG, 'debug: Foo');
-        $log->expects($this->at(2))->method('_write')->with(LOG_DEBUG, 'debug: Bar');
-        $log->expects($this->exactly(2))->method('_write');
+        $log->expects($this->exactly(2))
+            ->method('_write')
+            ->withConsecutive(
+                [LOG_DEBUG, 'debug: Foo'],
+                [LOG_DEBUG, 'debug: Bar']
+            );
         $log->log('debug', "Foo\nBar");
     }
 

+ 5 - 6
tests/TestCase/Log/LogTraitTest.php

@@ -37,13 +37,12 @@ class LogTraitTest extends TestCase
     public function testLog()
     {
         $mock = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
-        $mock->expects($this->at(0))
+        $mock->expects($this->exactly(2))
             ->method('log')
-            ->with('error', 'Testing');
-
-        $mock->expects($this->at(1))
-            ->method('log')
-            ->with('debug', 'message');
+            ->withConsecutive(
+                ['error', 'Testing'],
+                ['debug', 'message']
+            );
 
         Log::setConfig('trait_test', ['engine' => $mock]);
         $subject = $this->getObjectForTrait('Cake\Log\LogTrait');

+ 24 - 23
tests/TestCase/ORM/Association/BelongsToManyTest.php

@@ -207,9 +207,9 @@ class BelongsToManyTest extends TestCase
     public function testJunctionConnection()
     {
         $mock = $this->getMockBuilder(Connection::class)
-                ->onlyMethods(['setDriver'])
-                ->setConstructorArgs([['name' => 'other_source']])
-                ->getMock();
+            ->onlyMethods(['setDriver'])
+            ->setConstructorArgs([['name' => 'other_source']])
+            ->getMock();
         ConnectionManager::setConfig('other_source', $mock);
         $this->article->setConnection(ConnectionManager::get('other_source'));
 
@@ -638,27 +638,28 @@ class BelongsToManyTest extends TestCase
         $joint->method('getPrimaryKey')
             ->will($this->returnValue(['article_id', 'tag_id']));
 
-        $joint->expects($this->at(1))
+        $joint->expects($this->exactly(2))
             ->method('save')
-            ->will($this->returnCallback(function (EntityInterface $e, $opts) use ($entity) {
-                $expected = ['article_id' => 1, 'tag_id' => 2];
-                $this->assertEquals($expected, $e->toArray());
-                $this->assertEquals(['foo' => 'bar'], $opts);
-                $this->assertTrue($e->isNew());
-
-                return $entity;
-            }));
-
-        $joint->expects($this->at(2))
-            ->method('save')
-            ->will($this->returnCallback(function (EntityInterface $e, $opts) use ($entity) {
-                $expected = ['article_id' => 1, 'tag_id' => 3];
-                $this->assertEquals($expected, $e->toArray());
-                $this->assertEquals(['foo' => 'bar'], $opts);
-                $this->assertTrue($e->isNew());
-
-                return $entity;
-            }));
+            ->will(
+                $this->onConsecutiveCalls(
+                    $this->returnCallback(function (EntityInterface $e, $opts) use ($entity) {
+                        $expected = ['article_id' => 1, 'tag_id' => 2];
+                        $this->assertEquals($expected, $e->toArray());
+                        $this->assertEquals(['foo' => 'bar'], $opts);
+                        $this->assertTrue($e->isNew());
+
+                        return $entity;
+                    }),
+                    $this->returnCallback(function (EntityInterface $e, $opts) use ($entity) {
+                        $expected = ['article_id' => 1, 'tag_id' => 3];
+                        $this->assertEquals($expected, $e->toArray());
+                        $this->assertEquals(['foo' => 'bar'], $opts);
+                        $this->assertTrue($e->isNew());
+
+                        return $entity;
+                    })
+                )
+            );
 
         $this->assertTrue($assoc->link($entity, $tags, $saveOptions));
         $this->assertSame($entity->test, $tags);

+ 22 - 25
tests/TestCase/ORM/EntityTest.php

@@ -250,13 +250,14 @@ class EntityTest extends TestCase
             ->onlyMethods(['set'])
             ->disableOriginalConstructor()
             ->getMock();
-        $entity->expects($this->at(0))
+        $entity->expects($this->exactly(2))
             ->method('set')
-            ->with(['a' => 'b', 'c' => 'd'], ['setter' => true, 'guard' => false]);
-
-        $entity->expects($this->at(1))
-            ->method('set')
-            ->with(['foo' => 'bar'], ['setter' => false, 'guard' => false]);
+            ->withConsecutive(
+                [
+                    ['a' => 'b', 'c' => 'd'], ['setter' => true, 'guard' => false],
+                ],
+                [['foo' => 'bar'], ['setter' => false, 'guard' => false]]
+            );
 
         $entity->__construct(['a' => 'b', 'c' => 'd']);
         $entity->__construct(['foo' => 'bar'], ['useSetters' => false]);
@@ -583,7 +584,7 @@ class EntityTest extends TestCase
         $entity = $this->getMockBuilder(Entity::class)
             ->onlyMethods(['unset'])
             ->getMock();
-        $entity->expects($this->at(0))
+        $entity->expects($this->once())
             ->method('unset')
             ->with('foo');
         unset($entity->foo);
@@ -629,15 +630,13 @@ class EntityTest extends TestCase
         $entity = $this->getMockBuilder(Entity::class)
             ->onlyMethods(['get'])
             ->getMock();
-        $entity->expects($this->at(0))
-            ->method('get')
-            ->with('foo')
-            ->will($this->returnValue('worked'));
-
-        $entity->expects($this->at(1))
+        $entity->expects($this->exactly(2))
             ->method('get')
-            ->with('bar')
-            ->will($this->returnValue('worked too'));
+            ->withConsecutive(['foo'], ['bar'])
+            ->will($this->onConsecutiveCalls(
+                $this->returnValue('worked'),
+                $this->returnValue('worked too')
+            ));
 
         $this->assertSame('worked', $entity['foo']);
         $this->assertSame('worked too', $entity['bar']);
@@ -655,15 +654,13 @@ class EntityTest extends TestCase
             ->getMock();
         $entity->setAccess('*', true);
 
-        $entity->expects($this->at(0))
-            ->method('set')
-            ->with('foo', 1)
-            ->will($this->returnSelf());
-
-        $entity->expects($this->at(1))
+        $entity->expects($this->exactly(2))
             ->method('set')
-            ->with('bar', 2)
-            ->will($this->returnSelf());
+            ->withConsecutive(['foo', 1], ['bar', 2])
+            ->will($this->onConsecutiveCalls(
+                $this->returnSelf(),
+                $this->returnSelf()
+            ));
 
         $entity['foo'] = 1;
         $entity['bar'] = 2;
@@ -680,7 +677,7 @@ class EntityTest extends TestCase
         $entity = $this->getMockBuilder(Entity::class)
             ->onlyMethods(['unset'])
             ->getMock();
-        $entity->expects($this->at(0))
+        $entity->expects($this->once())
             ->method('unset')
             ->with('foo');
         unset($entity['foo']);
@@ -784,7 +781,7 @@ class EntityTest extends TestCase
             'title' => 'Foo',
             'author_id' => 3,
         ]);
-        $expected = ['author_id' => 3, 'title' => 'Foo', ];
+        $expected = ['author_id' => 3, 'title' => 'Foo',];
         $this->assertEquals($expected, $entity->extract(['author_id', 'title']));
 
         $expected = ['id' => 1];