Advertisement
Guest User

PHPUnit

a guest
Oct 27th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Tests\Service;
  4.  
  5. use App\Repository\FirstRepository;
  6. use App\Repository\SecondRepository;
  7. use App\Service\AppManager;
  8. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  9.  
  10. class AppManagerTest extends KernelTestCase
  11. {
  12.  
  13.     public function testFirst_Success()
  14.     {
  15.         static::bootKernel();
  16.  
  17.         $a = 2;
  18.         $b = 3;
  19.  
  20.         $firstRepositoryMock = $this->createMock(FirstRepository::class);
  21.         $firstRepositoryMock->expects($this->once())
  22.             ->method('find')
  23.             ->with($a, $b);
  24.  
  25.         $appService = new AppManager(
  26.             $firstRepositoryMock,
  27.             self::$container->get('App\Repository\SecondRepository'),
  28.         );
  29.  
  30.         $this->assertSame(5, $appService->first($a, $b));
  31.     }
  32.  
  33.     public function testSecond_Success()
  34.     {
  35.         static::bootKernel();
  36.  
  37.         $secondRepositoryMock = $this->createMock(SecondRepository::class);
  38.         $secondRepositoryMock->expects($this->once())
  39.             ->method('clear');
  40.  
  41.         $appService = new AppManager(
  42.             self::$container->get('App\Repository\FirstRepository'),
  43.             $secondRepositoryMock,
  44.         );
  45.  
  46.         $this->assertNull($appService->second());
  47.     }
  48.  
  49. }
  50.  
  51. ?>
  52.  
  53. <?php
  54.  
  55. namespace App\Service;
  56.  
  57. use App\Repository\FirstRepository;
  58. use App\Repository\SecondRepository;
  59.  
  60. class AppManager
  61. {
  62.  
  63.     /** @var FirstRepository */
  64.     protected $firstRepository;
  65.     /** @var SecondRepository */
  66.     protected $secondRepository;
  67.  
  68.     public function __construct(
  69.         FirstRepository $firstRepository,
  70.         SecondRepository $secondRepository
  71.     ) {
  72.         $this->firstRepository = $firstRepository;
  73.         $this->secondRepository = $secondRepository;
  74.     }
  75.  
  76.     public function first(int $a, int $b): int
  77.     {
  78.         $this->firstRepository->find($a, $b);
  79.  
  80.         return $a + $b;
  81.     }
  82.  
  83.     public function second(): void
  84.     {
  85.         $this->secondRepository->clear();
  86.     }
  87.  
  88. }
  89.  
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement