public
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/halorgium/mephisto.git
commit  3bbe3ca69a5aa2e4346e62a988edab0b3d9451b8
tree    75b03c20ef42d5fcd6a7ade7d2618846bd54acd2
parent  96cdc957653159c24ebf59a5096bcde6554ba58f
mephisto / test / referenced_caching_test_helper.rb
100644 52 lines (44 sloc) 1.808 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Mephisto # :nodoc:
  module Caching # :nodoc:
    module ReferencedCachingTestHelper
      # Prepares a caching directory for use. Put this in your test case's #setup method.
      # Be sure to CHANGE the page_cache_directory in config/environments/test.rb, otherwise
      # this will remove your complete public directory
      def prepare_for_caching!
        FileUtils.rm_rf ActionController::Base.page_cache_directory rescue nil
        FileUtils.mkdir_p ActionController::Base.page_cache_directory
      end
 
      def assert_caches_pages(*urls)
        yield(urls) if block_given?
        urls.map { |url| assert_cached url }
      end
 
      def assert_expires_pages(*urls)
        yield(urls) if block_given?
        urls.map { |url| assert_not_cached url }
      end
 
      # Asserts a page was cached.
      def assert_cached(url)
        assert page_cache_exists?(url), "#{url} is not cached"
      end
 
      # Asserts a page was not cached.
      def assert_not_cached(url)
        assert !page_cache_exists?(url), "#{url} is cached"
      end
 
      alias assert_caches_page assert_caches_pages
      alias assert_expires_page assert_expires_pages
 
      private
        # Gets the page cache filename given a relative URL like /blah
        def page_cache_file(url)
          ActionController::Base.send :page_cache_file, url.gsub(/^https?:\/\//, '')
        end
 
        # Gets a test page cache filename given a relative URL like /blah
        def page_cache_test_file(url)
          File.join ActionController::Base.page_cache_directory, page_cache_file(url).reverse.chomp('/').reverse
        end
 
        # Returns true/false whether the page cache file exists.
        def page_cache_exists?(url)
          File.exists? page_cache_test_file(url)
        end
    end
  end
end