Advertisement
Guest User

Untitled

a guest
Jan 16th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.66 KB | None | 0 0
  1. <?php
  2.  
  3. use Mockery as m;
  4. use Way\Tests\Factory;
  5. use Madserver\Tests\TestCase;
  6.  
  7. class BannersTest extends TestCase {
  8.  
  9.     public function __construct()
  10.     {
  11.         $this->mock = m::mock('Eloquent', 'Banner');
  12.         $this->collection = m::mock('Illuminate\Database\Eloquent\Collection')->shouldDeferMissing();
  13.     }
  14.  
  15.     public function setUp()
  16.     {
  17.         parent::setUp();
  18.  
  19.         $this->attributes = Way\Tests\Factory::banner(['id' => 1]);
  20.         $this->app->instance('Banner', $this->mock);
  21.     }
  22.  
  23.     public function tearDown()
  24.     {
  25.         m::close();
  26.     }
  27.  
  28.     public function testIndex()
  29.     {
  30.         $this->mock->shouldReceive('all')->once()->andReturn($this->collection);
  31.         $this->call('GET', 'banners');
  32.  
  33.         $this->assertViewHas('banners');
  34.     }
  35.  
  36.     public function testCreate()
  37.     {
  38.         $this->call('GET', 'banners/create');
  39.  
  40.         $this->assertResponseOk();
  41.     }
  42.  
  43.     public function testStore()
  44.     {
  45.         $this->mock->shouldReceive('create')->once();
  46.         $this->validate(true);
  47.         $this->call('POST', 'banners');
  48.  
  49.         $this->assertRedirectedToRoute('banners.index');
  50.     }
  51.  
  52.     public function testStoreFails()
  53.     {
  54.         $this->mock->shouldReceive('create')->once();
  55.         $this->validate(false);
  56.         $this->call('POST', 'banners');
  57.  
  58.         $this->assertRedirectedToRoute('banners.create');
  59.         $this->assertSessionHasErrors();
  60.         $this->assertSessionHas('message');
  61.     }
  62.  
  63.     public function testShow()
  64.     {
  65.         $this->mock->shouldReceive('findOrFail')
  66.                    ->with(1)
  67.                    ->once()
  68.                    ->andReturn($this->attributes);
  69.  
  70.         $this->call('GET', 'banners/1');
  71.  
  72.         $this->assertViewHas('banner');
  73.     }
  74.  
  75.     public function testEdit()
  76.     {
  77.         $this->collection->id = 1;
  78.         $this->mock->shouldReceive('find')
  79.                    ->with(1)
  80.                    ->once()
  81.                    ->andReturn($this->collection);
  82.  
  83.         $this->call('GET', 'banners/1/edit');
  84.  
  85.         $this->assertViewHas('banner');
  86.     }
  87.  
  88.     public function testUpdate()
  89.     {
  90.         $this->mock->shouldReceive('find')
  91.                    ->with(1)
  92.                    ->andReturn(m::mock(['update' => true]));
  93.  
  94.         $this->validate(true);
  95.         $this->call('PATCH', 'banners/1');
  96.  
  97.         $this->assertRedirectedTo('banners/1');
  98.     }
  99.  
  100.     public function testUpdateFails()
  101.     {
  102.         $this->mock->shouldReceive('find')->with(1)->andReturn(m::mock(['update' => true]));
  103.         $this->validate(false);
  104.         $this->call('PATCH', 'banners/1');
  105.  
  106.         $this->assertRedirectedTo('banners/1/edit');
  107.         $this->assertSessionHasErrors();
  108.         $this->assertSessionHas('message');
  109.     }
  110.  
  111.     public function testDestroy()
  112.     {
  113.         $this->mock->shouldReceive('find')->with(1)->andReturn(m::mock(['delete' => true]));
  114.  
  115.         $this->call('DELETE', 'banners/1');
  116.     }
  117.  
  118.     protected function validate($bool)
  119.     {
  120.         Validator::shouldReceive('make')
  121.                 ->once()
  122.                 ->andReturn(m::mock(['passes' => $bool]));
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement